blob: 1923fbca3713864b139bc46b7500944c5e9712c5 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
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
20/**
21 * This file provides an interface to the boot loader. Functions defined in
22 * this file should only be called while the boot loader is running.
23 */
24
25#include <assert.h>
26#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060027#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080028#include <inttypes.h>
29#include <stdlib.h>
30#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080031#include <hal/hal_flash.h>
32#include <os/os_malloc.h>
33#include "bootutil/bootutil.h"
34#include "bootutil/image.h"
35#include "bootutil_priv.h"
36
Marti Bolivarfd20c762017-02-07 16:52:50 -050037#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
38#include "bootutil/bootutil_log.h"
39
Fabio Utzigba1fbe62017-07-21 14:01:20 -030040#ifdef MCUBOOT_MYNEWT
41#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030042#endif
43
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040044static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080045
46struct boot_status_table {
47 /**
48 * For each field, a value of 0 means "any".
49 */
50 uint8_t bst_magic_slot0;
51 uint8_t bst_magic_scratch;
52 uint8_t bst_copy_done_slot0;
53 uint8_t bst_status_source;
54};
55
56/**
57 * This set of tables maps swap state contents to boot status location.
58 * When searching for a match, these tables must be iterated in order.
59 */
60static const struct boot_status_table boot_status_tables[] = {
61 {
62 /* | slot-0 | scratch |
63 * ----------+------------+------------|
64 * magic | Good | Any |
65 * copy-done | 0x01 | N/A |
66 * ----------+------------+------------'
67 * source: none |
68 * ------------------------------------'
69 */
70 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
71 .bst_magic_scratch = 0,
72 .bst_copy_done_slot0 = 0x01,
73 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
74 },
75
76 {
77 /* | slot-0 | scratch |
78 * ----------+------------+------------|
79 * magic | Good | Any |
80 * copy-done | 0xff | N/A |
81 * ----------+------------+------------'
82 * source: slot 0 |
83 * ------------------------------------'
84 */
85 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
86 .bst_magic_scratch = 0,
87 .bst_copy_done_slot0 = 0xff,
88 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
89 },
90
91 {
92 /* | slot-0 | scratch |
93 * ----------+------------+------------|
94 * magic | Any | Good |
95 * copy-done | Any | N/A |
96 * ----------+------------+------------'
97 * source: scratch |
98 * ------------------------------------'
99 */
100 .bst_magic_slot0 = 0,
101 .bst_magic_scratch = BOOT_MAGIC_GOOD,
102 .bst_copy_done_slot0 = 0,
103 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
104 },
105
106 {
107 /* | slot-0 | scratch |
108 * ----------+------------+------------|
109 * magic | Unset | Any |
110 * copy-done | 0xff | N/A |
111 * ----------+------------+------------|
112 * source: varies |
113 * ------------------------------------+------------------------------+
114 * This represents one of two cases: |
115 * o No swaps ever (no status to read, so no harm in checking). |
116 * o Mid-revert; status in slot 0. |
117 * -------------------------------------------------------------------'
118 */
119 .bst_magic_slot0 = BOOT_MAGIC_UNSET,
120 .bst_magic_scratch = 0,
121 .bst_copy_done_slot0 = 0xff,
122 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
123 },
124};
125
126#define BOOT_STATUS_TABLES_COUNT \
127 (sizeof boot_status_tables / sizeof boot_status_tables[0])
128
Marti Bolivarfd20c762017-02-07 16:52:50 -0500129#define BOOT_LOG_SWAP_STATE(area, state) \
130 BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \
131 (area), \
132 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
133 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
134 "bad"), \
135 (state)->copy_done, \
136 (state)->image_ok)
137
Christopher Collins92ea77f2016-12-12 15:59:26 -0800138/**
139 * Determines where in flash the most recent boot status is stored. The boot
140 * status is necessary for completing a swap that was interrupted by a boot
141 * loader reset.
142 *
143 * @return A BOOT_STATUS_SOURCE_[...] code indicating where * status should be read from.
144 */
145static int
146boot_status_source(void)
147{
148 const struct boot_status_table *table;
149 struct boot_swap_state state_scratch;
150 struct boot_swap_state state_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800151 int rc;
152 int i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500153 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800154
Fabio Utzig2473ac02017-05-02 12:45:02 -0300155 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800156 assert(rc == 0);
157
Fabio Utzig2473ac02017-05-02 12:45:02 -0300158 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800159 assert(rc == 0);
160
Marti Bolivarfd20c762017-02-07 16:52:50 -0500161 BOOT_LOG_SWAP_STATE("Image 0", &state_slot0);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500162 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
163
Christopher Collins92ea77f2016-12-12 15:59:26 -0800164 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300165 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800166
167 if ((table->bst_magic_slot0 == 0 ||
168 table->bst_magic_slot0 == state_slot0.magic) &&
169 (table->bst_magic_scratch == 0 ||
170 table->bst_magic_scratch == state_scratch.magic) &&
171 (table->bst_copy_done_slot0 == 0 ||
172 table->bst_copy_done_slot0 == state_slot0.copy_done)) {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500173 source = table->bst_status_source;
174 BOOT_LOG_INF("Boot source: %s",
175 source == BOOT_STATUS_SOURCE_NONE ? "none" :
176 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
177 source == BOOT_STATUS_SOURCE_SLOT0 ? "slot 0" :
178 "BUG; can't happen");
179 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800180 }
181 }
182
Marti Bolivarfd20c762017-02-07 16:52:50 -0500183 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800184 return BOOT_STATUS_SOURCE_NONE;
185}
186
187/**
188 * Calculates the type of swap that just completed.
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300189 *
190 * This is used when a swap is interrupted by an external event. After
191 * finishing the swap operation determines what the initial request was.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800192 */
193static int
194boot_previous_swap_type(void)
195{
196 int post_swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800197
198 post_swap_type = boot_swap_type();
199
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300200 switch (post_swap_type) {
201 case BOOT_SWAP_TYPE_NONE : return BOOT_SWAP_TYPE_PERM;
202 case BOOT_SWAP_TYPE_REVERT : return BOOT_SWAP_TYPE_TEST;
203 case BOOT_SWAP_TYPE_PANIC : return BOOT_SWAP_TYPE_PANIC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800204 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300205
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300206 return BOOT_SWAP_TYPE_FAIL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800207}
208
Fabio Utzig358c9352017-07-25 22:10:45 -0300209#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800210static int
Fabio Utzigc08ed212017-06-20 19:28:36 -0300211boot_read_header_from_scratch(struct image_header *out_hdr)
212{
213 const struct flash_area *fap;
214 int rc;
215
216 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
217 if (rc != 0) {
218 return BOOT_EFLASH;
219 }
220
221 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
222 if (rc != 0) {
223 rc = BOOT_EFLASH;
224 }
225
226 flash_area_close(fap);
227 return rc;
228}
Fabio Utzig358c9352017-07-25 22:10:45 -0300229#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzigc08ed212017-06-20 19:28:36 -0300230
231static int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800232boot_read_image_header(int slot, struct image_header *out_hdr)
233{
234 const struct flash_area *fap;
235 int area_id;
236 int rc;
237
238 area_id = flash_area_id_from_image_slot(slot);
239 rc = flash_area_open(area_id, &fap);
240 if (rc != 0) {
241 rc = BOOT_EFLASH;
242 goto done;
243 }
244
245 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
246 if (rc != 0) {
247 rc = BOOT_EFLASH;
248 goto done;
249 }
250
251 rc = 0;
252
253done:
254 flash_area_close(fap);
255 return rc;
256}
257
258static int
259boot_read_image_headers(void)
260{
261 int rc;
262 int i;
263
264 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400265 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800266 if (rc != 0) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300267 /* If at least the first slot's header was read successfully, then
268 * the boot loader can attempt a boot. Failure to read any headers
269 * is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800270 */
271 if (i > 0) {
272 return 0;
273 } else {
274 return rc;
275 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800276 }
277 }
278
279 return 0;
280}
281
282static uint8_t
283boot_write_sz(void)
284{
285 uint8_t elem_sz;
286 uint8_t align;
287
288 /* Figure out what size to write update status update as. The size depends
289 * on what the minimum write size is for scratch area, active image slot.
290 * We need to use the bigger of those 2 values.
291 */
Marti Bolivare2587152017-06-12 15:52:05 -0400292 elem_sz = hal_flash_align(boot_img_fa_device_id(&boot_data, 0));
293 align = hal_flash_align(boot_scratch_fa_device_id(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800294 if (align > elem_sz) {
295 elem_sz = align;
296 }
297
298 return elem_sz;
299}
300
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800301static int
302boot_slots_compatible(void)
303{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400304 size_t num_sectors_0 = boot_img_num_sectors(&boot_data, 0);
305 size_t num_sectors_1 = boot_img_num_sectors(&boot_data, 1);
306 size_t size_0, size_1;
307 size_t i;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800308
309 /* Ensure both image slots have identical sector layouts. */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400310 if (num_sectors_0 != num_sectors_1) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800311 return 0;
312 }
Marti Bolivard3269fd2017-06-12 16:31:12 -0400313 for (i = 0; i < num_sectors_0; i++) {
314 size_0 = boot_img_sector_size(&boot_data, 0, i);
315 size_1 = boot_img_sector_size(&boot_data, 1, i);
316 if (size_0 != size_1) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800317 return 0;
318 }
319 }
320
321 return 1;
322}
323
Christopher Collins92ea77f2016-12-12 15:59:26 -0800324/**
325 * Determines the sector layout of both image slots and the scratch area.
326 * This information is necessary for calculating the number of bytes to erase
327 * and copy during an image swap. The information collected during this
328 * function is used to populate the boot_data global.
329 */
330static int
331boot_read_sectors(void)
332{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800333 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800334
Marti Bolivarcca28a92017-06-12 16:52:22 -0400335 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800336 if (rc != 0) {
337 return BOOT_EFLASH;
338 }
339
Marti Bolivarcca28a92017-06-12 16:52:22 -0400340 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800341 if (rc != 0) {
342 return BOOT_EFLASH;
343 }
344
Marti Bolivare10a7392017-06-14 16:20:07 -0400345 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800346
347 return 0;
348}
349
350static uint32_t
351boot_status_internal_off(int idx, int state, int elem_sz)
352{
353 int idx_sz;
354
355 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
356
357 return idx * idx_sz + state * elem_sz;
358}
359
360/**
361 * Reads the status of a partially-completed swap, if any. This is necessary
362 * to recover in case the boot lodaer was reset in the middle of a swap
363 * operation.
364 */
365static int
366boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
367{
368 uint32_t off;
369 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300370 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800371 int found;
372 int rc;
373 int i;
374
375 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400376 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300377
Christopher Collins92ea77f2016-12-12 15:59:26 -0800378 found = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300379 for (i = 0; i < max_entries; i++) {
Marti Bolivare10a7392017-06-14 16:20:07 -0400380 rc = flash_area_read(fap, off + i * BOOT_WRITE_SZ(&boot_data),
381 &status, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800382 if (rc != 0) {
383 return BOOT_EFLASH;
384 }
385
386 if (status == 0xff) {
387 if (found) {
388 break;
389 }
390 } else if (!found) {
391 found = 1;
392 }
393 }
394
395 if (found) {
396 i--;
Fabio Utzig94d998c2017-05-22 11:02:41 -0400397 bs->idx = i / BOOT_STATUS_STATE_COUNT;
398 bs->state = i % BOOT_STATUS_STATE_COUNT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800399 }
400
401 return 0;
402}
403
404/**
405 * Reads the boot status from the flash. The boot status contains
406 * the current state of an interrupted image copy operation. If the boot
407 * status is not present, or it indicates that previous copy finished,
408 * there is no operation in progress.
409 */
410static int
411boot_read_status(struct boot_status *bs)
412{
413 const struct flash_area *fap;
414 int status_loc;
415 int area_id;
416 int rc;
417
418 memset(bs, 0, sizeof *bs);
419
420 status_loc = boot_status_source();
421 switch (status_loc) {
422 case BOOT_STATUS_SOURCE_NONE:
423 return 0;
424
425 case BOOT_STATUS_SOURCE_SCRATCH:
426 area_id = FLASH_AREA_IMAGE_SCRATCH;
427 break;
428
429 case BOOT_STATUS_SOURCE_SLOT0:
430 area_id = FLASH_AREA_IMAGE_0;
431 break;
432
433 default:
434 assert(0);
435 return BOOT_EBADARGS;
436 }
437
438 rc = flash_area_open(area_id, &fap);
439 if (rc != 0) {
440 return BOOT_EFLASH;
441 }
442
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300443 return boot_read_status_bytes(fap, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800444}
445
446/**
447 * Writes the supplied boot status to the flash file system. The boot status
448 * contains the current state of an in-progress image copy operation.
449 *
450 * @param bs The boot status to write.
451 *
452 * @return 0 on success; nonzero on failure.
453 */
454int
455boot_write_status(struct boot_status *bs)
456{
457 const struct flash_area *fap;
458 uint32_t off;
459 int area_id;
460 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300461 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700462 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800463
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300464 /* NOTE: The first sector copied (that is the last sector on slot) contains
465 * the trailer. Since in the last step SLOT 0 is erased, the first
466 * two status writes go to the scratch which will be copied to SLOT 0!
467 */
468
Fabio Utzig2473ac02017-05-02 12:45:02 -0300469 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800470 /* Write to scratch. */
471 area_id = FLASH_AREA_IMAGE_SCRATCH;
472 } else {
473 /* Write to slot 0. */
474 area_id = FLASH_AREA_IMAGE_0;
475 }
476
477 rc = flash_area_open(area_id, &fap);
478 if (rc != 0) {
479 rc = BOOT_EFLASH;
480 goto done;
481 }
482
483 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400484 boot_status_internal_off(bs->idx, bs->state,
485 BOOT_WRITE_SZ(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800486
David Brown9d725462017-01-23 15:50:58 -0700487 align = hal_flash_align(fap->fa_device_id);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300488 memset(buf, 0xFF, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700489 buf[0] = bs->state;
490
491 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800492 if (rc != 0) {
493 rc = BOOT_EFLASH;
494 goto done;
495 }
496
497 rc = 0;
498
499done:
500 flash_area_close(fap);
501 return rc;
502}
503
504/*
505 * Validate image hash/signature in a slot.
506 */
507static int
508boot_image_check(struct image_header *hdr, const struct flash_area *fap)
509{
David Browndb1d9d32017-01-06 11:07:54 -0700510 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800511
Christopher Collins92ea77f2016-12-12 15:59:26 -0800512 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
513 NULL, 0, NULL)) {
514 return BOOT_EBADIMAGE;
515 }
516 return 0;
517}
518
519static int
520split_image_check(struct image_header *app_hdr,
521 const struct flash_area *app_fap,
522 struct image_header *loader_hdr,
523 const struct flash_area *loader_fap)
524{
525 static void *tmpbuf;
526 uint8_t loader_hash[32];
527
528 if (!tmpbuf) {
529 tmpbuf = malloc(BOOT_TMPBUF_SZ);
530 if (!tmpbuf) {
531 return BOOT_ENOMEM;
532 }
533 }
534
535 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
536 NULL, 0, loader_hash)) {
537 return BOOT_EBADIMAGE;
538 }
539
540 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
541 loader_hash, 32, NULL)) {
542 return BOOT_EBADIMAGE;
543 }
544
545 return 0;
546}
547
548static int
David Brownd930ec62016-12-14 07:59:48 -0700549boot_validate_slot(int slot)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800550{
551 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400552 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800553 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300554
Marti Bolivarf804f622017-06-12 15:41:48 -0400555 hdr = boot_img_hdr(&boot_data, slot);
556 if (hdr->ih_magic == 0xffffffff || hdr->ih_flags & IMAGE_F_NON_BOOTABLE) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300557 /* No bootable image in slot; continue booting from slot 0. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800558 return -1;
559 }
560
David Brownd930ec62016-12-14 07:59:48 -0700561 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800562 if (rc != 0) {
563 return BOOT_EFLASH;
564 }
565
Marti Bolivarf804f622017-06-12 15:41:48 -0400566 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap) != 0)) {
David Brownb38e0442017-02-24 13:57:12 -0700567 if (slot != 0) {
568 flash_area_erase(fap, 0, fap->fa_size);
569 /* Image in slot 1 is invalid. Erase the image and
570 * continue booting from slot 0.
571 */
572 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800573 return -1;
574 }
575
576 flash_area_close(fap);
577
578 /* Image in slot 1 is valid. */
579 return 0;
580}
581
582/**
583 * Determines which swap operation to perform, if any. If it is determined
584 * that a swap operation is required, the image in the second slot is checked
585 * for validity. If the image in the second slot is invalid, it is erased, and
586 * a swap type of "none" is indicated.
587 *
588 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
589 */
590static int
591boot_validated_swap_type(void)
592{
593 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800594
595 swap_type = boot_swap_type();
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300596 switch (swap_type) {
597 case BOOT_SWAP_TYPE_TEST:
598 case BOOT_SWAP_TYPE_PERM:
599 case BOOT_SWAP_TYPE_REVERT:
600 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
601 if (boot_validate_slot(1) != 0) {
602 swap_type = BOOT_SWAP_TYPE_FAIL;
603 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800604 }
605
606 return swap_type;
607}
608
609/**
610 * Calculates the number of sectors the scratch area can contain. A "last"
611 * source sector is specified because images are copied backwards in flash
612 * (final index to index number 0).
613 *
614 * @param last_sector_idx The index of the last source sector
615 * (inclusive).
616 * @param out_first_sector_idx The index of the first source sector
617 * (inclusive) gets written here.
618 *
619 * @return The number of bytes comprised by the
620 * [first-sector, last-sector] range.
621 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300622#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800623static uint32_t
624boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
625{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400626 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800627 uint32_t new_sz;
628 uint32_t sz;
629 int i;
630
631 sz = 0;
632
Marti Bolivard3269fd2017-06-12 16:31:12 -0400633 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800634 for (i = last_sector_idx; i >= 0; i--) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400635 new_sz = sz + boot_img_sector_size(&boot_data, 0, i);
636 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800637 break;
638 }
639 sz = new_sz;
640 }
641
642 /* i currently refers to a sector that doesn't fit or it is -1 because all
643 * sectors have been processed. In both cases, exclude sector i.
644 */
645 *out_first_sector_idx = i + 1;
646 return sz;
647}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300648#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800649
650/**
651 * Erases a region of flash.
652 *
653 * @param flash_area_idx The ID of the flash area containing the region
654 * to erase.
655 * @param off The offset within the flash area to start the
656 * erase.
657 * @param sz The number of bytes to erase.
658 *
659 * @return 0 on success; nonzero on failure.
660 */
661static int
662boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
663{
664 const struct flash_area *fap;
665 int rc;
666
667 rc = flash_area_open(flash_area_id, &fap);
668 if (rc != 0) {
669 rc = BOOT_EFLASH;
670 goto done;
671 }
672
673 rc = flash_area_erase(fap, off, sz);
674 if (rc != 0) {
675 rc = BOOT_EFLASH;
676 goto done;
677 }
678
679 rc = 0;
680
681done:
682 flash_area_close(fap);
683 return rc;
684}
685
686/**
687 * Copies the contents of one flash region to another. You must erase the
688 * destination region prior to calling this function.
689 *
690 * @param flash_area_id_src The ID of the source flash area.
691 * @param flash_area_id_dst The ID of the destination flash area.
692 * @param off_src The offset within the source flash area to
693 * copy from.
694 * @param off_dst The offset within the destination flash area to
695 * copy to.
696 * @param sz The number of bytes to copy.
697 *
698 * @return 0 on success; nonzero on failure.
699 */
700static int
701boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
702 uint32_t off_src, uint32_t off_dst, uint32_t sz)
703{
704 const struct flash_area *fap_src;
705 const struct flash_area *fap_dst;
706 uint32_t bytes_copied;
707 int chunk_sz;
708 int rc;
709
710 static uint8_t buf[1024];
711
712 fap_src = NULL;
713 fap_dst = NULL;
714
715 rc = flash_area_open(flash_area_id_src, &fap_src);
716 if (rc != 0) {
717 rc = BOOT_EFLASH;
718 goto done;
719 }
720
721 rc = flash_area_open(flash_area_id_dst, &fap_dst);
722 if (rc != 0) {
723 rc = BOOT_EFLASH;
724 goto done;
725 }
726
727 bytes_copied = 0;
728 while (bytes_copied < sz) {
729 if (sz - bytes_copied > sizeof buf) {
730 chunk_sz = sizeof buf;
731 } else {
732 chunk_sz = sz - bytes_copied;
733 }
734
735 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
736 if (rc != 0) {
737 rc = BOOT_EFLASH;
738 goto done;
739 }
740
741 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
742 if (rc != 0) {
743 rc = BOOT_EFLASH;
744 goto done;
745 }
746
747 bytes_copied += chunk_sz;
748 }
749
750 rc = 0;
751
752done:
Fabio Utzige7686262017-06-28 09:26:54 -0300753 if (fap_src) {
754 flash_area_close(fap_src);
755 }
756 if (fap_dst) {
757 flash_area_close(fap_dst);
758 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800759 return rc;
760}
761
Fabio Utzig2473ac02017-05-02 12:45:02 -0300762static inline int
763boot_status_init_by_id(int flash_area_id)
764{
765 const struct flash_area *fap;
766 struct boot_swap_state swap_state;
767 int rc;
768
769 rc = flash_area_open(flash_area_id, &fap);
770 assert(rc == 0);
771
772 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
773 assert(rc == 0);
774
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400775 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300776 rc = boot_write_image_ok(fap);
777 assert(rc == 0);
778 }
779
780 rc = boot_write_magic(fap);
781 assert(rc == 0);
782
783 flash_area_close(fap);
784
785 return 0;
786}
787
Fabio Utzig358c9352017-07-25 22:10:45 -0300788#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300789static int
790boot_erase_last_sector_by_id(int flash_area_id)
791{
792 uint8_t slot;
793 uint32_t last_sector;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300794 int rc;
795
796 switch (flash_area_id) {
797 case FLASH_AREA_IMAGE_0:
798 slot = 0;
799 break;
800 case FLASH_AREA_IMAGE_1:
801 slot = 1;
802 break;
803 default:
804 return BOOT_EFLASH;
805 }
806
Marti Bolivard3269fd2017-06-12 16:31:12 -0400807 last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300808 rc = boot_erase_sector(flash_area_id,
Marti Bolivarea088872017-06-12 17:10:49 -0400809 boot_img_sector_off(&boot_data, slot, last_sector),
Marti Bolivard3269fd2017-06-12 16:31:12 -0400810 boot_img_sector_size(&boot_data, slot, last_sector));
Fabio Utzig2473ac02017-05-02 12:45:02 -0300811 assert(rc == 0);
812
813 return rc;
814}
Fabio Utzig358c9352017-07-25 22:10:45 -0300815#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -0300816
Christopher Collins92ea77f2016-12-12 15:59:26 -0800817/**
818 * Swaps the contents of two flash regions within the two image slots.
819 *
820 * @param idx The index of the first sector in the range of
821 * sectors being swapped.
822 * @param sz The number of bytes to swap.
823 * @param bs The current boot status. This struct gets
824 * updated according to the outcome.
825 *
826 * @return 0 on success; nonzero on failure.
827 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300828#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800829static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800830boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
831{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300832 const struct flash_area *fap;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800833 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300834 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800835 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300836 uint32_t scratch_trailer_off;
837 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400838 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800839 int rc;
840
841 /* Calculate offset from start of image area. */
Marti Bolivarea088872017-06-12 17:10:49 -0400842 img_off = boot_img_sector_off(&boot_data, 0, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800843
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300844 copy_sz = sz;
Marti Bolivare10a7392017-06-14 16:20:07 -0400845 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -0400846
847 /* sz in this function is always is always sized on a multiple of the
Marti Bolivarea088872017-06-12 17:10:49 -0400848 * sector size. The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -0400849 * is to determine if we're swapping the last sector. The last sector
850 * needs special handling because it's where the trailer lives. If we're
851 * copying it, we need to use scratch to write the trailer temporarily.
852 *
853 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
854 * controls if special handling is needed (swapping last sector).
855 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400856 last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
Marti Bolivarea088872017-06-12 17:10:49 -0400857 if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300858 copy_sz -= trailer_sz;
859 }
860
Fabio Utzig2473ac02017-05-02 12:45:02 -0300861 bs->use_scratch = (bs->idx == 0 && copy_sz != sz);
862
Christopher Collins92ea77f2016-12-12 15:59:26 -0800863 if (bs->state == 0) {
864 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800865 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800866
867 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300868 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800869 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800870
Fabio Utzig2473ac02017-05-02 12:45:02 -0300871 if (bs->idx == 0) {
872 if (bs->use_scratch) {
873 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH);
874 } else {
875 /* Prepare the status area... here it is known that the
876 * last sector is not being used by the image data so it's
877 * safe to erase.
878 */
879 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300880 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300881
882 boot_status_init_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300883 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300884 }
885
Christopher Collins92ea77f2016-12-12 15:59:26 -0800886 bs->state = 1;
Christopher Collins4772ac42017-02-27 20:08:01 -0800887 rc = boot_write_status(bs);
888 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800889 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300890
Christopher Collins92ea77f2016-12-12 15:59:26 -0800891 if (bs->state == 1) {
892 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800893 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800894
Christopher Collins92ea77f2016-12-12 15:59:26 -0800895 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
896 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800897 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800898
Fabio Utzig2473ac02017-05-02 12:45:02 -0300899 if (bs->idx == 0 && !bs->use_scratch) {
900 /* If not all sectors of the slot are being swapped,
901 * guarantee here that only slot0 will have the state.
902 */
903 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1);
904 assert(rc == 0);
905 }
906
Christopher Collins92ea77f2016-12-12 15:59:26 -0800907 bs->state = 2;
Christopher Collins4772ac42017-02-27 20:08:01 -0800908 rc = boot_write_status(bs);
909 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800910 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300911
Christopher Collins92ea77f2016-12-12 15:59:26 -0800912 if (bs->state == 2) {
913 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800914 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800915
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300916 /* NOTE: also copy trailer from scratch (has status info) */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800917 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300918 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800919 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800920
Fabio Utzig94d998c2017-05-22 11:02:41 -0400921 if (bs->use_scratch) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300922 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
923 assert(rc == 0);
924
925 scratch_trailer_off = boot_status_off(fap);
926
927 flash_area_close(fap);
928
929 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
930 assert(rc == 0);
931
932 /* copy current status that is being maintained in scratch */
933 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Marti Bolivare10a7392017-06-14 16:20:07 -0400934 scratch_trailer_off,
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300935 img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -0400936 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300937 assert(rc == 0);
938
Fabio Utzig2473ac02017-05-02 12:45:02 -0300939 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
940 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300941 assert(rc == 0);
942
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400943 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300944 rc = boot_write_image_ok(fap);
945 assert(rc == 0);
946 }
947
948 rc = boot_write_magic(fap);
949 assert(rc == 0);
950
951 flash_area_close(fap);
952 }
953
Christopher Collins92ea77f2016-12-12 15:59:26 -0800954 bs->idx++;
955 bs->state = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300956 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -0800957 rc = boot_write_status(bs);
958 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800959 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800960}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300961#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800962
963/**
964 * Swaps the two images in flash. If a prior copy operation was interrupted
965 * by a system reset, this function completes that operation.
966 *
967 * @param bs The current boot status. This function reads
968 * this struct to determine if it is resuming
969 * an interrupted swap operation. This
970 * function writes the updated status to this
971 * function on return.
972 *
973 * @return 0 on success; nonzero on failure.
974 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300975#ifdef MCUBOOT_OVERWRITE_ONLY
David Brown17609d82017-05-05 09:41:34 -0600976static int
977boot_copy_image(struct boot_status *bs)
978{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400979 size_t sect_count;
980 size_t sect;
David Brown17609d82017-05-05 09:41:34 -0600981 int rc;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400982 size_t size = 0;
983 size_t this_size;
David Brown17609d82017-05-05 09:41:34 -0600984
985 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
986 BOOT_LOG_INF("Erasing slot0");
987
Marti Bolivard3269fd2017-06-12 16:31:12 -0400988 sect_count = boot_img_num_sectors(&boot_data, 0);
David Brown17609d82017-05-05 09:41:34 -0600989 for (sect = 0; sect < sect_count; sect++) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400990 this_size = boot_img_sector_size(&boot_data, 0, sect);
David Brown17609d82017-05-05 09:41:34 -0600991 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
992 size,
993 this_size);
994 assert(rc == 0);
995
996 size += this_size;
997 }
998
999 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%x bytes",
1000 size);
1001 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
1002 0, 0, size);
1003
1004 /* Erase slot 1 so that we don't do the upgrade on every boot.
1005 * TODO: Perhaps verify slot 0's signature again? */
1006 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
Marti Bolivard3269fd2017-06-12 16:31:12 -04001007 0, boot_img_sector_size(&boot_data, 1, 0));
David Brown17609d82017-05-05 09:41:34 -06001008 assert(rc == 0);
1009
1010 return 0;
1011}
1012#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001013static int
1014boot_copy_image(struct boot_status *bs)
1015{
1016 uint32_t sz;
1017 int first_sector_idx;
1018 int last_sector_idx;
1019 int swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001020 struct image_header *hdr;
1021 uint32_t size;
1022 uint32_t copy_size;
1023 struct image_header tmp_hdr;
1024 int rc;
1025
1026 /* FIXME: just do this if asked by user? */
1027
1028 size = copy_size = 0;
1029
Marti Bolivarf804f622017-06-12 15:41:48 -04001030 hdr = boot_img_hdr(&boot_data, 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001031 if (hdr->ih_magic == IMAGE_MAGIC) {
1032 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1033 }
1034
Marti Bolivarf804f622017-06-12 15:41:48 -04001035 hdr = boot_img_hdr(&boot_data, 1);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001036 if (hdr->ih_magic == IMAGE_MAGIC) {
1037 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1038 }
1039
1040 if (!size || !copy_size || size == copy_size) {
Fabio Utzigc08ed212017-06-20 19:28:36 -03001041 rc = boot_read_header_from_scratch(&tmp_hdr);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001042 assert(rc == 0);
1043
1044 hdr = &tmp_hdr;
1045 if (hdr->ih_magic == IMAGE_MAGIC) {
1046 if (!size) {
1047 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1048 } else {
1049 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1050 }
1051 }
1052 }
1053
1054 if (size > copy_size) {
1055 copy_size = size;
1056 }
1057
1058 size = 0;
1059 last_sector_idx = 0;
1060 while (1) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001061 size += boot_img_sector_size(&boot_data, 0, last_sector_idx);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001062 if (size >= copy_size) {
1063 break;
1064 }
1065 last_sector_idx++;
1066 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001067
1068 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001069 while (last_sector_idx >= 0) {
1070 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
1071 if (swap_idx >= bs->idx) {
1072 boot_swap_sectors(first_sector_idx, sz, bs);
1073 }
1074
1075 last_sector_idx = first_sector_idx - 1;
1076 swap_idx++;
1077 }
1078
1079 return 0;
1080}
David Brown17609d82017-05-05 09:41:34 -06001081#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001082
1083/**
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001084 * Marks the image in slot 0 as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001085 */
1086static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001087boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001088{
1089 const struct flash_area *fap;
1090 int rc;
1091
1092 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1093 if (rc != 0) {
1094 return BOOT_EFLASH;
1095 }
1096
1097 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001098 flash_area_close(fap);
1099 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001100}
1101
1102/**
1103 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1104 * the status bytes from the image revert operation don't get processed on a
1105 * subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001106 *
1107 * NOTE: image_ok is tested before writing because if there's a valid permanent
1108 * image installed on slot0 and the new image to be upgrade to has a bad sig,
1109 * image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001110 */
1111static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001112boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001113{
1114 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001115 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001116 int rc;
1117
1118 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1119 if (rc != 0) {
1120 return BOOT_EFLASH;
1121 }
1122
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001123 rc = boot_read_swap_state(fap, &state);
1124 if (rc != 0) {
1125 rc = BOOT_EFLASH;
1126 goto out;
1127 }
1128
1129 if (state.image_ok == BOOT_FLAG_UNSET) {
1130 rc = boot_write_image_ok(fap);
1131 }
1132
1133out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001134 flash_area_close(fap);
1135 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001136}
1137
1138/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001139 * Performs an image swap if one is required.
1140 *
1141 * @param out_swap_type On success, the type of swap performed gets
1142 * written here.
1143 *
1144 * @return 0 on success; nonzero on failure.
1145 */
1146static int
1147boot_swap_if_needed(int *out_swap_type)
1148{
1149 struct boot_status bs;
1150 int swap_type;
1151 int rc;
1152
1153 /* Determine if we rebooted in the middle of an image swap
1154 * operation.
1155 */
1156 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001157 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001158 if (rc != 0) {
1159 return rc;
1160 }
1161
1162 /* If a partial swap was detected, complete it. */
1163 if (bs.idx != 0 || bs.state != 0) {
1164 rc = boot_copy_image(&bs);
1165 assert(rc == 0);
1166
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001167 /* NOTE: here we have finished a swap resume. The initial request
1168 * was either a TEST or PERM swap, which now after the completed
1169 * swap will be determined to be respectively REVERT (was TEST)
1170 * or NONE (was PERM).
1171 */
1172
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001173 /* Extrapolate the type of the partial swap. We need this
1174 * information to know how to mark the swap complete in flash.
1175 */
1176 swap_type = boot_previous_swap_type();
1177 } else {
1178 swap_type = boot_validated_swap_type();
1179 switch (swap_type) {
1180 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001181 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001182 case BOOT_SWAP_TYPE_REVERT:
1183 rc = boot_copy_image(&bs);
1184 assert(rc == 0);
1185 break;
1186 }
1187 }
1188
1189 *out_swap_type = swap_type;
1190 return 0;
1191}
1192
1193/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001194 * Prepares the booting process. This function moves images around in flash as
1195 * appropriate, and tells you what address to boot from.
1196 *
1197 * @param rsp On success, indicates how booting should occur.
1198 *
1199 * @return 0 on success; nonzero on failure.
1200 */
1201int
1202boot_go(struct boot_rsp *rsp)
1203{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001204 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001205 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001206 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001207 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001208 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001209
1210 /* The array of slot sectors are defined here (as opposed to file scope) so
1211 * that they don't get allocated for non-boot-loader apps. This is
1212 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001213 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001214 */
Marti Bolivarc50926f2017-06-14 09:35:40 -04001215 static boot_sector_t slot0_sectors[BOOT_MAX_IMG_SECTORS];
1216 static boot_sector_t slot1_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001217 boot_data.imgs[0].sectors = slot0_sectors;
1218 boot_data.imgs[1].sectors = slot1_sectors;
1219
Marti Bolivarc0b47912017-06-13 17:18:09 -04001220 /* Open boot_data image areas for the duration of this call. */
1221 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1222 fa_id = flash_area_id_from_image_slot(slot);
1223 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1224 assert(rc == 0);
1225 }
1226 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1227 &BOOT_SCRATCH_AREA(&boot_data));
1228 assert(rc == 0);
1229
Christopher Collins92ea77f2016-12-12 15:59:26 -08001230 /* Determine the sector layout of the image slots and scratch area. */
1231 rc = boot_read_sectors();
1232 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001233 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001234 }
1235
1236 /* Attempt to read an image header from each slot. */
1237 rc = boot_read_image_headers();
1238 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001239 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001240 }
1241
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001242 /* If the image slots aren't compatible, no swap is possible. Just boot
1243 * into slot 0.
1244 */
1245 if (boot_slots_compatible()) {
1246 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001247 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001248 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001249 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001250 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001251
1252 /*
1253 * The following states need image_ok be explicitly set after the
1254 * swap was finished to avoid a new revert.
1255 */
1256 if (swap_type == BOOT_SWAP_TYPE_REVERT || swap_type == BOOT_SWAP_TYPE_FAIL) {
Fabio Utzig695d5642017-07-20 09:47:16 -03001257 rc = boot_set_image_ok();
1258 if (rc != 0) {
1259 swap_type = BOOT_SWAP_TYPE_PANIC;
1260 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001261 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001262 } else {
1263 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001264 }
1265
1266 switch (swap_type) {
1267 case BOOT_SWAP_TYPE_NONE:
1268 slot = 0;
1269 break;
1270
Fabio Utzig695d5642017-07-20 09:47:16 -03001271 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1272 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001273 case BOOT_SWAP_TYPE_REVERT:
1274 slot = 1;
David Brown52eee562017-07-05 11:25:09 -06001275 reload_headers = true;
Fabio Utzig695d5642017-07-20 09:47:16 -03001276 rc = boot_set_copy_done();
1277 if (rc != 0) {
1278 swap_type = BOOT_SWAP_TYPE_PANIC;
1279 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001280 break;
1281
1282 case BOOT_SWAP_TYPE_FAIL:
1283 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1284 * try to boot into it again on the next reboot. Do this by pretending
1285 * we just reverted back to slot 0.
1286 */
1287 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001288 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001289 break;
1290
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001291 default:
Fabio Utzig695d5642017-07-20 09:47:16 -03001292 swap_type = BOOT_SWAP_TYPE_PANIC;
1293 }
1294
1295 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1296 BOOT_LOG_ERR("panic!");
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001297 assert(0);
Fabio Utzig695d5642017-07-20 09:47:16 -03001298
1299 /* Loop forever... */
1300 while (1) {}
Christopher Collins92ea77f2016-12-12 15:59:26 -08001301 }
1302
David Brown554c52e2017-06-30 16:01:07 -06001303#ifdef MCUBOOT_VALIDATE_SLOT0
David Brown52eee562017-07-05 11:25:09 -06001304 if (reload_headers) {
1305 rc = boot_read_image_headers();
1306 if (rc != 0) {
1307 goto out;
1308 }
1309 }
1310
David Brown554c52e2017-06-30 16:01:07 -06001311 rc = boot_validate_slot(0);
1312 assert(rc == 0);
1313 if (rc != 0) {
1314 rc = BOOT_EBADIMAGE;
1315 goto out;
1316 }
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001317#else
1318 (void)reload_headers;
David Brown554c52e2017-06-30 16:01:07 -06001319#endif
1320
Christopher Collins92ea77f2016-12-12 15:59:26 -08001321 /* Always boot from the primary slot. */
Marti Bolivar428cdbf2017-05-01 22:32:42 -04001322 rsp->br_flash_dev_id = boot_img_fa_device_id(&boot_data, 0);
Marti Bolivar88f48d92017-05-01 22:30:02 -04001323 rsp->br_image_off = boot_img_slot_off(&boot_data, 0);
Marti Bolivarf804f622017-06-12 15:41:48 -04001324 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001325
Marti Bolivarc0b47912017-06-13 17:18:09 -04001326 out:
1327 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1328 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1329 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1330 }
1331 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001332}
1333
1334int
1335split_go(int loader_slot, int split_slot, void **entry)
1336{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001337 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001338 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001339 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001340 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001341 int rc;
1342
Christopher Collins92ea77f2016-12-12 15:59:26 -08001343 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1344 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001345 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001346 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001347 boot_data.imgs[loader_slot].sectors = sectors + 0;
1348 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1349
1350 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1351 rc = flash_area_open(loader_flash_id,
1352 &BOOT_IMG_AREA(&boot_data, split_slot));
1353 assert(rc == 0);
1354 split_flash_id = flash_area_id_from_image_slot(split_slot);
1355 rc = flash_area_open(split_flash_id,
1356 &BOOT_IMG_AREA(&boot_data, split_slot));
1357 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001358
1359 /* Determine the sector layout of the image slots and scratch area. */
1360 rc = boot_read_sectors();
1361 if (rc != 0) {
1362 rc = SPLIT_GO_ERR;
1363 goto done;
1364 }
1365
1366 rc = boot_read_image_headers();
1367 if (rc != 0) {
1368 goto done;
1369 }
1370
Christopher Collins92ea77f2016-12-12 15:59:26 -08001371 /* Don't check the bootable image flag because we could really call a
1372 * bootable or non-bootable image. Just validate that the image check
1373 * passes which is distinct from the normal check.
1374 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001375 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001376 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001377 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001378 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001379 if (rc != 0) {
1380 rc = SPLIT_GO_NON_MATCHING;
1381 goto done;
1382 }
1383
Marti Bolivarea088872017-06-12 17:10:49 -04001384 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001385 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001386 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001387 rc = SPLIT_GO_OK;
1388
1389done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001390 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1391 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001392 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001393 return rc;
1394}