blob: dc1e88d1a55598f6ce0f0130c4ae11bd2356d36b [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;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300474 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700475 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);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300501 memset(buf, 0xFF, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700502 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:
Fabio Utzige7686262017-06-28 09:26:54 -0300768 if (fap_src) {
769 flash_area_close(fap_src);
770 }
771 if (fap_dst) {
772 flash_area_close(fap_dst);
773 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800774 return rc;
775}
776
Fabio Utzig2473ac02017-05-02 12:45:02 -0300777static inline int
778boot_status_init_by_id(int flash_area_id)
779{
780 const struct flash_area *fap;
781 struct boot_swap_state swap_state;
782 int rc;
783
784 rc = flash_area_open(flash_area_id, &fap);
785 assert(rc == 0);
786
787 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
788 assert(rc == 0);
789
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400790 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300791 rc = boot_write_image_ok(fap);
792 assert(rc == 0);
793 }
794
795 rc = boot_write_magic(fap);
796 assert(rc == 0);
797
798 flash_area_close(fap);
799
800 return 0;
801}
802
803static int
804boot_erase_last_sector_by_id(int flash_area_id)
805{
806 uint8_t slot;
807 uint32_t last_sector;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300808 int rc;
809
810 switch (flash_area_id) {
811 case FLASH_AREA_IMAGE_0:
812 slot = 0;
813 break;
814 case FLASH_AREA_IMAGE_1:
815 slot = 1;
816 break;
817 default:
818 return BOOT_EFLASH;
819 }
820
Marti Bolivard3269fd2017-06-12 16:31:12 -0400821 last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300822 rc = boot_erase_sector(flash_area_id,
Marti Bolivarea088872017-06-12 17:10:49 -0400823 boot_img_sector_off(&boot_data, slot, last_sector),
Marti Bolivard3269fd2017-06-12 16:31:12 -0400824 boot_img_sector_size(&boot_data, slot, last_sector));
Fabio Utzig2473ac02017-05-02 12:45:02 -0300825 assert(rc == 0);
826
827 return rc;
828}
829
Christopher Collins92ea77f2016-12-12 15:59:26 -0800830/**
831 * Swaps the contents of two flash regions within the two image slots.
832 *
833 * @param idx The index of the first sector in the range of
834 * sectors being swapped.
835 * @param sz The number of bytes to swap.
836 * @param bs The current boot status. This struct gets
837 * updated according to the outcome.
838 *
839 * @return 0 on success; nonzero on failure.
840 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300841#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800842static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800843boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
844{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300845 const struct flash_area *fap;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800846 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300847 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800848 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300849 uint32_t scratch_trailer_off;
850 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400851 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800852 int rc;
853
854 /* Calculate offset from start of image area. */
Marti Bolivarea088872017-06-12 17:10:49 -0400855 img_off = boot_img_sector_off(&boot_data, 0, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800856
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300857 copy_sz = sz;
Marti Bolivare10a7392017-06-14 16:20:07 -0400858 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -0400859
860 /* sz in this function is always is always sized on a multiple of the
Marti Bolivarea088872017-06-12 17:10:49 -0400861 * sector size. The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -0400862 * is to determine if we're swapping the last sector. The last sector
863 * needs special handling because it's where the trailer lives. If we're
864 * copying it, we need to use scratch to write the trailer temporarily.
865 *
866 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
867 * controls if special handling is needed (swapping last sector).
868 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400869 last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
Marti Bolivarea088872017-06-12 17:10:49 -0400870 if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300871 copy_sz -= trailer_sz;
872 }
873
Fabio Utzig2473ac02017-05-02 12:45:02 -0300874 bs->use_scratch = (bs->idx == 0 && copy_sz != sz);
875
Christopher Collins92ea77f2016-12-12 15:59:26 -0800876 if (bs->state == 0) {
877 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800878 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800879
880 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300881 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800882 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800883
Fabio Utzig2473ac02017-05-02 12:45:02 -0300884 if (bs->idx == 0) {
885 if (bs->use_scratch) {
886 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH);
887 } else {
888 /* Prepare the status area... here it is known that the
889 * last sector is not being used by the image data so it's
890 * safe to erase.
891 */
892 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300893 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300894
895 boot_status_init_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300896 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300897 }
898
Christopher Collins92ea77f2016-12-12 15:59:26 -0800899 bs->state = 1;
Christopher Collins4772ac42017-02-27 20:08:01 -0800900 rc = boot_write_status(bs);
901 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800902 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300903
Christopher Collins92ea77f2016-12-12 15:59:26 -0800904 if (bs->state == 1) {
905 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800906 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800907
Christopher Collins92ea77f2016-12-12 15:59:26 -0800908 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
909 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800910 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800911
Fabio Utzig2473ac02017-05-02 12:45:02 -0300912 if (bs->idx == 0 && !bs->use_scratch) {
913 /* If not all sectors of the slot are being swapped,
914 * guarantee here that only slot0 will have the state.
915 */
916 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1);
917 assert(rc == 0);
918 }
919
Christopher Collins92ea77f2016-12-12 15:59:26 -0800920 bs->state = 2;
Christopher Collins4772ac42017-02-27 20:08:01 -0800921 rc = boot_write_status(bs);
922 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800923 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300924
Christopher Collins92ea77f2016-12-12 15:59:26 -0800925 if (bs->state == 2) {
926 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800927 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800928
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300929 /* NOTE: also copy trailer from scratch (has status info) */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800930 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300931 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800932 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800933
Fabio Utzig94d998c2017-05-22 11:02:41 -0400934 if (bs->use_scratch) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300935 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
936 assert(rc == 0);
937
938 scratch_trailer_off = boot_status_off(fap);
939
940 flash_area_close(fap);
941
942 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
943 assert(rc == 0);
944
945 /* copy current status that is being maintained in scratch */
946 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Marti Bolivare10a7392017-06-14 16:20:07 -0400947 scratch_trailer_off,
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300948 img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -0400949 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300950 assert(rc == 0);
951
Fabio Utzig2473ac02017-05-02 12:45:02 -0300952 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
953 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300954 assert(rc == 0);
955
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400956 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300957 rc = boot_write_image_ok(fap);
958 assert(rc == 0);
959 }
960
961 rc = boot_write_magic(fap);
962 assert(rc == 0);
963
964 flash_area_close(fap);
965 }
966
Christopher Collins92ea77f2016-12-12 15:59:26 -0800967 bs->idx++;
968 bs->state = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300969 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -0800970 rc = boot_write_status(bs);
971 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800972 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800973}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300974#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800975
976/**
977 * Swaps the two images in flash. If a prior copy operation was interrupted
978 * by a system reset, this function completes that operation.
979 *
980 * @param bs The current boot status. This function reads
981 * this struct to determine if it is resuming
982 * an interrupted swap operation. This
983 * function writes the updated status to this
984 * function on return.
985 *
986 * @return 0 on success; nonzero on failure.
987 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300988#ifdef MCUBOOT_OVERWRITE_ONLY
David Brown17609d82017-05-05 09:41:34 -0600989static int
990boot_copy_image(struct boot_status *bs)
991{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400992 size_t sect_count;
993 size_t sect;
David Brown17609d82017-05-05 09:41:34 -0600994 int rc;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400995 size_t size = 0;
996 size_t this_size;
David Brown17609d82017-05-05 09:41:34 -0600997
998 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
999 BOOT_LOG_INF("Erasing slot0");
1000
Marti Bolivard3269fd2017-06-12 16:31:12 -04001001 sect_count = boot_img_num_sectors(&boot_data, 0);
David Brown17609d82017-05-05 09:41:34 -06001002 for (sect = 0; sect < sect_count; sect++) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001003 this_size = boot_img_sector_size(&boot_data, 0, sect);
David Brown17609d82017-05-05 09:41:34 -06001004 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
1005 size,
1006 this_size);
1007 assert(rc == 0);
1008
1009 size += this_size;
1010 }
1011
1012 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%x bytes",
1013 size);
1014 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
1015 0, 0, size);
1016
1017 /* Erase slot 1 so that we don't do the upgrade on every boot.
1018 * TODO: Perhaps verify slot 0's signature again? */
1019 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
Marti Bolivard3269fd2017-06-12 16:31:12 -04001020 0, boot_img_sector_size(&boot_data, 1, 0));
David Brown17609d82017-05-05 09:41:34 -06001021 assert(rc == 0);
1022
1023 return 0;
1024}
1025#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001026static int
1027boot_copy_image(struct boot_status *bs)
1028{
1029 uint32_t sz;
1030 int first_sector_idx;
1031 int last_sector_idx;
1032 int swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001033 struct image_header *hdr;
1034 uint32_t size;
1035 uint32_t copy_size;
1036 struct image_header tmp_hdr;
1037 int rc;
1038
1039 /* FIXME: just do this if asked by user? */
1040
1041 size = copy_size = 0;
1042
Marti Bolivarf804f622017-06-12 15:41:48 -04001043 hdr = boot_img_hdr(&boot_data, 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001044 if (hdr->ih_magic == IMAGE_MAGIC) {
1045 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1046 }
1047
Marti Bolivarf804f622017-06-12 15:41:48 -04001048 hdr = boot_img_hdr(&boot_data, 1);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001049 if (hdr->ih_magic == IMAGE_MAGIC) {
1050 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1051 }
1052
1053 if (!size || !copy_size || size == copy_size) {
Fabio Utzigc08ed212017-06-20 19:28:36 -03001054 rc = boot_read_header_from_scratch(&tmp_hdr);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001055 assert(rc == 0);
1056
1057 hdr = &tmp_hdr;
1058 if (hdr->ih_magic == IMAGE_MAGIC) {
1059 if (!size) {
1060 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1061 } else {
1062 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1063 }
1064 }
1065 }
1066
1067 if (size > copy_size) {
1068 copy_size = size;
1069 }
1070
1071 size = 0;
1072 last_sector_idx = 0;
1073 while (1) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001074 size += boot_img_sector_size(&boot_data, 0, last_sector_idx);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001075 if (size >= copy_size) {
1076 break;
1077 }
1078 last_sector_idx++;
1079 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001080
1081 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001082 while (last_sector_idx >= 0) {
1083 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
1084 if (swap_idx >= bs->idx) {
1085 boot_swap_sectors(first_sector_idx, sz, bs);
1086 }
1087
1088 last_sector_idx = first_sector_idx - 1;
1089 swap_idx++;
1090 }
1091
1092 return 0;
1093}
David Brown17609d82017-05-05 09:41:34 -06001094#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001095
1096/**
1097 * Marks a test image in slot 0 as fully copied.
1098 */
1099static int
1100boot_finalize_test_swap(void)
1101{
1102 const struct flash_area *fap;
1103 int rc;
1104
1105 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1106 if (rc != 0) {
1107 return BOOT_EFLASH;
1108 }
1109
1110 rc = boot_write_copy_done(fap);
1111 if (rc != 0) {
1112 return rc;
1113 }
1114
1115 return 0;
1116}
1117
1118/**
1119 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1120 * the status bytes from the image revert operation don't get processed on a
1121 * subsequent boot.
1122 */
1123static int
1124boot_finalize_revert_swap(void)
1125{
1126 const struct flash_area *fap;
1127 struct boot_swap_state state_slot0;
1128 int rc;
1129
1130 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1131 if (rc != 0) {
1132 return BOOT_EFLASH;
1133 }
1134
1135 rc = boot_read_swap_state(fap, &state_slot0);
1136 if (rc != 0) {
1137 return BOOT_EFLASH;
1138 }
1139
1140 if (state_slot0.magic == BOOT_MAGIC_UNSET) {
1141 rc = boot_write_magic(fap);
1142 if (rc != 0) {
1143 return rc;
1144 }
1145 }
1146
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001147 if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001148 rc = boot_write_copy_done(fap);
1149 if (rc != 0) {
1150 return rc;
1151 }
1152 }
1153
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001154 if (state_slot0.image_ok == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001155 rc = boot_write_image_ok(fap);
1156 if (rc != 0) {
1157 return rc;
1158 }
1159 }
1160
1161 return 0;
1162}
1163
1164/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001165 * Performs an image swap if one is required.
1166 *
1167 * @param out_swap_type On success, the type of swap performed gets
1168 * written here.
1169 *
1170 * @return 0 on success; nonzero on failure.
1171 */
1172static int
1173boot_swap_if_needed(int *out_swap_type)
1174{
1175 struct boot_status bs;
1176 int swap_type;
1177 int rc;
1178
1179 /* Determine if we rebooted in the middle of an image swap
1180 * operation.
1181 */
1182 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001183 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001184 if (rc != 0) {
1185 return rc;
1186 }
1187
1188 /* If a partial swap was detected, complete it. */
1189 if (bs.idx != 0 || bs.state != 0) {
1190 rc = boot_copy_image(&bs);
1191 assert(rc == 0);
1192
1193 /* Extrapolate the type of the partial swap. We need this
1194 * information to know how to mark the swap complete in flash.
1195 */
1196 swap_type = boot_previous_swap_type();
1197 } else {
1198 swap_type = boot_validated_swap_type();
1199 switch (swap_type) {
1200 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001201 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001202 case BOOT_SWAP_TYPE_REVERT:
1203 rc = boot_copy_image(&bs);
1204 assert(rc == 0);
1205 break;
1206 }
1207 }
1208
1209 *out_swap_type = swap_type;
1210 return 0;
1211}
1212
1213/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001214 * Prepares the booting process. This function moves images around in flash as
1215 * appropriate, and tells you what address to boot from.
1216 *
1217 * @param rsp On success, indicates how booting should occur.
1218 *
1219 * @return 0 on success; nonzero on failure.
1220 */
1221int
1222boot_go(struct boot_rsp *rsp)
1223{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001224 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001225 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001226 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001227 int fa_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001228
1229 /* The array of slot sectors are defined here (as opposed to file scope) so
1230 * that they don't get allocated for non-boot-loader apps. This is
1231 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001232 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001233 */
Marti Bolivarc50926f2017-06-14 09:35:40 -04001234 static boot_sector_t slot0_sectors[BOOT_MAX_IMG_SECTORS];
1235 static boot_sector_t slot1_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001236 boot_data.imgs[0].sectors = slot0_sectors;
1237 boot_data.imgs[1].sectors = slot1_sectors;
1238
Marti Bolivarc0b47912017-06-13 17:18:09 -04001239 /* Open boot_data image areas for the duration of this call. */
1240 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1241 fa_id = flash_area_id_from_image_slot(slot);
1242 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1243 assert(rc == 0);
1244 }
1245 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1246 &BOOT_SCRATCH_AREA(&boot_data));
1247 assert(rc == 0);
1248
Christopher Collins92ea77f2016-12-12 15:59:26 -08001249 /* Determine the sector layout of the image slots and scratch area. */
1250 rc = boot_read_sectors();
1251 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001252 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001253 }
1254
1255 /* Attempt to read an image header from each slot. */
1256 rc = boot_read_image_headers();
1257 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001258 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001259 }
1260
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001261 /* If the image slots aren't compatible, no swap is possible. Just boot
1262 * into slot 0.
1263 */
1264 if (boot_slots_compatible()) {
1265 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001266 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001267 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001268 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001269 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001270 } else {
1271 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001272 }
1273
1274 switch (swap_type) {
1275 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig19356bf2017-05-11 16:19:36 -03001276#ifdef MCUBOOT_VALIDATE_SLOT0
David Brownd930ec62016-12-14 07:59:48 -07001277 rc = boot_validate_slot(0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001278 assert(rc == 0);
David Brownd930ec62016-12-14 07:59:48 -07001279 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001280 rc = BOOT_EBADIMAGE;
1281 goto out;
David Brownd930ec62016-12-14 07:59:48 -07001282 }
1283#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001284 slot = 0;
1285 break;
1286
1287 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001288 case BOOT_SWAP_TYPE_PERM:
Christopher Collins92ea77f2016-12-12 15:59:26 -08001289 slot = 1;
1290 boot_finalize_test_swap();
1291 break;
1292
1293 case BOOT_SWAP_TYPE_REVERT:
1294 slot = 1;
1295 boot_finalize_revert_swap();
1296 break;
1297
1298 case BOOT_SWAP_TYPE_FAIL:
1299 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1300 * try to boot into it again on the next reboot. Do this by pretending
1301 * we just reverted back to slot 0.
1302 */
1303 slot = 0;
1304 boot_finalize_revert_swap();
1305 break;
1306
1307 default:
1308 assert(0);
1309 slot = 0;
1310 break;
1311 }
1312
1313 /* Always boot from the primary slot. */
Marti Bolivar428cdbf2017-05-01 22:32:42 -04001314 rsp->br_flash_dev_id = boot_img_fa_device_id(&boot_data, 0);
Marti Bolivar88f48d92017-05-01 22:30:02 -04001315 rsp->br_image_off = boot_img_slot_off(&boot_data, 0);
Marti Bolivarf804f622017-06-12 15:41:48 -04001316 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001317
Marti Bolivarc0b47912017-06-13 17:18:09 -04001318 out:
1319 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1320 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1321 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1322 }
1323 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001324}
1325
1326int
1327split_go(int loader_slot, int split_slot, void **entry)
1328{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001329 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001330 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001331 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001332 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001333 int rc;
1334
Christopher Collins92ea77f2016-12-12 15:59:26 -08001335 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1336 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001337 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001338 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001339 boot_data.imgs[loader_slot].sectors = sectors + 0;
1340 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1341
1342 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1343 rc = flash_area_open(loader_flash_id,
1344 &BOOT_IMG_AREA(&boot_data, split_slot));
1345 assert(rc == 0);
1346 split_flash_id = flash_area_id_from_image_slot(split_slot);
1347 rc = flash_area_open(split_flash_id,
1348 &BOOT_IMG_AREA(&boot_data, split_slot));
1349 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001350
1351 /* Determine the sector layout of the image slots and scratch area. */
1352 rc = boot_read_sectors();
1353 if (rc != 0) {
1354 rc = SPLIT_GO_ERR;
1355 goto done;
1356 }
1357
1358 rc = boot_read_image_headers();
1359 if (rc != 0) {
1360 goto done;
1361 }
1362
Christopher Collins92ea77f2016-12-12 15:59:26 -08001363 /* Don't check the bootable image flag because we could really call a
1364 * bootable or non-bootable image. Just validate that the image check
1365 * passes which is distinct from the normal check.
1366 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001367 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001368 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001369 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001370 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001371 if (rc != 0) {
1372 rc = SPLIT_GO_NON_MATCHING;
1373 goto done;
1374 }
1375
Marti Bolivarea088872017-06-12 17:10:49 -04001376 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001377 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001378 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001379 rc = SPLIT_GO_OK;
1380
1381done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001382 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1383 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001384 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001385 return rc;
1386}