blob: 178a9c73ab13e345cce39b8ad2c9a4b7736d6743 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/**
21 * This file provides an interface to the boot loader. Functions defined in
22 * this file should only be called while the boot loader is running.
23 */
24
25#include <assert.h>
26#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060027#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080028#include <inttypes.h>
29#include <stdlib.h>
30#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080031#include <hal/hal_flash.h>
32#include <os/os_malloc.h>
33#include "bootutil/bootutil.h"
34#include "bootutil/image.h"
35#include "bootutil_priv.h"
36
Marti Bolivarfd20c762017-02-07 16:52:50 -050037#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
38#include "bootutil/bootutil_log.h"
39
Fabio Utzigeed80b62017-06-10 08:03:05 -030040#ifdef APP_mynewt
41#include "mynewt/config.h"
42#endif
43
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040044static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080045
46struct boot_status_table {
47 /**
48 * For each field, a value of 0 means "any".
49 */
50 uint8_t bst_magic_slot0;
51 uint8_t bst_magic_scratch;
52 uint8_t bst_copy_done_slot0;
53 uint8_t bst_status_source;
54};
55
56/**
57 * This set of tables maps swap state contents to boot status location.
58 * When searching for a match, these tables must be iterated in order.
59 */
60static const struct boot_status_table boot_status_tables[] = {
61 {
62 /* | slot-0 | scratch |
63 * ----------+------------+------------|
64 * magic | Good | Any |
65 * copy-done | 0x01 | N/A |
66 * ----------+------------+------------'
67 * source: none |
68 * ------------------------------------'
69 */
70 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
71 .bst_magic_scratch = 0,
72 .bst_copy_done_slot0 = 0x01,
73 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
74 },
75
76 {
77 /* | slot-0 | scratch |
78 * ----------+------------+------------|
79 * magic | Good | Any |
80 * copy-done | 0xff | N/A |
81 * ----------+------------+------------'
82 * source: slot 0 |
83 * ------------------------------------'
84 */
85 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
86 .bst_magic_scratch = 0,
87 .bst_copy_done_slot0 = 0xff,
88 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
89 },
90
91 {
92 /* | slot-0 | scratch |
93 * ----------+------------+------------|
94 * magic | Any | Good |
95 * copy-done | Any | N/A |
96 * ----------+------------+------------'
97 * source: scratch |
98 * ------------------------------------'
99 */
100 .bst_magic_slot0 = 0,
101 .bst_magic_scratch = BOOT_MAGIC_GOOD,
102 .bst_copy_done_slot0 = 0,
103 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
104 },
105
106 {
107 /* | slot-0 | scratch |
108 * ----------+------------+------------|
109 * magic | Unset | Any |
110 * copy-done | 0xff | N/A |
111 * ----------+------------+------------|
112 * source: varies |
113 * ------------------------------------+------------------------------+
114 * This represents one of two cases: |
115 * o No swaps ever (no status to read, so no harm in checking). |
116 * o Mid-revert; status in slot 0. |
117 * -------------------------------------------------------------------'
118 */
119 .bst_magic_slot0 = BOOT_MAGIC_UNSET,
120 .bst_magic_scratch = 0,
121 .bst_copy_done_slot0 = 0xff,
122 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
123 },
124};
125
126#define BOOT_STATUS_TABLES_COUNT \
127 (sizeof boot_status_tables / sizeof boot_status_tables[0])
128
129/**
130 * This table indicates the next swap type that should be performed. The first
131 * column contains the current swap type. The second column contains the swap
132 * type that should be effected after the first completes.
133 */
134static const uint8_t boot_swap_trans_table[][2] = {
135 /* From To */
136 { BOOT_SWAP_TYPE_REVERT, BOOT_SWAP_TYPE_NONE },
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800137 { BOOT_SWAP_TYPE_PERM, BOOT_SWAP_TYPE_NONE },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800138 { BOOT_SWAP_TYPE_TEST, BOOT_SWAP_TYPE_REVERT },
139};
140
141#define BOOT_SWAP_TRANS_TABLE_SIZE \
142 (sizeof boot_swap_trans_table / sizeof boot_swap_trans_table[0])
143
Marti Bolivarfd20c762017-02-07 16:52:50 -0500144#define BOOT_LOG_SWAP_STATE(area, state) \
145 BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \
146 (area), \
147 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
148 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
149 "bad"), \
150 (state)->copy_done, \
151 (state)->image_ok)
152
Christopher Collins92ea77f2016-12-12 15:59:26 -0800153/**
154 * Determines where in flash the most recent boot status is stored. The boot
155 * status is necessary for completing a swap that was interrupted by a boot
156 * loader reset.
157 *
158 * @return A BOOT_STATUS_SOURCE_[...] code indicating where * status should be read from.
159 */
160static int
161boot_status_source(void)
162{
163 const struct boot_status_table *table;
164 struct boot_swap_state state_scratch;
165 struct boot_swap_state state_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800166 int rc;
167 int i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500168 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800169
Fabio Utzig2473ac02017-05-02 12:45:02 -0300170 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800171 assert(rc == 0);
172
Fabio Utzig2473ac02017-05-02 12:45:02 -0300173 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800174 assert(rc == 0);
175
Marti Bolivarfd20c762017-02-07 16:52:50 -0500176 BOOT_LOG_SWAP_STATE("Image 0", &state_slot0);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500177 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
178
Christopher Collins92ea77f2016-12-12 15:59:26 -0800179 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300180 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800181
182 if ((table->bst_magic_slot0 == 0 ||
183 table->bst_magic_slot0 == state_slot0.magic) &&
184 (table->bst_magic_scratch == 0 ||
185 table->bst_magic_scratch == state_scratch.magic) &&
186 (table->bst_copy_done_slot0 == 0 ||
187 table->bst_copy_done_slot0 == state_slot0.copy_done)) {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500188 source = table->bst_status_source;
189 BOOT_LOG_INF("Boot source: %s",
190 source == BOOT_STATUS_SOURCE_NONE ? "none" :
191 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
192 source == BOOT_STATUS_SOURCE_SLOT0 ? "slot 0" :
193 "BUG; can't happen");
194 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800195 }
196 }
197
Marti Bolivarfd20c762017-02-07 16:52:50 -0500198 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800199 return BOOT_STATUS_SOURCE_NONE;
200}
201
202/**
203 * Calculates the type of swap that just completed.
204 */
205static int
206boot_previous_swap_type(void)
207{
208 int post_swap_type;
209 int i;
210
211 post_swap_type = boot_swap_type();
212
213 for (i = 0; i < BOOT_SWAP_TRANS_TABLE_SIZE; i++){
214 if (boot_swap_trans_table[i][1] == post_swap_type) {
215 return boot_swap_trans_table[i][0];
216 }
217 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300218
Christopher Collins92ea77f2016-12-12 15:59:26 -0800219 /* XXX: Temporary assert. */
220 assert(0);
221
222 return BOOT_SWAP_TYPE_REVERT;
223}
224
225static int
Fabio Utzigc08ed212017-06-20 19:28:36 -0300226boot_read_header_from_scratch(struct image_header *out_hdr)
227{
228 const struct flash_area *fap;
229 int rc;
230
231 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
232 if (rc != 0) {
233 return BOOT_EFLASH;
234 }
235
236 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
237 if (rc != 0) {
238 rc = BOOT_EFLASH;
239 }
240
241 flash_area_close(fap);
242 return rc;
243}
244
245static int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800246boot_read_image_header(int slot, struct image_header *out_hdr)
247{
248 const struct flash_area *fap;
249 int area_id;
250 int rc;
251
252 area_id = flash_area_id_from_image_slot(slot);
253 rc = flash_area_open(area_id, &fap);
254 if (rc != 0) {
255 rc = BOOT_EFLASH;
256 goto done;
257 }
258
259 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
260 if (rc != 0) {
261 rc = BOOT_EFLASH;
262 goto done;
263 }
264
265 rc = 0;
266
267done:
268 flash_area_close(fap);
269 return rc;
270}
271
272static int
273boot_read_image_headers(void)
274{
275 int rc;
276 int i;
277
278 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400279 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800280 if (rc != 0) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300281 /* If at least the first slot's header was read successfully, then
282 * the boot loader can attempt a boot. Failure to read any headers
283 * is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800284 */
285 if (i > 0) {
286 return 0;
287 } else {
288 return rc;
289 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800290 }
291 }
292
293 return 0;
294}
295
296static uint8_t
297boot_write_sz(void)
298{
299 uint8_t elem_sz;
300 uint8_t align;
301
302 /* Figure out what size to write update status update as. The size depends
303 * on what the minimum write size is for scratch area, active image slot.
304 * We need to use the bigger of those 2 values.
305 */
Marti Bolivare2587152017-06-12 15:52:05 -0400306 elem_sz = hal_flash_align(boot_img_fa_device_id(&boot_data, 0));
307 align = hal_flash_align(boot_scratch_fa_device_id(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800308 if (align > elem_sz) {
309 elem_sz = align;
310 }
311
312 return elem_sz;
313}
314
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800315static int
316boot_slots_compatible(void)
317{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400318 size_t num_sectors_0 = boot_img_num_sectors(&boot_data, 0);
319 size_t num_sectors_1 = boot_img_num_sectors(&boot_data, 1);
320 size_t size_0, size_1;
321 size_t i;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800322
323 /* Ensure both image slots have identical sector layouts. */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400324 if (num_sectors_0 != num_sectors_1) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800325 return 0;
326 }
Marti Bolivard3269fd2017-06-12 16:31:12 -0400327 for (i = 0; i < num_sectors_0; i++) {
328 size_0 = boot_img_sector_size(&boot_data, 0, i);
329 size_1 = boot_img_sector_size(&boot_data, 1, i);
330 if (size_0 != size_1) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800331 return 0;
332 }
333 }
334
335 return 1;
336}
337
Christopher Collins92ea77f2016-12-12 15:59:26 -0800338/**
339 * Determines the sector layout of both image slots and the scratch area.
340 * This information is necessary for calculating the number of bytes to erase
341 * and copy during an image swap. The information collected during this
342 * function is used to populate the boot_data global.
343 */
344static int
345boot_read_sectors(void)
346{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800347 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800348
Marti Bolivarcca28a92017-06-12 16:52:22 -0400349 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800350 if (rc != 0) {
351 return BOOT_EFLASH;
352 }
353
Marti Bolivarcca28a92017-06-12 16:52:22 -0400354 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800355 if (rc != 0) {
356 return BOOT_EFLASH;
357 }
358
Marti Bolivare10a7392017-06-14 16:20:07 -0400359 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800360
361 return 0;
362}
363
364static uint32_t
365boot_status_internal_off(int idx, int state, int elem_sz)
366{
367 int idx_sz;
368
369 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
370
371 return idx * idx_sz + state * elem_sz;
372}
373
374/**
375 * Reads the status of a partially-completed swap, if any. This is necessary
376 * to recover in case the boot lodaer was reset in the middle of a swap
377 * operation.
378 */
379static int
380boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
381{
382 uint32_t off;
383 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300384 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800385 int found;
386 int rc;
387 int i;
388
389 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400390 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300391
Christopher Collins92ea77f2016-12-12 15:59:26 -0800392 found = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300393 for (i = 0; i < max_entries; i++) {
Marti Bolivare10a7392017-06-14 16:20:07 -0400394 rc = flash_area_read(fap, off + i * BOOT_WRITE_SZ(&boot_data),
395 &status, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800396 if (rc != 0) {
397 return BOOT_EFLASH;
398 }
399
400 if (status == 0xff) {
401 if (found) {
402 break;
403 }
404 } else if (!found) {
405 found = 1;
406 }
407 }
408
409 if (found) {
410 i--;
Fabio Utzig94d998c2017-05-22 11:02:41 -0400411 bs->idx = i / BOOT_STATUS_STATE_COUNT;
412 bs->state = i % BOOT_STATUS_STATE_COUNT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800413 }
414
415 return 0;
416}
417
418/**
419 * Reads the boot status from the flash. The boot status contains
420 * the current state of an interrupted image copy operation. If the boot
421 * status is not present, or it indicates that previous copy finished,
422 * there is no operation in progress.
423 */
424static int
425boot_read_status(struct boot_status *bs)
426{
427 const struct flash_area *fap;
428 int status_loc;
429 int area_id;
430 int rc;
431
432 memset(bs, 0, sizeof *bs);
433
434 status_loc = boot_status_source();
435 switch (status_loc) {
436 case BOOT_STATUS_SOURCE_NONE:
437 return 0;
438
439 case BOOT_STATUS_SOURCE_SCRATCH:
440 area_id = FLASH_AREA_IMAGE_SCRATCH;
441 break;
442
443 case BOOT_STATUS_SOURCE_SLOT0:
444 area_id = FLASH_AREA_IMAGE_0;
445 break;
446
447 default:
448 assert(0);
449 return BOOT_EBADARGS;
450 }
451
452 rc = flash_area_open(area_id, &fap);
453 if (rc != 0) {
454 return BOOT_EFLASH;
455 }
456
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300457 return boot_read_status_bytes(fap, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800458}
459
460/**
461 * Writes the supplied boot status to the flash file system. The boot status
462 * contains the current state of an in-progress image copy operation.
463 *
464 * @param bs The boot status to write.
465 *
466 * @return 0 on success; nonzero on failure.
467 */
468int
469boot_write_status(struct boot_status *bs)
470{
471 const struct flash_area *fap;
472 uint32_t off;
473 int area_id;
474 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300475 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700476 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800477
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300478 /* NOTE: The first sector copied (that is the last sector on slot) contains
479 * the trailer. Since in the last step SLOT 0 is erased, the first
480 * two status writes go to the scratch which will be copied to SLOT 0!
481 */
482
Fabio Utzig2473ac02017-05-02 12:45:02 -0300483 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800484 /* Write to scratch. */
485 area_id = FLASH_AREA_IMAGE_SCRATCH;
486 } else {
487 /* Write to slot 0. */
488 area_id = FLASH_AREA_IMAGE_0;
489 }
490
491 rc = flash_area_open(area_id, &fap);
492 if (rc != 0) {
493 rc = BOOT_EFLASH;
494 goto done;
495 }
496
497 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400498 boot_status_internal_off(bs->idx, bs->state,
499 BOOT_WRITE_SZ(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800500
David Brown9d725462017-01-23 15:50:58 -0700501 align = hal_flash_align(fap->fa_device_id);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300502 memset(buf, 0xFF, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700503 buf[0] = bs->state;
504
505 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800506 if (rc != 0) {
507 rc = BOOT_EFLASH;
508 goto done;
509 }
510
511 rc = 0;
512
513done:
514 flash_area_close(fap);
515 return rc;
516}
517
518/*
519 * Validate image hash/signature in a slot.
520 */
521static int
522boot_image_check(struct image_header *hdr, const struct flash_area *fap)
523{
David Browndb1d9d32017-01-06 11:07:54 -0700524 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800525
Christopher Collins92ea77f2016-12-12 15:59:26 -0800526 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
527 NULL, 0, NULL)) {
528 return BOOT_EBADIMAGE;
529 }
530 return 0;
531}
532
533static int
534split_image_check(struct image_header *app_hdr,
535 const struct flash_area *app_fap,
536 struct image_header *loader_hdr,
537 const struct flash_area *loader_fap)
538{
539 static void *tmpbuf;
540 uint8_t loader_hash[32];
541
542 if (!tmpbuf) {
543 tmpbuf = malloc(BOOT_TMPBUF_SZ);
544 if (!tmpbuf) {
545 return BOOT_ENOMEM;
546 }
547 }
548
549 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
550 NULL, 0, loader_hash)) {
551 return BOOT_EBADIMAGE;
552 }
553
554 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
555 loader_hash, 32, NULL)) {
556 return BOOT_EBADIMAGE;
557 }
558
559 return 0;
560}
561
562static int
David Brownd930ec62016-12-14 07:59:48 -0700563boot_validate_slot(int slot)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800564{
565 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400566 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800567 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300568
Marti Bolivarf804f622017-06-12 15:41:48 -0400569 hdr = boot_img_hdr(&boot_data, slot);
570 if (hdr->ih_magic == 0xffffffff || hdr->ih_flags & IMAGE_F_NON_BOOTABLE) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300571 /* No bootable image in slot; continue booting from slot 0. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800572 return -1;
573 }
574
David Brownd930ec62016-12-14 07:59:48 -0700575 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800576 if (rc != 0) {
577 return BOOT_EFLASH;
578 }
579
Marti Bolivarf804f622017-06-12 15:41:48 -0400580 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap) != 0)) {
David Brownb38e0442017-02-24 13:57:12 -0700581 if (slot != 0) {
582 flash_area_erase(fap, 0, fap->fa_size);
583 /* Image in slot 1 is invalid. Erase the image and
584 * continue booting from slot 0.
585 */
586 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800587 return -1;
588 }
589
590 flash_area_close(fap);
591
592 /* Image in slot 1 is valid. */
593 return 0;
594}
595
596/**
597 * Determines which swap operation to perform, if any. If it is determined
598 * that a swap operation is required, the image in the second slot is checked
599 * for validity. If the image in the second slot is invalid, it is erased, and
600 * a swap type of "none" is indicated.
601 *
602 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
603 */
604static int
605boot_validated_swap_type(void)
606{
607 int swap_type;
608 int rc;
609
610 swap_type = boot_swap_type();
611 if (swap_type == BOOT_SWAP_TYPE_NONE) {
612 /* Continue using slot 0. */
613 return BOOT_SWAP_TYPE_NONE;
614 }
615
616 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
David Brownd930ec62016-12-14 07:59:48 -0700617 rc = boot_validate_slot(1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800618 if (rc != 0) {
619 return BOOT_SWAP_TYPE_FAIL;
620 }
621
622 return swap_type;
623}
624
625/**
626 * Calculates the number of sectors the scratch area can contain. A "last"
627 * source sector is specified because images are copied backwards in flash
628 * (final index to index number 0).
629 *
630 * @param last_sector_idx The index of the last source sector
631 * (inclusive).
632 * @param out_first_sector_idx The index of the first source sector
633 * (inclusive) gets written here.
634 *
635 * @return The number of bytes comprised by the
636 * [first-sector, last-sector] range.
637 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300638#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800639static uint32_t
640boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
641{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400642 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800643 uint32_t new_sz;
644 uint32_t sz;
645 int i;
646
647 sz = 0;
648
Marti Bolivard3269fd2017-06-12 16:31:12 -0400649 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800650 for (i = last_sector_idx; i >= 0; i--) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400651 new_sz = sz + boot_img_sector_size(&boot_data, 0, i);
652 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800653 break;
654 }
655 sz = new_sz;
656 }
657
658 /* i currently refers to a sector that doesn't fit or it is -1 because all
659 * sectors have been processed. In both cases, exclude sector i.
660 */
661 *out_first_sector_idx = i + 1;
662 return sz;
663}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300664#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800665
666/**
667 * Erases a region of flash.
668 *
669 * @param flash_area_idx The ID of the flash area containing the region
670 * to erase.
671 * @param off The offset within the flash area to start the
672 * erase.
673 * @param sz The number of bytes to erase.
674 *
675 * @return 0 on success; nonzero on failure.
676 */
677static int
678boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
679{
680 const struct flash_area *fap;
681 int rc;
682
683 rc = flash_area_open(flash_area_id, &fap);
684 if (rc != 0) {
685 rc = BOOT_EFLASH;
686 goto done;
687 }
688
689 rc = flash_area_erase(fap, off, sz);
690 if (rc != 0) {
691 rc = BOOT_EFLASH;
692 goto done;
693 }
694
695 rc = 0;
696
697done:
698 flash_area_close(fap);
699 return rc;
700}
701
702/**
703 * Copies the contents of one flash region to another. You must erase the
704 * destination region prior to calling this function.
705 *
706 * @param flash_area_id_src The ID of the source flash area.
707 * @param flash_area_id_dst The ID of the destination flash area.
708 * @param off_src The offset within the source flash area to
709 * copy from.
710 * @param off_dst The offset within the destination flash area to
711 * copy to.
712 * @param sz The number of bytes to copy.
713 *
714 * @return 0 on success; nonzero on failure.
715 */
716static int
717boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
718 uint32_t off_src, uint32_t off_dst, uint32_t sz)
719{
720 const struct flash_area *fap_src;
721 const struct flash_area *fap_dst;
722 uint32_t bytes_copied;
723 int chunk_sz;
724 int rc;
725
726 static uint8_t buf[1024];
727
728 fap_src = NULL;
729 fap_dst = NULL;
730
731 rc = flash_area_open(flash_area_id_src, &fap_src);
732 if (rc != 0) {
733 rc = BOOT_EFLASH;
734 goto done;
735 }
736
737 rc = flash_area_open(flash_area_id_dst, &fap_dst);
738 if (rc != 0) {
739 rc = BOOT_EFLASH;
740 goto done;
741 }
742
743 bytes_copied = 0;
744 while (bytes_copied < sz) {
745 if (sz - bytes_copied > sizeof buf) {
746 chunk_sz = sizeof buf;
747 } else {
748 chunk_sz = sz - bytes_copied;
749 }
750
751 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
752 if (rc != 0) {
753 rc = BOOT_EFLASH;
754 goto done;
755 }
756
757 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
758 if (rc != 0) {
759 rc = BOOT_EFLASH;
760 goto done;
761 }
762
763 bytes_copied += chunk_sz;
764 }
765
766 rc = 0;
767
768done:
Fabio Utzige7686262017-06-28 09:26:54 -0300769 if (fap_src) {
770 flash_area_close(fap_src);
771 }
772 if (fap_dst) {
773 flash_area_close(fap_dst);
774 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800775 return rc;
776}
777
Fabio Utzig2473ac02017-05-02 12:45:02 -0300778static inline int
779boot_status_init_by_id(int flash_area_id)
780{
781 const struct flash_area *fap;
782 struct boot_swap_state swap_state;
783 int rc;
784
785 rc = flash_area_open(flash_area_id, &fap);
786 assert(rc == 0);
787
788 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
789 assert(rc == 0);
790
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400791 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300792 rc = boot_write_image_ok(fap);
793 assert(rc == 0);
794 }
795
796 rc = boot_write_magic(fap);
797 assert(rc == 0);
798
799 flash_area_close(fap);
800
801 return 0;
802}
803
804static int
805boot_erase_last_sector_by_id(int flash_area_id)
806{
807 uint8_t slot;
808 uint32_t last_sector;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300809 int rc;
810
811 switch (flash_area_id) {
812 case FLASH_AREA_IMAGE_0:
813 slot = 0;
814 break;
815 case FLASH_AREA_IMAGE_1:
816 slot = 1;
817 break;
818 default:
819 return BOOT_EFLASH;
820 }
821
Marti Bolivard3269fd2017-06-12 16:31:12 -0400822 last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300823 rc = boot_erase_sector(flash_area_id,
Marti Bolivarea088872017-06-12 17:10:49 -0400824 boot_img_sector_off(&boot_data, slot, last_sector),
Marti Bolivard3269fd2017-06-12 16:31:12 -0400825 boot_img_sector_size(&boot_data, slot, last_sector));
Fabio Utzig2473ac02017-05-02 12:45:02 -0300826 assert(rc == 0);
827
828 return rc;
829}
830
Christopher Collins92ea77f2016-12-12 15:59:26 -0800831/**
832 * Swaps the contents of two flash regions within the two image slots.
833 *
834 * @param idx The index of the first sector in the range of
835 * sectors being swapped.
836 * @param sz The number of bytes to swap.
837 * @param bs The current boot status. This struct gets
838 * updated according to the outcome.
839 *
840 * @return 0 on success; nonzero on failure.
841 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300842#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800843static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800844boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
845{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300846 const struct flash_area *fap;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800847 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300848 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800849 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300850 uint32_t scratch_trailer_off;
851 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400852 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800853 int rc;
854
855 /* Calculate offset from start of image area. */
Marti Bolivarea088872017-06-12 17:10:49 -0400856 img_off = boot_img_sector_off(&boot_data, 0, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800857
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300858 copy_sz = sz;
Marti Bolivare10a7392017-06-14 16:20:07 -0400859 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -0400860
861 /* sz in this function is always is always sized on a multiple of the
Marti Bolivarea088872017-06-12 17:10:49 -0400862 * sector size. The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -0400863 * is to determine if we're swapping the last sector. The last sector
864 * needs special handling because it's where the trailer lives. If we're
865 * copying it, we need to use scratch to write the trailer temporarily.
866 *
867 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
868 * controls if special handling is needed (swapping last sector).
869 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400870 last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
Marti Bolivarea088872017-06-12 17:10:49 -0400871 if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300872 copy_sz -= trailer_sz;
873 }
874
Fabio Utzig2473ac02017-05-02 12:45:02 -0300875 bs->use_scratch = (bs->idx == 0 && copy_sz != sz);
876
Christopher Collins92ea77f2016-12-12 15:59:26 -0800877 if (bs->state == 0) {
878 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800879 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800880
881 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300882 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800883 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800884
Fabio Utzig2473ac02017-05-02 12:45:02 -0300885 if (bs->idx == 0) {
886 if (bs->use_scratch) {
887 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH);
888 } else {
889 /* Prepare the status area... here it is known that the
890 * last sector is not being used by the image data so it's
891 * safe to erase.
892 */
893 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300894 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300895
896 boot_status_init_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300897 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300898 }
899
Christopher Collins92ea77f2016-12-12 15:59:26 -0800900 bs->state = 1;
Christopher Collins4772ac42017-02-27 20:08:01 -0800901 rc = boot_write_status(bs);
902 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800903 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300904
Christopher Collins92ea77f2016-12-12 15:59:26 -0800905 if (bs->state == 1) {
906 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800907 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800908
Christopher Collins92ea77f2016-12-12 15:59:26 -0800909 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
910 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800911 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800912
Fabio Utzig2473ac02017-05-02 12:45:02 -0300913 if (bs->idx == 0 && !bs->use_scratch) {
914 /* If not all sectors of the slot are being swapped,
915 * guarantee here that only slot0 will have the state.
916 */
917 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1);
918 assert(rc == 0);
919 }
920
Christopher Collins92ea77f2016-12-12 15:59:26 -0800921 bs->state = 2;
Christopher Collins4772ac42017-02-27 20:08:01 -0800922 rc = boot_write_status(bs);
923 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800924 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300925
Christopher Collins92ea77f2016-12-12 15:59:26 -0800926 if (bs->state == 2) {
927 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800928 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800929
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300930 /* NOTE: also copy trailer from scratch (has status info) */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800931 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300932 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800933 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800934
Fabio Utzig94d998c2017-05-22 11:02:41 -0400935 if (bs->use_scratch) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300936 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
937 assert(rc == 0);
938
939 scratch_trailer_off = boot_status_off(fap);
940
941 flash_area_close(fap);
942
943 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
944 assert(rc == 0);
945
946 /* copy current status that is being maintained in scratch */
947 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Marti Bolivare10a7392017-06-14 16:20:07 -0400948 scratch_trailer_off,
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300949 img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -0400950 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300951 assert(rc == 0);
952
Fabio Utzig2473ac02017-05-02 12:45:02 -0300953 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
954 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300955 assert(rc == 0);
956
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400957 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300958 rc = boot_write_image_ok(fap);
959 assert(rc == 0);
960 }
961
962 rc = boot_write_magic(fap);
963 assert(rc == 0);
964
965 flash_area_close(fap);
966 }
967
Christopher Collins92ea77f2016-12-12 15:59:26 -0800968 bs->idx++;
969 bs->state = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300970 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -0800971 rc = boot_write_status(bs);
972 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800973 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800974}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300975#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800976
977/**
978 * Swaps the two images in flash. If a prior copy operation was interrupted
979 * by a system reset, this function completes that operation.
980 *
981 * @param bs The current boot status. This function reads
982 * this struct to determine if it is resuming
983 * an interrupted swap operation. This
984 * function writes the updated status to this
985 * function on return.
986 *
987 * @return 0 on success; nonzero on failure.
988 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300989#ifdef MCUBOOT_OVERWRITE_ONLY
David Brown17609d82017-05-05 09:41:34 -0600990static int
991boot_copy_image(struct boot_status *bs)
992{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400993 size_t sect_count;
994 size_t sect;
David Brown17609d82017-05-05 09:41:34 -0600995 int rc;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400996 size_t size = 0;
997 size_t this_size;
David Brown17609d82017-05-05 09:41:34 -0600998
999 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
1000 BOOT_LOG_INF("Erasing slot0");
1001
Marti Bolivard3269fd2017-06-12 16:31:12 -04001002 sect_count = boot_img_num_sectors(&boot_data, 0);
David Brown17609d82017-05-05 09:41:34 -06001003 for (sect = 0; sect < sect_count; sect++) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001004 this_size = boot_img_sector_size(&boot_data, 0, sect);
David Brown17609d82017-05-05 09:41:34 -06001005 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
1006 size,
1007 this_size);
1008 assert(rc == 0);
1009
1010 size += this_size;
1011 }
1012
1013 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%x bytes",
1014 size);
1015 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
1016 0, 0, size);
1017
1018 /* Erase slot 1 so that we don't do the upgrade on every boot.
1019 * TODO: Perhaps verify slot 0's signature again? */
1020 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
Marti Bolivard3269fd2017-06-12 16:31:12 -04001021 0, boot_img_sector_size(&boot_data, 1, 0));
David Brown17609d82017-05-05 09:41:34 -06001022 assert(rc == 0);
1023
1024 return 0;
1025}
1026#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001027static int
1028boot_copy_image(struct boot_status *bs)
1029{
1030 uint32_t sz;
1031 int first_sector_idx;
1032 int last_sector_idx;
1033 int swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001034 struct image_header *hdr;
1035 uint32_t size;
1036 uint32_t copy_size;
1037 struct image_header tmp_hdr;
1038 int rc;
1039
1040 /* FIXME: just do this if asked by user? */
1041
1042 size = copy_size = 0;
1043
Marti Bolivarf804f622017-06-12 15:41:48 -04001044 hdr = boot_img_hdr(&boot_data, 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001045 if (hdr->ih_magic == IMAGE_MAGIC) {
1046 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1047 }
1048
Marti Bolivarf804f622017-06-12 15:41:48 -04001049 hdr = boot_img_hdr(&boot_data, 1);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001050 if (hdr->ih_magic == IMAGE_MAGIC) {
1051 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1052 }
1053
1054 if (!size || !copy_size || size == copy_size) {
Fabio Utzigc08ed212017-06-20 19:28:36 -03001055 rc = boot_read_header_from_scratch(&tmp_hdr);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001056 assert(rc == 0);
1057
1058 hdr = &tmp_hdr;
1059 if (hdr->ih_magic == IMAGE_MAGIC) {
1060 if (!size) {
1061 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1062 } else {
1063 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1064 }
1065 }
1066 }
1067
1068 if (size > copy_size) {
1069 copy_size = size;
1070 }
1071
1072 size = 0;
1073 last_sector_idx = 0;
1074 while (1) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001075 size += boot_img_sector_size(&boot_data, 0, last_sector_idx);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001076 if (size >= copy_size) {
1077 break;
1078 }
1079 last_sector_idx++;
1080 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001081
1082 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001083 while (last_sector_idx >= 0) {
1084 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
1085 if (swap_idx >= bs->idx) {
1086 boot_swap_sectors(first_sector_idx, sz, bs);
1087 }
1088
1089 last_sector_idx = first_sector_idx - 1;
1090 swap_idx++;
1091 }
1092
1093 return 0;
1094}
David Brown17609d82017-05-05 09:41:34 -06001095#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001096
1097/**
1098 * Marks a test image in slot 0 as fully copied.
1099 */
1100static int
1101boot_finalize_test_swap(void)
1102{
1103 const struct flash_area *fap;
1104 int rc;
1105
1106 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1107 if (rc != 0) {
1108 return BOOT_EFLASH;
1109 }
1110
1111 rc = boot_write_copy_done(fap);
1112 if (rc != 0) {
1113 return rc;
1114 }
1115
1116 return 0;
1117}
1118
1119/**
1120 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1121 * the status bytes from the image revert operation don't get processed on a
1122 * subsequent boot.
1123 */
1124static int
1125boot_finalize_revert_swap(void)
1126{
1127 const struct flash_area *fap;
1128 struct boot_swap_state state_slot0;
1129 int rc;
1130
1131 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1132 if (rc != 0) {
1133 return BOOT_EFLASH;
1134 }
1135
1136 rc = boot_read_swap_state(fap, &state_slot0);
1137 if (rc != 0) {
1138 return BOOT_EFLASH;
1139 }
1140
1141 if (state_slot0.magic == BOOT_MAGIC_UNSET) {
1142 rc = boot_write_magic(fap);
1143 if (rc != 0) {
1144 return rc;
1145 }
1146 }
1147
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001148 if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001149 rc = boot_write_copy_done(fap);
1150 if (rc != 0) {
1151 return rc;
1152 }
1153 }
1154
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001155 if (state_slot0.image_ok == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001156 rc = boot_write_image_ok(fap);
1157 if (rc != 0) {
1158 return rc;
1159 }
1160 }
1161
1162 return 0;
1163}
1164
1165/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001166 * Performs an image swap if one is required.
1167 *
1168 * @param out_swap_type On success, the type of swap performed gets
1169 * written here.
1170 *
1171 * @return 0 on success; nonzero on failure.
1172 */
1173static int
1174boot_swap_if_needed(int *out_swap_type)
1175{
1176 struct boot_status bs;
1177 int swap_type;
1178 int rc;
1179
1180 /* Determine if we rebooted in the middle of an image swap
1181 * operation.
1182 */
1183 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001184 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001185 if (rc != 0) {
1186 return rc;
1187 }
1188
1189 /* If a partial swap was detected, complete it. */
1190 if (bs.idx != 0 || bs.state != 0) {
1191 rc = boot_copy_image(&bs);
1192 assert(rc == 0);
1193
1194 /* Extrapolate the type of the partial swap. We need this
1195 * information to know how to mark the swap complete in flash.
1196 */
1197 swap_type = boot_previous_swap_type();
1198 } else {
1199 swap_type = boot_validated_swap_type();
1200 switch (swap_type) {
1201 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001202 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001203 case BOOT_SWAP_TYPE_REVERT:
1204 rc = boot_copy_image(&bs);
1205 assert(rc == 0);
1206 break;
1207 }
1208 }
1209
1210 *out_swap_type = swap_type;
1211 return 0;
1212}
1213
1214/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001215 * Prepares the booting process. This function moves images around in flash as
1216 * appropriate, and tells you what address to boot from.
1217 *
1218 * @param rsp On success, indicates how booting should occur.
1219 *
1220 * @return 0 on success; nonzero on failure.
1221 */
1222int
1223boot_go(struct boot_rsp *rsp)
1224{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001225 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001226 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001227 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001228 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001229 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001230
1231 /* The array of slot sectors are defined here (as opposed to file scope) so
1232 * that they don't get allocated for non-boot-loader apps. This is
1233 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001234 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001235 */
Marti Bolivarc50926f2017-06-14 09:35:40 -04001236 static boot_sector_t slot0_sectors[BOOT_MAX_IMG_SECTORS];
1237 static boot_sector_t slot1_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001238 boot_data.imgs[0].sectors = slot0_sectors;
1239 boot_data.imgs[1].sectors = slot1_sectors;
1240
Marti Bolivarc0b47912017-06-13 17:18:09 -04001241 /* Open boot_data image areas for the duration of this call. */
1242 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1243 fa_id = flash_area_id_from_image_slot(slot);
1244 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1245 assert(rc == 0);
1246 }
1247 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1248 &BOOT_SCRATCH_AREA(&boot_data));
1249 assert(rc == 0);
1250
Christopher Collins92ea77f2016-12-12 15:59:26 -08001251 /* Determine the sector layout of the image slots and scratch area. */
1252 rc = boot_read_sectors();
1253 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001254 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001255 }
1256
1257 /* Attempt to read an image header from each slot. */
1258 rc = boot_read_image_headers();
1259 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001260 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001261 }
1262
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001263 /* If the image slots aren't compatible, no swap is possible. Just boot
1264 * into slot 0.
1265 */
1266 if (boot_slots_compatible()) {
1267 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001268 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001269 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001270 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001271 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001272 } else {
1273 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001274 }
1275
1276 switch (swap_type) {
1277 case BOOT_SWAP_TYPE_NONE:
1278 slot = 0;
1279 break;
1280
1281 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001282 case BOOT_SWAP_TYPE_PERM:
Christopher Collins92ea77f2016-12-12 15:59:26 -08001283 slot = 1;
1284 boot_finalize_test_swap();
David Brown52eee562017-07-05 11:25:09 -06001285 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001286 break;
1287
1288 case BOOT_SWAP_TYPE_REVERT:
1289 slot = 1;
1290 boot_finalize_revert_swap();
David Brown52eee562017-07-05 11:25:09 -06001291 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001292 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();
David Brown52eee562017-07-05 11:25:09 -06001301 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001302 break;
1303
1304 default:
1305 assert(0);
1306 slot = 0;
1307 break;
1308 }
1309
David Brown554c52e2017-06-30 16:01:07 -06001310#ifdef MCUBOOT_VALIDATE_SLOT0
David Brown52eee562017-07-05 11:25:09 -06001311 if (reload_headers) {
1312 rc = boot_read_image_headers();
1313 if (rc != 0) {
1314 goto out;
1315 }
1316 }
1317
David Brown554c52e2017-06-30 16:01:07 -06001318 rc = boot_validate_slot(0);
1319 assert(rc == 0);
1320 if (rc != 0) {
1321 rc = BOOT_EBADIMAGE;
1322 goto out;
1323 }
1324#endif
1325
Christopher Collins92ea77f2016-12-12 15:59:26 -08001326 /* Always boot from the primary slot. */
Marti Bolivar428cdbf2017-05-01 22:32:42 -04001327 rsp->br_flash_dev_id = boot_img_fa_device_id(&boot_data, 0);
Marti Bolivar88f48d92017-05-01 22:30:02 -04001328 rsp->br_image_off = boot_img_slot_off(&boot_data, 0);
Marti Bolivarf804f622017-06-12 15:41:48 -04001329 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001330
Marti Bolivarc0b47912017-06-13 17:18:09 -04001331 out:
1332 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1333 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1334 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1335 }
1336 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001337}
1338
1339int
1340split_go(int loader_slot, int split_slot, void **entry)
1341{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001342 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001343 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001344 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001345 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001346 int rc;
1347
Christopher Collins92ea77f2016-12-12 15:59:26 -08001348 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1349 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001350 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001351 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001352 boot_data.imgs[loader_slot].sectors = sectors + 0;
1353 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1354
1355 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1356 rc = flash_area_open(loader_flash_id,
1357 &BOOT_IMG_AREA(&boot_data, split_slot));
1358 assert(rc == 0);
1359 split_flash_id = flash_area_id_from_image_slot(split_slot);
1360 rc = flash_area_open(split_flash_id,
1361 &BOOT_IMG_AREA(&boot_data, split_slot));
1362 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001363
1364 /* Determine the sector layout of the image slots and scratch area. */
1365 rc = boot_read_sectors();
1366 if (rc != 0) {
1367 rc = SPLIT_GO_ERR;
1368 goto done;
1369 }
1370
1371 rc = boot_read_image_headers();
1372 if (rc != 0) {
1373 goto done;
1374 }
1375
Christopher Collins92ea77f2016-12-12 15:59:26 -08001376 /* Don't check the bootable image flag because we could really call a
1377 * bootable or non-bootable image. Just validate that the image check
1378 * passes which is distinct from the normal check.
1379 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001380 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001381 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001382 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001383 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001384 if (rc != 0) {
1385 rc = SPLIT_GO_NON_MATCHING;
1386 goto done;
1387 }
1388
Marti Bolivarea088872017-06-12 17:10:49 -04001389 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001390 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001391 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001392 rc = SPLIT_GO_OK;
1393
1394done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001395 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1396 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001397 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001398 return rc;
1399}