blob: 782d860cd2d15382bb051551c9deb08bce17f2a6 [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>
27#include <inttypes.h>
28#include <stdlib.h>
29#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080030#include <hal/hal_flash.h>
31#include <os/os_malloc.h>
32#include "bootutil/bootutil.h"
33#include "bootutil/image.h"
34#include "bootutil_priv.h"
35
Marti Bolivarfd20c762017-02-07 16:52:50 -050036#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
37#include "bootutil/bootutil_log.h"
38
Fabio Utzigeed80b62017-06-10 08:03:05 -030039#ifdef APP_mynewt
40#include "mynewt/config.h"
41#endif
42
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040043static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080044
45struct boot_status_table {
46 /**
47 * For each field, a value of 0 means "any".
48 */
49 uint8_t bst_magic_slot0;
50 uint8_t bst_magic_scratch;
51 uint8_t bst_copy_done_slot0;
52 uint8_t bst_status_source;
53};
54
55/**
56 * This set of tables maps swap state contents to boot status location.
57 * When searching for a match, these tables must be iterated in order.
58 */
59static const struct boot_status_table boot_status_tables[] = {
60 {
61 /* | slot-0 | scratch |
62 * ----------+------------+------------|
63 * magic | Good | Any |
64 * copy-done | 0x01 | N/A |
65 * ----------+------------+------------'
66 * source: none |
67 * ------------------------------------'
68 */
69 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
70 .bst_magic_scratch = 0,
71 .bst_copy_done_slot0 = 0x01,
72 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
73 },
74
75 {
76 /* | slot-0 | scratch |
77 * ----------+------------+------------|
78 * magic | Good | Any |
79 * copy-done | 0xff | N/A |
80 * ----------+------------+------------'
81 * source: slot 0 |
82 * ------------------------------------'
83 */
84 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
85 .bst_magic_scratch = 0,
86 .bst_copy_done_slot0 = 0xff,
87 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
88 },
89
90 {
91 /* | slot-0 | scratch |
92 * ----------+------------+------------|
93 * magic | Any | Good |
94 * copy-done | Any | N/A |
95 * ----------+------------+------------'
96 * source: scratch |
97 * ------------------------------------'
98 */
99 .bst_magic_slot0 = 0,
100 .bst_magic_scratch = BOOT_MAGIC_GOOD,
101 .bst_copy_done_slot0 = 0,
102 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
103 },
104
105 {
106 /* | slot-0 | scratch |
107 * ----------+------------+------------|
108 * magic | Unset | Any |
109 * copy-done | 0xff | N/A |
110 * ----------+------------+------------|
111 * source: varies |
112 * ------------------------------------+------------------------------+
113 * This represents one of two cases: |
114 * o No swaps ever (no status to read, so no harm in checking). |
115 * o Mid-revert; status in slot 0. |
116 * -------------------------------------------------------------------'
117 */
118 .bst_magic_slot0 = BOOT_MAGIC_UNSET,
119 .bst_magic_scratch = 0,
120 .bst_copy_done_slot0 = 0xff,
121 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
122 },
123};
124
125#define BOOT_STATUS_TABLES_COUNT \
126 (sizeof boot_status_tables / sizeof boot_status_tables[0])
127
128/**
129 * This table indicates the next swap type that should be performed. The first
130 * column contains the current swap type. The second column contains the swap
131 * type that should be effected after the first completes.
132 */
133static const uint8_t boot_swap_trans_table[][2] = {
134 /* From To */
135 { BOOT_SWAP_TYPE_REVERT, BOOT_SWAP_TYPE_NONE },
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800136 { BOOT_SWAP_TYPE_PERM, BOOT_SWAP_TYPE_NONE },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800137 { BOOT_SWAP_TYPE_TEST, BOOT_SWAP_TYPE_REVERT },
138};
139
140#define BOOT_SWAP_TRANS_TABLE_SIZE \
141 (sizeof boot_swap_trans_table / sizeof boot_swap_trans_table[0])
142
Marti Bolivarfd20c762017-02-07 16:52:50 -0500143#define BOOT_LOG_SWAP_STATE(area, state) \
144 BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \
145 (area), \
146 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
147 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
148 "bad"), \
149 (state)->copy_done, \
150 (state)->image_ok)
151
Christopher Collins92ea77f2016-12-12 15:59:26 -0800152/**
153 * Determines where in flash the most recent boot status is stored. The boot
154 * status is necessary for completing a swap that was interrupted by a boot
155 * loader reset.
156 *
157 * @return A BOOT_STATUS_SOURCE_[...] code indicating where * status should be read from.
158 */
159static int
160boot_status_source(void)
161{
162 const struct boot_status_table *table;
163 struct boot_swap_state state_scratch;
164 struct boot_swap_state state_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800165 int rc;
166 int i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500167 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800168
Fabio Utzig2473ac02017-05-02 12:45:02 -0300169 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800170 assert(rc == 0);
171
Fabio Utzig2473ac02017-05-02 12:45:02 -0300172 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800173 assert(rc == 0);
174
Marti Bolivarfd20c762017-02-07 16:52:50 -0500175 BOOT_LOG_SWAP_STATE("Image 0", &state_slot0);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500176 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
177
Christopher Collins92ea77f2016-12-12 15:59:26 -0800178 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300179 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800180
181 if ((table->bst_magic_slot0 == 0 ||
182 table->bst_magic_slot0 == state_slot0.magic) &&
183 (table->bst_magic_scratch == 0 ||
184 table->bst_magic_scratch == state_scratch.magic) &&
185 (table->bst_copy_done_slot0 == 0 ||
186 table->bst_copy_done_slot0 == state_slot0.copy_done)) {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500187 source = table->bst_status_source;
188 BOOT_LOG_INF("Boot source: %s",
189 source == BOOT_STATUS_SOURCE_NONE ? "none" :
190 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
191 source == BOOT_STATUS_SOURCE_SLOT0 ? "slot 0" :
192 "BUG; can't happen");
193 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800194 }
195 }
196
Marti Bolivarfd20c762017-02-07 16:52:50 -0500197 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800198 return BOOT_STATUS_SOURCE_NONE;
199}
200
201/**
202 * Calculates the type of swap that just completed.
203 */
204static int
205boot_previous_swap_type(void)
206{
207 int post_swap_type;
208 int i;
209
210 post_swap_type = boot_swap_type();
211
212 for (i = 0; i < BOOT_SWAP_TRANS_TABLE_SIZE; i++){
213 if (boot_swap_trans_table[i][1] == post_swap_type) {
214 return boot_swap_trans_table[i][0];
215 }
216 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300217
Christopher Collins92ea77f2016-12-12 15:59:26 -0800218 /* XXX: Temporary assert. */
219 assert(0);
220
221 return BOOT_SWAP_TYPE_REVERT;
222}
223
224static int
Fabio Utzigc08ed212017-06-20 19:28:36 -0300225boot_read_header_from_scratch(struct image_header *out_hdr)
226{
227 const struct flash_area *fap;
228 int rc;
229
230 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
231 if (rc != 0) {
232 return BOOT_EFLASH;
233 }
234
235 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
236 if (rc != 0) {
237 rc = BOOT_EFLASH;
238 }
239
240 flash_area_close(fap);
241 return rc;
242}
243
244static int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800245boot_read_image_header(int slot, struct image_header *out_hdr)
246{
247 const struct flash_area *fap;
248 int area_id;
249 int rc;
250
251 area_id = flash_area_id_from_image_slot(slot);
252 rc = flash_area_open(area_id, &fap);
253 if (rc != 0) {
254 rc = BOOT_EFLASH;
255 goto done;
256 }
257
258 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
259 if (rc != 0) {
260 rc = BOOT_EFLASH;
261 goto done;
262 }
263
264 rc = 0;
265
266done:
267 flash_area_close(fap);
268 return rc;
269}
270
271static int
272boot_read_image_headers(void)
273{
274 int rc;
275 int i;
276
277 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400278 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800279 if (rc != 0) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300280 /* If at least the first slot's header was read successfully, then
281 * the boot loader can attempt a boot. Failure to read any headers
282 * is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800283 */
284 if (i > 0) {
285 return 0;
286 } else {
287 return rc;
288 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800289 }
290 }
291
292 return 0;
293}
294
295static uint8_t
296boot_write_sz(void)
297{
298 uint8_t elem_sz;
299 uint8_t align;
300
301 /* Figure out what size to write update status update as. The size depends
302 * on what the minimum write size is for scratch area, active image slot.
303 * We need to use the bigger of those 2 values.
304 */
Marti Bolivare2587152017-06-12 15:52:05 -0400305 elem_sz = hal_flash_align(boot_img_fa_device_id(&boot_data, 0));
306 align = hal_flash_align(boot_scratch_fa_device_id(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800307 if (align > elem_sz) {
308 elem_sz = align;
309 }
310
311 return elem_sz;
312}
313
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800314static int
315boot_slots_compatible(void)
316{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400317 size_t num_sectors_0 = boot_img_num_sectors(&boot_data, 0);
318 size_t num_sectors_1 = boot_img_num_sectors(&boot_data, 1);
319 size_t size_0, size_1;
320 size_t i;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800321
322 /* Ensure both image slots have identical sector layouts. */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400323 if (num_sectors_0 != num_sectors_1) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800324 return 0;
325 }
Marti Bolivard3269fd2017-06-12 16:31:12 -0400326 for (i = 0; i < num_sectors_0; i++) {
327 size_0 = boot_img_sector_size(&boot_data, 0, i);
328 size_1 = boot_img_sector_size(&boot_data, 1, i);
329 if (size_0 != size_1) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800330 return 0;
331 }
332 }
333
334 return 1;
335}
336
Christopher Collins92ea77f2016-12-12 15:59:26 -0800337/**
338 * Determines the sector layout of both image slots and the scratch area.
339 * This information is necessary for calculating the number of bytes to erase
340 * and copy during an image swap. The information collected during this
341 * function is used to populate the boot_data global.
342 */
343static int
344boot_read_sectors(void)
345{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800346 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800347
Marti Bolivarcca28a92017-06-12 16:52:22 -0400348 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800349 if (rc != 0) {
350 return BOOT_EFLASH;
351 }
352
Marti Bolivarcca28a92017-06-12 16:52:22 -0400353 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800354 if (rc != 0) {
355 return BOOT_EFLASH;
356 }
357
Marti Bolivare10a7392017-06-14 16:20:07 -0400358 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800359
360 return 0;
361}
362
363static uint32_t
364boot_status_internal_off(int idx, int state, int elem_sz)
365{
366 int idx_sz;
367
368 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
369
370 return idx * idx_sz + state * elem_sz;
371}
372
373/**
374 * Reads the status of a partially-completed swap, if any. This is necessary
375 * to recover in case the boot lodaer was reset in the middle of a swap
376 * operation.
377 */
378static int
379boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
380{
381 uint32_t off;
382 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300383 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800384 int found;
385 int rc;
386 int i;
387
388 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400389 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300390
Christopher Collins92ea77f2016-12-12 15:59:26 -0800391 found = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300392 for (i = 0; i < max_entries; i++) {
Marti Bolivare10a7392017-06-14 16:20:07 -0400393 rc = flash_area_read(fap, off + i * BOOT_WRITE_SZ(&boot_data),
394 &status, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800395 if (rc != 0) {
396 return BOOT_EFLASH;
397 }
398
399 if (status == 0xff) {
400 if (found) {
401 break;
402 }
403 } else if (!found) {
404 found = 1;
405 }
406 }
407
408 if (found) {
409 i--;
Fabio Utzig94d998c2017-05-22 11:02:41 -0400410 bs->idx = i / BOOT_STATUS_STATE_COUNT;
411 bs->state = i % BOOT_STATUS_STATE_COUNT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800412 }
413
414 return 0;
415}
416
417/**
418 * Reads the boot status from the flash. The boot status contains
419 * the current state of an interrupted image copy operation. If the boot
420 * status is not present, or it indicates that previous copy finished,
421 * there is no operation in progress.
422 */
423static int
424boot_read_status(struct boot_status *bs)
425{
426 const struct flash_area *fap;
427 int status_loc;
428 int area_id;
429 int rc;
430
431 memset(bs, 0, sizeof *bs);
432
433 status_loc = boot_status_source();
434 switch (status_loc) {
435 case BOOT_STATUS_SOURCE_NONE:
436 return 0;
437
438 case BOOT_STATUS_SOURCE_SCRATCH:
439 area_id = FLASH_AREA_IMAGE_SCRATCH;
440 break;
441
442 case BOOT_STATUS_SOURCE_SLOT0:
443 area_id = FLASH_AREA_IMAGE_0;
444 break;
445
446 default:
447 assert(0);
448 return BOOT_EBADARGS;
449 }
450
451 rc = flash_area_open(area_id, &fap);
452 if (rc != 0) {
453 return BOOT_EFLASH;
454 }
455
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300456 return boot_read_status_bytes(fap, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800457}
458
459/**
460 * Writes the supplied boot status to the flash file system. The boot status
461 * contains the current state of an in-progress image copy operation.
462 *
463 * @param bs The boot status to write.
464 *
465 * @return 0 on success; nonzero on failure.
466 */
467int
468boot_write_status(struct boot_status *bs)
469{
470 const struct flash_area *fap;
471 uint32_t off;
472 int area_id;
473 int rc;
David Brown9d725462017-01-23 15:50:58 -0700474 uint8_t buf[8];
475 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800476
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300477 /* NOTE: The first sector copied (that is the last sector on slot) contains
478 * the trailer. Since in the last step SLOT 0 is erased, the first
479 * two status writes go to the scratch which will be copied to SLOT 0!
480 */
481
Fabio Utzig2473ac02017-05-02 12:45:02 -0300482 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800483 /* Write to scratch. */
484 area_id = FLASH_AREA_IMAGE_SCRATCH;
485 } else {
486 /* Write to slot 0. */
487 area_id = FLASH_AREA_IMAGE_0;
488 }
489
490 rc = flash_area_open(area_id, &fap);
491 if (rc != 0) {
492 rc = BOOT_EFLASH;
493 goto done;
494 }
495
496 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400497 boot_status_internal_off(bs->idx, bs->state,
498 BOOT_WRITE_SZ(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800499
David Brown9d725462017-01-23 15:50:58 -0700500 align = hal_flash_align(fap->fa_device_id);
David Brown9d725462017-01-23 15:50:58 -0700501 memset(buf, 0xFF, 8);
502 buf[0] = bs->state;
503
504 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800505 if (rc != 0) {
506 rc = BOOT_EFLASH;
507 goto done;
508 }
509
510 rc = 0;
511
512done:
513 flash_area_close(fap);
514 return rc;
515}
516
517/*
518 * Validate image hash/signature in a slot.
519 */
520static int
521boot_image_check(struct image_header *hdr, const struct flash_area *fap)
522{
David Browndb1d9d32017-01-06 11:07:54 -0700523 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800524
Christopher Collins92ea77f2016-12-12 15:59:26 -0800525 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
526 NULL, 0, NULL)) {
527 return BOOT_EBADIMAGE;
528 }
529 return 0;
530}
531
532static int
533split_image_check(struct image_header *app_hdr,
534 const struct flash_area *app_fap,
535 struct image_header *loader_hdr,
536 const struct flash_area *loader_fap)
537{
538 static void *tmpbuf;
539 uint8_t loader_hash[32];
540
541 if (!tmpbuf) {
542 tmpbuf = malloc(BOOT_TMPBUF_SZ);
543 if (!tmpbuf) {
544 return BOOT_ENOMEM;
545 }
546 }
547
548 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
549 NULL, 0, loader_hash)) {
550 return BOOT_EBADIMAGE;
551 }
552
553 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
554 loader_hash, 32, NULL)) {
555 return BOOT_EBADIMAGE;
556 }
557
558 return 0;
559}
560
561static int
David Brownd930ec62016-12-14 07:59:48 -0700562boot_validate_slot(int slot)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800563{
564 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400565 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800566 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300567
Marti Bolivarf804f622017-06-12 15:41:48 -0400568 hdr = boot_img_hdr(&boot_data, slot);
569 if (hdr->ih_magic == 0xffffffff || hdr->ih_flags & IMAGE_F_NON_BOOTABLE) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300570 /* No bootable image in slot; continue booting from slot 0. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800571 return -1;
572 }
573
David Brownd930ec62016-12-14 07:59:48 -0700574 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800575 if (rc != 0) {
576 return BOOT_EFLASH;
577 }
578
Marti Bolivarf804f622017-06-12 15:41:48 -0400579 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap) != 0)) {
David Brownb38e0442017-02-24 13:57:12 -0700580 if (slot != 0) {
581 flash_area_erase(fap, 0, fap->fa_size);
582 /* Image in slot 1 is invalid. Erase the image and
583 * continue booting from slot 0.
584 */
585 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800586 return -1;
587 }
588
589 flash_area_close(fap);
590
591 /* Image in slot 1 is valid. */
592 return 0;
593}
594
595/**
596 * Determines which swap operation to perform, if any. If it is determined
597 * that a swap operation is required, the image in the second slot is checked
598 * for validity. If the image in the second slot is invalid, it is erased, and
599 * a swap type of "none" is indicated.
600 *
601 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
602 */
603static int
604boot_validated_swap_type(void)
605{
606 int swap_type;
607 int rc;
608
609 swap_type = boot_swap_type();
610 if (swap_type == BOOT_SWAP_TYPE_NONE) {
611 /* Continue using slot 0. */
612 return BOOT_SWAP_TYPE_NONE;
613 }
614
615 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
David Brownd930ec62016-12-14 07:59:48 -0700616 rc = boot_validate_slot(1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800617 if (rc != 0) {
618 return BOOT_SWAP_TYPE_FAIL;
619 }
620
621 return swap_type;
622}
623
624/**
625 * Calculates the number of sectors the scratch area can contain. A "last"
626 * source sector is specified because images are copied backwards in flash
627 * (final index to index number 0).
628 *
629 * @param last_sector_idx The index of the last source sector
630 * (inclusive).
631 * @param out_first_sector_idx The index of the first source sector
632 * (inclusive) gets written here.
633 *
634 * @return The number of bytes comprised by the
635 * [first-sector, last-sector] range.
636 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300637#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800638static uint32_t
639boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
640{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400641 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800642 uint32_t new_sz;
643 uint32_t sz;
644 int i;
645
646 sz = 0;
647
Marti Bolivard3269fd2017-06-12 16:31:12 -0400648 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800649 for (i = last_sector_idx; i >= 0; i--) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400650 new_sz = sz + boot_img_sector_size(&boot_data, 0, i);
651 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800652 break;
653 }
654 sz = new_sz;
655 }
656
657 /* i currently refers to a sector that doesn't fit or it is -1 because all
658 * sectors have been processed. In both cases, exclude sector i.
659 */
660 *out_first_sector_idx = i + 1;
661 return sz;
662}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300663#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800664
665/**
666 * Erases a region of flash.
667 *
668 * @param flash_area_idx The ID of the flash area containing the region
669 * to erase.
670 * @param off The offset within the flash area to start the
671 * erase.
672 * @param sz The number of bytes to erase.
673 *
674 * @return 0 on success; nonzero on failure.
675 */
676static int
677boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
678{
679 const struct flash_area *fap;
680 int rc;
681
682 rc = flash_area_open(flash_area_id, &fap);
683 if (rc != 0) {
684 rc = BOOT_EFLASH;
685 goto done;
686 }
687
688 rc = flash_area_erase(fap, off, sz);
689 if (rc != 0) {
690 rc = BOOT_EFLASH;
691 goto done;
692 }
693
694 rc = 0;
695
696done:
697 flash_area_close(fap);
698 return rc;
699}
700
701/**
702 * Copies the contents of one flash region to another. You must erase the
703 * destination region prior to calling this function.
704 *
705 * @param flash_area_id_src The ID of the source flash area.
706 * @param flash_area_id_dst The ID of the destination flash area.
707 * @param off_src The offset within the source flash area to
708 * copy from.
709 * @param off_dst The offset within the destination flash area to
710 * copy to.
711 * @param sz The number of bytes to copy.
712 *
713 * @return 0 on success; nonzero on failure.
714 */
715static int
716boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
717 uint32_t off_src, uint32_t off_dst, uint32_t sz)
718{
719 const struct flash_area *fap_src;
720 const struct flash_area *fap_dst;
721 uint32_t bytes_copied;
722 int chunk_sz;
723 int rc;
724
725 static uint8_t buf[1024];
726
727 fap_src = NULL;
728 fap_dst = NULL;
729
730 rc = flash_area_open(flash_area_id_src, &fap_src);
731 if (rc != 0) {
732 rc = BOOT_EFLASH;
733 goto done;
734 }
735
736 rc = flash_area_open(flash_area_id_dst, &fap_dst);
737 if (rc != 0) {
738 rc = BOOT_EFLASH;
739 goto done;
740 }
741
742 bytes_copied = 0;
743 while (bytes_copied < sz) {
744 if (sz - bytes_copied > sizeof buf) {
745 chunk_sz = sizeof buf;
746 } else {
747 chunk_sz = sz - bytes_copied;
748 }
749
750 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
751 if (rc != 0) {
752 rc = BOOT_EFLASH;
753 goto done;
754 }
755
756 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
757 if (rc != 0) {
758 rc = BOOT_EFLASH;
759 goto done;
760 }
761
762 bytes_copied += chunk_sz;
763 }
764
765 rc = 0;
766
767done:
768 flash_area_close(fap_src);
769 flash_area_close(fap_dst);
770 return rc;
771}
772
Fabio Utzig2473ac02017-05-02 12:45:02 -0300773static inline int
774boot_status_init_by_id(int flash_area_id)
775{
776 const struct flash_area *fap;
777 struct boot_swap_state swap_state;
778 int rc;
779
780 rc = flash_area_open(flash_area_id, &fap);
781 assert(rc == 0);
782
783 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
784 assert(rc == 0);
785
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400786 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300787 rc = boot_write_image_ok(fap);
788 assert(rc == 0);
789 }
790
791 rc = boot_write_magic(fap);
792 assert(rc == 0);
793
794 flash_area_close(fap);
795
796 return 0;
797}
798
799static int
800boot_erase_last_sector_by_id(int flash_area_id)
801{
802 uint8_t slot;
803 uint32_t last_sector;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300804 int rc;
805
806 switch (flash_area_id) {
807 case FLASH_AREA_IMAGE_0:
808 slot = 0;
809 break;
810 case FLASH_AREA_IMAGE_1:
811 slot = 1;
812 break;
813 default:
814 return BOOT_EFLASH;
815 }
816
Marti Bolivard3269fd2017-06-12 16:31:12 -0400817 last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300818 rc = boot_erase_sector(flash_area_id,
Marti Bolivarea088872017-06-12 17:10:49 -0400819 boot_img_sector_off(&boot_data, slot, last_sector),
Marti Bolivard3269fd2017-06-12 16:31:12 -0400820 boot_img_sector_size(&boot_data, slot, last_sector));
Fabio Utzig2473ac02017-05-02 12:45:02 -0300821 assert(rc == 0);
822
823 return rc;
824}
825
Christopher Collins92ea77f2016-12-12 15:59:26 -0800826/**
827 * Swaps the contents of two flash regions within the two image slots.
828 *
829 * @param idx The index of the first sector in the range of
830 * sectors being swapped.
831 * @param sz The number of bytes to swap.
832 * @param bs The current boot status. This struct gets
833 * updated according to the outcome.
834 *
835 * @return 0 on success; nonzero on failure.
836 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300837#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800838static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800839boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
840{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300841 const struct flash_area *fap;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800842 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300843 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800844 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300845 uint32_t scratch_trailer_off;
846 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400847 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800848 int rc;
849
850 /* Calculate offset from start of image area. */
Marti Bolivarea088872017-06-12 17:10:49 -0400851 img_off = boot_img_sector_off(&boot_data, 0, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800852
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300853 copy_sz = sz;
Marti Bolivare10a7392017-06-14 16:20:07 -0400854 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -0400855
856 /* sz in this function is always is always sized on a multiple of the
Marti Bolivarea088872017-06-12 17:10:49 -0400857 * sector size. The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -0400858 * is to determine if we're swapping the last sector. The last sector
859 * needs special handling because it's where the trailer lives. If we're
860 * copying it, we need to use scratch to write the trailer temporarily.
861 *
862 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
863 * controls if special handling is needed (swapping last sector).
864 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400865 last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
Marti Bolivarea088872017-06-12 17:10:49 -0400866 if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300867 copy_sz -= trailer_sz;
868 }
869
Fabio Utzig2473ac02017-05-02 12:45:02 -0300870 bs->use_scratch = (bs->idx == 0 && copy_sz != sz);
871
Christopher Collins92ea77f2016-12-12 15:59:26 -0800872 if (bs->state == 0) {
873 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800874 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800875
876 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300877 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800878 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800879
Fabio Utzig2473ac02017-05-02 12:45:02 -0300880 if (bs->idx == 0) {
881 if (bs->use_scratch) {
882 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH);
883 } else {
884 /* Prepare the status area... here it is known that the
885 * last sector is not being used by the image data so it's
886 * safe to erase.
887 */
888 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300889 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300890
891 boot_status_init_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300892 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300893 }
894
Christopher Collins92ea77f2016-12-12 15:59:26 -0800895 bs->state = 1;
Christopher Collins4772ac42017-02-27 20:08:01 -0800896 rc = boot_write_status(bs);
897 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800898 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300899
Christopher Collins92ea77f2016-12-12 15:59:26 -0800900 if (bs->state == 1) {
901 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800902 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800903
Christopher Collins92ea77f2016-12-12 15:59:26 -0800904 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
905 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800906 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800907
Fabio Utzig2473ac02017-05-02 12:45:02 -0300908 if (bs->idx == 0 && !bs->use_scratch) {
909 /* If not all sectors of the slot are being swapped,
910 * guarantee here that only slot0 will have the state.
911 */
912 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1);
913 assert(rc == 0);
914 }
915
Christopher Collins92ea77f2016-12-12 15:59:26 -0800916 bs->state = 2;
Christopher Collins4772ac42017-02-27 20:08:01 -0800917 rc = boot_write_status(bs);
918 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800919 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300920
Christopher Collins92ea77f2016-12-12 15:59:26 -0800921 if (bs->state == 2) {
922 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800923 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800924
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300925 /* NOTE: also copy trailer from scratch (has status info) */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800926 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300927 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800928 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800929
Fabio Utzig94d998c2017-05-22 11:02:41 -0400930 if (bs->use_scratch) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300931 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
932 assert(rc == 0);
933
934 scratch_trailer_off = boot_status_off(fap);
935
936 flash_area_close(fap);
937
938 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
939 assert(rc == 0);
940
941 /* copy current status that is being maintained in scratch */
942 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Marti Bolivare10a7392017-06-14 16:20:07 -0400943 scratch_trailer_off,
944 img_off + copy_sz + BOOT_MAGIC_SZ,
945 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300946 assert(rc == 0);
947
Fabio Utzig2473ac02017-05-02 12:45:02 -0300948 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
949 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300950 assert(rc == 0);
951
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400952 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300953 rc = boot_write_image_ok(fap);
954 assert(rc == 0);
955 }
956
957 rc = boot_write_magic(fap);
958 assert(rc == 0);
959
960 flash_area_close(fap);
961 }
962
Christopher Collins92ea77f2016-12-12 15:59:26 -0800963 bs->idx++;
964 bs->state = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300965 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -0800966 rc = boot_write_status(bs);
967 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800968 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800969}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300970#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800971
972/**
973 * Swaps the two images in flash. If a prior copy operation was interrupted
974 * by a system reset, this function completes that operation.
975 *
976 * @param bs The current boot status. This function reads
977 * this struct to determine if it is resuming
978 * an interrupted swap operation. This
979 * function writes the updated status to this
980 * function on return.
981 *
982 * @return 0 on success; nonzero on failure.
983 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300984#ifdef MCUBOOT_OVERWRITE_ONLY
David Brown17609d82017-05-05 09:41:34 -0600985static int
986boot_copy_image(struct boot_status *bs)
987{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400988 size_t sect_count;
989 size_t sect;
David Brown17609d82017-05-05 09:41:34 -0600990 int rc;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400991 size_t size = 0;
992 size_t this_size;
David Brown17609d82017-05-05 09:41:34 -0600993
994 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
995 BOOT_LOG_INF("Erasing slot0");
996
Marti Bolivard3269fd2017-06-12 16:31:12 -0400997 sect_count = boot_img_num_sectors(&boot_data, 0);
David Brown17609d82017-05-05 09:41:34 -0600998 for (sect = 0; sect < sect_count; sect++) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400999 this_size = boot_img_sector_size(&boot_data, 0, sect);
David Brown17609d82017-05-05 09:41:34 -06001000 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
1001 size,
1002 this_size);
1003 assert(rc == 0);
1004
1005 size += this_size;
1006 }
1007
1008 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%x bytes",
1009 size);
1010 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
1011 0, 0, size);
1012
1013 /* Erase slot 1 so that we don't do the upgrade on every boot.
1014 * TODO: Perhaps verify slot 0's signature again? */
1015 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
Marti Bolivard3269fd2017-06-12 16:31:12 -04001016 0, boot_img_sector_size(&boot_data, 1, 0));
David Brown17609d82017-05-05 09:41:34 -06001017 assert(rc == 0);
1018
1019 return 0;
1020}
1021#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001022static int
1023boot_copy_image(struct boot_status *bs)
1024{
1025 uint32_t sz;
1026 int first_sector_idx;
1027 int last_sector_idx;
1028 int swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001029 struct image_header *hdr;
1030 uint32_t size;
1031 uint32_t copy_size;
1032 struct image_header tmp_hdr;
1033 int rc;
1034
1035 /* FIXME: just do this if asked by user? */
1036
1037 size = copy_size = 0;
1038
Marti Bolivarf804f622017-06-12 15:41:48 -04001039 hdr = boot_img_hdr(&boot_data, 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001040 if (hdr->ih_magic == IMAGE_MAGIC) {
1041 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1042 }
1043
Marti Bolivarf804f622017-06-12 15:41:48 -04001044 hdr = boot_img_hdr(&boot_data, 1);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001045 if (hdr->ih_magic == IMAGE_MAGIC) {
1046 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1047 }
1048
1049 if (!size || !copy_size || size == copy_size) {
Fabio Utzigc08ed212017-06-20 19:28:36 -03001050 rc = boot_read_header_from_scratch(&tmp_hdr);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001051 assert(rc == 0);
1052
1053 hdr = &tmp_hdr;
1054 if (hdr->ih_magic == IMAGE_MAGIC) {
1055 if (!size) {
1056 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1057 } else {
1058 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1059 }
1060 }
1061 }
1062
1063 if (size > copy_size) {
1064 copy_size = size;
1065 }
1066
1067 size = 0;
1068 last_sector_idx = 0;
1069 while (1) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001070 size += boot_img_sector_size(&boot_data, 0, last_sector_idx);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001071 if (size >= copy_size) {
1072 break;
1073 }
1074 last_sector_idx++;
1075 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001076
1077 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001078 while (last_sector_idx >= 0) {
1079 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
1080 if (swap_idx >= bs->idx) {
1081 boot_swap_sectors(first_sector_idx, sz, bs);
1082 }
1083
1084 last_sector_idx = first_sector_idx - 1;
1085 swap_idx++;
1086 }
1087
1088 return 0;
1089}
David Brown17609d82017-05-05 09:41:34 -06001090#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001091
1092/**
1093 * Marks a test image in slot 0 as fully copied.
1094 */
1095static int
1096boot_finalize_test_swap(void)
1097{
1098 const struct flash_area *fap;
1099 int rc;
1100
1101 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1102 if (rc != 0) {
1103 return BOOT_EFLASH;
1104 }
1105
1106 rc = boot_write_copy_done(fap);
1107 if (rc != 0) {
1108 return rc;
1109 }
1110
1111 return 0;
1112}
1113
1114/**
1115 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1116 * the status bytes from the image revert operation don't get processed on a
1117 * subsequent boot.
1118 */
1119static int
1120boot_finalize_revert_swap(void)
1121{
1122 const struct flash_area *fap;
1123 struct boot_swap_state state_slot0;
1124 int rc;
1125
1126 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1127 if (rc != 0) {
1128 return BOOT_EFLASH;
1129 }
1130
1131 rc = boot_read_swap_state(fap, &state_slot0);
1132 if (rc != 0) {
1133 return BOOT_EFLASH;
1134 }
1135
1136 if (state_slot0.magic == BOOT_MAGIC_UNSET) {
1137 rc = boot_write_magic(fap);
1138 if (rc != 0) {
1139 return rc;
1140 }
1141 }
1142
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001143 if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001144 rc = boot_write_copy_done(fap);
1145 if (rc != 0) {
1146 return rc;
1147 }
1148 }
1149
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001150 if (state_slot0.image_ok == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001151 rc = boot_write_image_ok(fap);
1152 if (rc != 0) {
1153 return rc;
1154 }
1155 }
1156
1157 return 0;
1158}
1159
1160/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001161 * Performs an image swap if one is required.
1162 *
1163 * @param out_swap_type On success, the type of swap performed gets
1164 * written here.
1165 *
1166 * @return 0 on success; nonzero on failure.
1167 */
1168static int
1169boot_swap_if_needed(int *out_swap_type)
1170{
1171 struct boot_status bs;
1172 int swap_type;
1173 int rc;
1174
1175 /* Determine if we rebooted in the middle of an image swap
1176 * operation.
1177 */
1178 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001179 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001180 if (rc != 0) {
1181 return rc;
1182 }
1183
1184 /* If a partial swap was detected, complete it. */
1185 if (bs.idx != 0 || bs.state != 0) {
1186 rc = boot_copy_image(&bs);
1187 assert(rc == 0);
1188
1189 /* Extrapolate the type of the partial swap. We need this
1190 * information to know how to mark the swap complete in flash.
1191 */
1192 swap_type = boot_previous_swap_type();
1193 } else {
1194 swap_type = boot_validated_swap_type();
1195 switch (swap_type) {
1196 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001197 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001198 case BOOT_SWAP_TYPE_REVERT:
1199 rc = boot_copy_image(&bs);
1200 assert(rc == 0);
1201 break;
1202 }
1203 }
1204
1205 *out_swap_type = swap_type;
1206 return 0;
1207}
1208
1209/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001210 * Prepares the booting process. This function moves images around in flash as
1211 * appropriate, and tells you what address to boot from.
1212 *
1213 * @param rsp On success, indicates how booting should occur.
1214 *
1215 * @return 0 on success; nonzero on failure.
1216 */
1217int
1218boot_go(struct boot_rsp *rsp)
1219{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001220 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001221 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001222 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001223 int fa_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001224
1225 /* The array of slot sectors are defined here (as opposed to file scope) so
1226 * that they don't get allocated for non-boot-loader apps. This is
1227 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001228 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001229 */
Marti Bolivarc50926f2017-06-14 09:35:40 -04001230 static boot_sector_t slot0_sectors[BOOT_MAX_IMG_SECTORS];
1231 static boot_sector_t slot1_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001232 boot_data.imgs[0].sectors = slot0_sectors;
1233 boot_data.imgs[1].sectors = slot1_sectors;
1234
Marti Bolivarc0b47912017-06-13 17:18:09 -04001235 /* Open boot_data image areas for the duration of this call. */
1236 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1237 fa_id = flash_area_id_from_image_slot(slot);
1238 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1239 assert(rc == 0);
1240 }
1241 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1242 &BOOT_SCRATCH_AREA(&boot_data));
1243 assert(rc == 0);
1244
Christopher Collins92ea77f2016-12-12 15:59:26 -08001245 /* Determine the sector layout of the image slots and scratch area. */
1246 rc = boot_read_sectors();
1247 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001248 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001249 }
1250
1251 /* Attempt to read an image header from each slot. */
1252 rc = boot_read_image_headers();
1253 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001254 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001255 }
1256
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001257 /* If the image slots aren't compatible, no swap is possible. Just boot
1258 * into slot 0.
1259 */
1260 if (boot_slots_compatible()) {
1261 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001262 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001263 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001264 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001265 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001266 } else {
1267 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001268 }
1269
1270 switch (swap_type) {
1271 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig19356bf2017-05-11 16:19:36 -03001272#ifdef MCUBOOT_VALIDATE_SLOT0
David Brownd930ec62016-12-14 07:59:48 -07001273 rc = boot_validate_slot(0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001274 assert(rc == 0);
David Brownd930ec62016-12-14 07:59:48 -07001275 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001276 rc = BOOT_EBADIMAGE;
1277 goto out;
David Brownd930ec62016-12-14 07:59:48 -07001278 }
1279#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001280 slot = 0;
1281 break;
1282
1283 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001284 case BOOT_SWAP_TYPE_PERM:
Christopher Collins92ea77f2016-12-12 15:59:26 -08001285 slot = 1;
1286 boot_finalize_test_swap();
1287 break;
1288
1289 case BOOT_SWAP_TYPE_REVERT:
1290 slot = 1;
1291 boot_finalize_revert_swap();
1292 break;
1293
1294 case BOOT_SWAP_TYPE_FAIL:
1295 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1296 * try to boot into it again on the next reboot. Do this by pretending
1297 * we just reverted back to slot 0.
1298 */
1299 slot = 0;
1300 boot_finalize_revert_swap();
1301 break;
1302
1303 default:
1304 assert(0);
1305 slot = 0;
1306 break;
1307 }
1308
1309 /* Always boot from the primary slot. */
Marti Bolivar428cdbf2017-05-01 22:32:42 -04001310 rsp->br_flash_dev_id = boot_img_fa_device_id(&boot_data, 0);
Marti Bolivar88f48d92017-05-01 22:30:02 -04001311 rsp->br_image_off = boot_img_slot_off(&boot_data, 0);
Marti Bolivarf804f622017-06-12 15:41:48 -04001312 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001313
Marti Bolivarc0b47912017-06-13 17:18:09 -04001314 out:
1315 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1316 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1317 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1318 }
1319 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001320}
1321
1322int
1323split_go(int loader_slot, int split_slot, void **entry)
1324{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001325 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001326 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001327 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001328 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001329 int rc;
1330
Christopher Collins92ea77f2016-12-12 15:59:26 -08001331 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1332 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001333 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001334 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001335 boot_data.imgs[loader_slot].sectors = sectors + 0;
1336 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1337
1338 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1339 rc = flash_area_open(loader_flash_id,
1340 &BOOT_IMG_AREA(&boot_data, split_slot));
1341 assert(rc == 0);
1342 split_flash_id = flash_area_id_from_image_slot(split_slot);
1343 rc = flash_area_open(split_flash_id,
1344 &BOOT_IMG_AREA(&boot_data, split_slot));
1345 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001346
1347 /* Determine the sector layout of the image slots and scratch area. */
1348 rc = boot_read_sectors();
1349 if (rc != 0) {
1350 rc = SPLIT_GO_ERR;
1351 goto done;
1352 }
1353
1354 rc = boot_read_image_headers();
1355 if (rc != 0) {
1356 goto done;
1357 }
1358
Christopher Collins92ea77f2016-12-12 15:59:26 -08001359 /* Don't check the bootable image flag because we could really call a
1360 * bootable or non-bootable image. Just validate that the image check
1361 * passes which is distinct from the normal check.
1362 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001363 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001364 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001365 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001366 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001367 if (rc != 0) {
1368 rc = SPLIT_GO_NON_MATCHING;
1369 goto done;
1370 }
1371
Marti Bolivarea088872017-06-12 17:10:49 -04001372 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001373 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001374 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001375 rc = SPLIT_GO_OK;
1376
1377done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001378 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1379 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001380 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001381 return rc;
1382}