blob: 205422da3e53b05d71409ab46252c153c969a6d4 [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
Marti Bolivarfd20c762017-02-07 16:52:50 -0500129#define BOOT_LOG_SWAP_STATE(area, state) \
130 BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \
131 (area), \
132 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
133 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
134 "bad"), \
135 (state)->copy_done, \
136 (state)->image_ok)
137
Christopher Collins92ea77f2016-12-12 15:59:26 -0800138/**
139 * Determines where in flash the most recent boot status is stored. The boot
140 * status is necessary for completing a swap that was interrupted by a boot
141 * loader reset.
142 *
143 * @return A BOOT_STATUS_SOURCE_[...] code indicating where * status should be read from.
144 */
145static int
146boot_status_source(void)
147{
148 const struct boot_status_table *table;
149 struct boot_swap_state state_scratch;
150 struct boot_swap_state state_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800151 int rc;
152 int i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500153 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800154
Fabio Utzig2473ac02017-05-02 12:45:02 -0300155 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800156 assert(rc == 0);
157
Fabio Utzig2473ac02017-05-02 12:45:02 -0300158 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800159 assert(rc == 0);
160
Marti Bolivarfd20c762017-02-07 16:52:50 -0500161 BOOT_LOG_SWAP_STATE("Image 0", &state_slot0);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500162 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
163
Christopher Collins92ea77f2016-12-12 15:59:26 -0800164 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300165 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800166
167 if ((table->bst_magic_slot0 == 0 ||
168 table->bst_magic_slot0 == state_slot0.magic) &&
169 (table->bst_magic_scratch == 0 ||
170 table->bst_magic_scratch == state_scratch.magic) &&
171 (table->bst_copy_done_slot0 == 0 ||
172 table->bst_copy_done_slot0 == state_slot0.copy_done)) {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500173 source = table->bst_status_source;
174 BOOT_LOG_INF("Boot source: %s",
175 source == BOOT_STATUS_SOURCE_NONE ? "none" :
176 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
177 source == BOOT_STATUS_SOURCE_SLOT0 ? "slot 0" :
178 "BUG; can't happen");
179 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800180 }
181 }
182
Marti Bolivarfd20c762017-02-07 16:52:50 -0500183 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800184 return BOOT_STATUS_SOURCE_NONE;
185}
186
187/**
188 * Calculates the type of swap that just completed.
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300189 *
190 * This is used when a swap is interrupted by an external event. After
191 * finishing the swap operation determines what the initial request was.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800192 */
193static int
194boot_previous_swap_type(void)
195{
196 int post_swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800197
198 post_swap_type = boot_swap_type();
199
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300200 switch (post_swap_type) {
201 case BOOT_SWAP_TYPE_NONE : return BOOT_SWAP_TYPE_PERM;
202 case BOOT_SWAP_TYPE_REVERT : return BOOT_SWAP_TYPE_TEST;
203 case BOOT_SWAP_TYPE_PANIC : return BOOT_SWAP_TYPE_PANIC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800204 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300205
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300206 return BOOT_SWAP_TYPE_FAIL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800207}
208
209static int
Fabio Utzigc08ed212017-06-20 19:28:36 -0300210boot_read_header_from_scratch(struct image_header *out_hdr)
211{
212 const struct flash_area *fap;
213 int rc;
214
215 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
216 if (rc != 0) {
217 return BOOT_EFLASH;
218 }
219
220 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
221 if (rc != 0) {
222 rc = BOOT_EFLASH;
223 }
224
225 flash_area_close(fap);
226 return rc;
227}
228
229static int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800230boot_read_image_header(int slot, struct image_header *out_hdr)
231{
232 const struct flash_area *fap;
233 int area_id;
234 int rc;
235
236 area_id = flash_area_id_from_image_slot(slot);
237 rc = flash_area_open(area_id, &fap);
238 if (rc != 0) {
239 rc = BOOT_EFLASH;
240 goto done;
241 }
242
243 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
244 if (rc != 0) {
245 rc = BOOT_EFLASH;
246 goto done;
247 }
248
249 rc = 0;
250
251done:
252 flash_area_close(fap);
253 return rc;
254}
255
256static int
257boot_read_image_headers(void)
258{
259 int rc;
260 int i;
261
262 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400263 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800264 if (rc != 0) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300265 /* If at least the first slot's header was read successfully, then
266 * the boot loader can attempt a boot. Failure to read any headers
267 * is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800268 */
269 if (i > 0) {
270 return 0;
271 } else {
272 return rc;
273 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800274 }
275 }
276
277 return 0;
278}
279
280static uint8_t
281boot_write_sz(void)
282{
283 uint8_t elem_sz;
284 uint8_t align;
285
286 /* Figure out what size to write update status update as. The size depends
287 * on what the minimum write size is for scratch area, active image slot.
288 * We need to use the bigger of those 2 values.
289 */
Marti Bolivare2587152017-06-12 15:52:05 -0400290 elem_sz = hal_flash_align(boot_img_fa_device_id(&boot_data, 0));
291 align = hal_flash_align(boot_scratch_fa_device_id(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800292 if (align > elem_sz) {
293 elem_sz = align;
294 }
295
296 return elem_sz;
297}
298
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800299static int
300boot_slots_compatible(void)
301{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400302 size_t num_sectors_0 = boot_img_num_sectors(&boot_data, 0);
303 size_t num_sectors_1 = boot_img_num_sectors(&boot_data, 1);
304 size_t size_0, size_1;
305 size_t i;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800306
307 /* Ensure both image slots have identical sector layouts. */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400308 if (num_sectors_0 != num_sectors_1) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800309 return 0;
310 }
Marti Bolivard3269fd2017-06-12 16:31:12 -0400311 for (i = 0; i < num_sectors_0; i++) {
312 size_0 = boot_img_sector_size(&boot_data, 0, i);
313 size_1 = boot_img_sector_size(&boot_data, 1, i);
314 if (size_0 != size_1) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800315 return 0;
316 }
317 }
318
319 return 1;
320}
321
Christopher Collins92ea77f2016-12-12 15:59:26 -0800322/**
323 * Determines the sector layout of both image slots and the scratch area.
324 * This information is necessary for calculating the number of bytes to erase
325 * and copy during an image swap. The information collected during this
326 * function is used to populate the boot_data global.
327 */
328static int
329boot_read_sectors(void)
330{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800331 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800332
Marti Bolivarcca28a92017-06-12 16:52:22 -0400333 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800334 if (rc != 0) {
335 return BOOT_EFLASH;
336 }
337
Marti Bolivarcca28a92017-06-12 16:52:22 -0400338 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800339 if (rc != 0) {
340 return BOOT_EFLASH;
341 }
342
Marti Bolivare10a7392017-06-14 16:20:07 -0400343 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800344
345 return 0;
346}
347
348static uint32_t
349boot_status_internal_off(int idx, int state, int elem_sz)
350{
351 int idx_sz;
352
353 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
354
355 return idx * idx_sz + state * elem_sz;
356}
357
358/**
359 * Reads the status of a partially-completed swap, if any. This is necessary
360 * to recover in case the boot lodaer was reset in the middle of a swap
361 * operation.
362 */
363static int
364boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
365{
366 uint32_t off;
367 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300368 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800369 int found;
370 int rc;
371 int i;
372
373 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400374 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300375
Christopher Collins92ea77f2016-12-12 15:59:26 -0800376 found = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300377 for (i = 0; i < max_entries; i++) {
Marti Bolivare10a7392017-06-14 16:20:07 -0400378 rc = flash_area_read(fap, off + i * BOOT_WRITE_SZ(&boot_data),
379 &status, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800380 if (rc != 0) {
381 return BOOT_EFLASH;
382 }
383
384 if (status == 0xff) {
385 if (found) {
386 break;
387 }
388 } else if (!found) {
389 found = 1;
390 }
391 }
392
393 if (found) {
394 i--;
Fabio Utzig94d998c2017-05-22 11:02:41 -0400395 bs->idx = i / BOOT_STATUS_STATE_COUNT;
396 bs->state = i % BOOT_STATUS_STATE_COUNT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800397 }
398
399 return 0;
400}
401
402/**
403 * Reads the boot status from the flash. The boot status contains
404 * the current state of an interrupted image copy operation. If the boot
405 * status is not present, or it indicates that previous copy finished,
406 * there is no operation in progress.
407 */
408static int
409boot_read_status(struct boot_status *bs)
410{
411 const struct flash_area *fap;
412 int status_loc;
413 int area_id;
414 int rc;
415
416 memset(bs, 0, sizeof *bs);
417
418 status_loc = boot_status_source();
419 switch (status_loc) {
420 case BOOT_STATUS_SOURCE_NONE:
421 return 0;
422
423 case BOOT_STATUS_SOURCE_SCRATCH:
424 area_id = FLASH_AREA_IMAGE_SCRATCH;
425 break;
426
427 case BOOT_STATUS_SOURCE_SLOT0:
428 area_id = FLASH_AREA_IMAGE_0;
429 break;
430
431 default:
432 assert(0);
433 return BOOT_EBADARGS;
434 }
435
436 rc = flash_area_open(area_id, &fap);
437 if (rc != 0) {
438 return BOOT_EFLASH;
439 }
440
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300441 return boot_read_status_bytes(fap, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800442}
443
444/**
445 * Writes the supplied boot status to the flash file system. The boot status
446 * contains the current state of an in-progress image copy operation.
447 *
448 * @param bs The boot status to write.
449 *
450 * @return 0 on success; nonzero on failure.
451 */
452int
453boot_write_status(struct boot_status *bs)
454{
455 const struct flash_area *fap;
456 uint32_t off;
457 int area_id;
458 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300459 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700460 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800461
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300462 /* NOTE: The first sector copied (that is the last sector on slot) contains
463 * the trailer. Since in the last step SLOT 0 is erased, the first
464 * two status writes go to the scratch which will be copied to SLOT 0!
465 */
466
Fabio Utzig2473ac02017-05-02 12:45:02 -0300467 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800468 /* Write to scratch. */
469 area_id = FLASH_AREA_IMAGE_SCRATCH;
470 } else {
471 /* Write to slot 0. */
472 area_id = FLASH_AREA_IMAGE_0;
473 }
474
475 rc = flash_area_open(area_id, &fap);
476 if (rc != 0) {
477 rc = BOOT_EFLASH;
478 goto done;
479 }
480
481 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400482 boot_status_internal_off(bs->idx, bs->state,
483 BOOT_WRITE_SZ(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800484
David Brown9d725462017-01-23 15:50:58 -0700485 align = hal_flash_align(fap->fa_device_id);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300486 memset(buf, 0xFF, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700487 buf[0] = bs->state;
488
489 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800490 if (rc != 0) {
491 rc = BOOT_EFLASH;
492 goto done;
493 }
494
495 rc = 0;
496
497done:
498 flash_area_close(fap);
499 return rc;
500}
501
502/*
503 * Validate image hash/signature in a slot.
504 */
505static int
506boot_image_check(struct image_header *hdr, const struct flash_area *fap)
507{
David Browndb1d9d32017-01-06 11:07:54 -0700508 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800509
Christopher Collins92ea77f2016-12-12 15:59:26 -0800510 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
511 NULL, 0, NULL)) {
512 return BOOT_EBADIMAGE;
513 }
514 return 0;
515}
516
517static int
518split_image_check(struct image_header *app_hdr,
519 const struct flash_area *app_fap,
520 struct image_header *loader_hdr,
521 const struct flash_area *loader_fap)
522{
523 static void *tmpbuf;
524 uint8_t loader_hash[32];
525
526 if (!tmpbuf) {
527 tmpbuf = malloc(BOOT_TMPBUF_SZ);
528 if (!tmpbuf) {
529 return BOOT_ENOMEM;
530 }
531 }
532
533 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
534 NULL, 0, loader_hash)) {
535 return BOOT_EBADIMAGE;
536 }
537
538 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
539 loader_hash, 32, NULL)) {
540 return BOOT_EBADIMAGE;
541 }
542
543 return 0;
544}
545
546static int
David Brownd930ec62016-12-14 07:59:48 -0700547boot_validate_slot(int slot)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800548{
549 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400550 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800551 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300552
Marti Bolivarf804f622017-06-12 15:41:48 -0400553 hdr = boot_img_hdr(&boot_data, slot);
554 if (hdr->ih_magic == 0xffffffff || hdr->ih_flags & IMAGE_F_NON_BOOTABLE) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300555 /* No bootable image in slot; continue booting from slot 0. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800556 return -1;
557 }
558
David Brownd930ec62016-12-14 07:59:48 -0700559 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800560 if (rc != 0) {
561 return BOOT_EFLASH;
562 }
563
Marti Bolivarf804f622017-06-12 15:41:48 -0400564 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap) != 0)) {
David Brownb38e0442017-02-24 13:57:12 -0700565 if (slot != 0) {
566 flash_area_erase(fap, 0, fap->fa_size);
567 /* Image in slot 1 is invalid. Erase the image and
568 * continue booting from slot 0.
569 */
570 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800571 return -1;
572 }
573
574 flash_area_close(fap);
575
576 /* Image in slot 1 is valid. */
577 return 0;
578}
579
580/**
581 * Determines which swap operation to perform, if any. If it is determined
582 * that a swap operation is required, the image in the second slot is checked
583 * for validity. If the image in the second slot is invalid, it is erased, and
584 * a swap type of "none" is indicated.
585 *
586 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
587 */
588static int
589boot_validated_swap_type(void)
590{
591 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800592
593 swap_type = boot_swap_type();
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300594 switch (swap_type) {
595 case BOOT_SWAP_TYPE_TEST:
596 case BOOT_SWAP_TYPE_PERM:
597 case BOOT_SWAP_TYPE_REVERT:
598 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
599 if (boot_validate_slot(1) != 0) {
600 swap_type = BOOT_SWAP_TYPE_FAIL;
601 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800602 }
603
604 return swap_type;
605}
606
607/**
608 * Calculates the number of sectors the scratch area can contain. A "last"
609 * source sector is specified because images are copied backwards in flash
610 * (final index to index number 0).
611 *
612 * @param last_sector_idx The index of the last source sector
613 * (inclusive).
614 * @param out_first_sector_idx The index of the first source sector
615 * (inclusive) gets written here.
616 *
617 * @return The number of bytes comprised by the
618 * [first-sector, last-sector] range.
619 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300620#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800621static uint32_t
622boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
623{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400624 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800625 uint32_t new_sz;
626 uint32_t sz;
627 int i;
628
629 sz = 0;
630
Marti Bolivard3269fd2017-06-12 16:31:12 -0400631 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800632 for (i = last_sector_idx; i >= 0; i--) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400633 new_sz = sz + boot_img_sector_size(&boot_data, 0, i);
634 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800635 break;
636 }
637 sz = new_sz;
638 }
639
640 /* i currently refers to a sector that doesn't fit or it is -1 because all
641 * sectors have been processed. In both cases, exclude sector i.
642 */
643 *out_first_sector_idx = i + 1;
644 return sz;
645}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300646#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800647
648/**
649 * Erases a region of flash.
650 *
651 * @param flash_area_idx The ID of the flash area containing the region
652 * to erase.
653 * @param off The offset within the flash area to start the
654 * erase.
655 * @param sz The number of bytes to erase.
656 *
657 * @return 0 on success; nonzero on failure.
658 */
659static int
660boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
661{
662 const struct flash_area *fap;
663 int rc;
664
665 rc = flash_area_open(flash_area_id, &fap);
666 if (rc != 0) {
667 rc = BOOT_EFLASH;
668 goto done;
669 }
670
671 rc = flash_area_erase(fap, off, sz);
672 if (rc != 0) {
673 rc = BOOT_EFLASH;
674 goto done;
675 }
676
677 rc = 0;
678
679done:
680 flash_area_close(fap);
681 return rc;
682}
683
684/**
685 * Copies the contents of one flash region to another. You must erase the
686 * destination region prior to calling this function.
687 *
688 * @param flash_area_id_src The ID of the source flash area.
689 * @param flash_area_id_dst The ID of the destination flash area.
690 * @param off_src The offset within the source flash area to
691 * copy from.
692 * @param off_dst The offset within the destination flash area to
693 * copy to.
694 * @param sz The number of bytes to copy.
695 *
696 * @return 0 on success; nonzero on failure.
697 */
698static int
699boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
700 uint32_t off_src, uint32_t off_dst, uint32_t sz)
701{
702 const struct flash_area *fap_src;
703 const struct flash_area *fap_dst;
704 uint32_t bytes_copied;
705 int chunk_sz;
706 int rc;
707
708 static uint8_t buf[1024];
709
710 fap_src = NULL;
711 fap_dst = NULL;
712
713 rc = flash_area_open(flash_area_id_src, &fap_src);
714 if (rc != 0) {
715 rc = BOOT_EFLASH;
716 goto done;
717 }
718
719 rc = flash_area_open(flash_area_id_dst, &fap_dst);
720 if (rc != 0) {
721 rc = BOOT_EFLASH;
722 goto done;
723 }
724
725 bytes_copied = 0;
726 while (bytes_copied < sz) {
727 if (sz - bytes_copied > sizeof buf) {
728 chunk_sz = sizeof buf;
729 } else {
730 chunk_sz = sz - bytes_copied;
731 }
732
733 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
734 if (rc != 0) {
735 rc = BOOT_EFLASH;
736 goto done;
737 }
738
739 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
740 if (rc != 0) {
741 rc = BOOT_EFLASH;
742 goto done;
743 }
744
745 bytes_copied += chunk_sz;
746 }
747
748 rc = 0;
749
750done:
Fabio Utzige7686262017-06-28 09:26:54 -0300751 if (fap_src) {
752 flash_area_close(fap_src);
753 }
754 if (fap_dst) {
755 flash_area_close(fap_dst);
756 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800757 return rc;
758}
759
Fabio Utzig2473ac02017-05-02 12:45:02 -0300760static inline int
761boot_status_init_by_id(int flash_area_id)
762{
763 const struct flash_area *fap;
764 struct boot_swap_state swap_state;
765 int rc;
766
767 rc = flash_area_open(flash_area_id, &fap);
768 assert(rc == 0);
769
770 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
771 assert(rc == 0);
772
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400773 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300774 rc = boot_write_image_ok(fap);
775 assert(rc == 0);
776 }
777
778 rc = boot_write_magic(fap);
779 assert(rc == 0);
780
781 flash_area_close(fap);
782
783 return 0;
784}
785
786static int
787boot_erase_last_sector_by_id(int flash_area_id)
788{
789 uint8_t slot;
790 uint32_t last_sector;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300791 int rc;
792
793 switch (flash_area_id) {
794 case FLASH_AREA_IMAGE_0:
795 slot = 0;
796 break;
797 case FLASH_AREA_IMAGE_1:
798 slot = 1;
799 break;
800 default:
801 return BOOT_EFLASH;
802 }
803
Marti Bolivard3269fd2017-06-12 16:31:12 -0400804 last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300805 rc = boot_erase_sector(flash_area_id,
Marti Bolivarea088872017-06-12 17:10:49 -0400806 boot_img_sector_off(&boot_data, slot, last_sector),
Marti Bolivard3269fd2017-06-12 16:31:12 -0400807 boot_img_sector_size(&boot_data, slot, last_sector));
Fabio Utzig2473ac02017-05-02 12:45:02 -0300808 assert(rc == 0);
809
810 return rc;
811}
812
Christopher Collins92ea77f2016-12-12 15:59:26 -0800813/**
814 * Swaps the contents of two flash regions within the two image slots.
815 *
816 * @param idx The index of the first sector in the range of
817 * sectors being swapped.
818 * @param sz The number of bytes to swap.
819 * @param bs The current boot status. This struct gets
820 * updated according to the outcome.
821 *
822 * @return 0 on success; nonzero on failure.
823 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300824#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800825static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800826boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
827{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300828 const struct flash_area *fap;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800829 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300830 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800831 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300832 uint32_t scratch_trailer_off;
833 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400834 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800835 int rc;
836
837 /* Calculate offset from start of image area. */
Marti Bolivarea088872017-06-12 17:10:49 -0400838 img_off = boot_img_sector_off(&boot_data, 0, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800839
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300840 copy_sz = sz;
Marti Bolivare10a7392017-06-14 16:20:07 -0400841 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -0400842
843 /* sz in this function is always is always sized on a multiple of the
Marti Bolivarea088872017-06-12 17:10:49 -0400844 * sector size. The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -0400845 * is to determine if we're swapping the last sector. The last sector
846 * needs special handling because it's where the trailer lives. If we're
847 * copying it, we need to use scratch to write the trailer temporarily.
848 *
849 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
850 * controls if special handling is needed (swapping last sector).
851 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400852 last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
Marti Bolivarea088872017-06-12 17:10:49 -0400853 if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300854 copy_sz -= trailer_sz;
855 }
856
Fabio Utzig2473ac02017-05-02 12:45:02 -0300857 bs->use_scratch = (bs->idx == 0 && copy_sz != sz);
858
Christopher Collins92ea77f2016-12-12 15:59:26 -0800859 if (bs->state == 0) {
860 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800861 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800862
863 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300864 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800865 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800866
Fabio Utzig2473ac02017-05-02 12:45:02 -0300867 if (bs->idx == 0) {
868 if (bs->use_scratch) {
869 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH);
870 } else {
871 /* Prepare the status area... here it is known that the
872 * last sector is not being used by the image data so it's
873 * safe to erase.
874 */
875 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300876 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300877
878 boot_status_init_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300879 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300880 }
881
Christopher Collins92ea77f2016-12-12 15:59:26 -0800882 bs->state = 1;
Christopher Collins4772ac42017-02-27 20:08:01 -0800883 rc = boot_write_status(bs);
884 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800885 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300886
Christopher Collins92ea77f2016-12-12 15:59:26 -0800887 if (bs->state == 1) {
888 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800889 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800890
Christopher Collins92ea77f2016-12-12 15:59:26 -0800891 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
892 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800893 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800894
Fabio Utzig2473ac02017-05-02 12:45:02 -0300895 if (bs->idx == 0 && !bs->use_scratch) {
896 /* If not all sectors of the slot are being swapped,
897 * guarantee here that only slot0 will have the state.
898 */
899 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1);
900 assert(rc == 0);
901 }
902
Christopher Collins92ea77f2016-12-12 15:59:26 -0800903 bs->state = 2;
Christopher Collins4772ac42017-02-27 20:08:01 -0800904 rc = boot_write_status(bs);
905 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800906 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300907
Christopher Collins92ea77f2016-12-12 15:59:26 -0800908 if (bs->state == 2) {
909 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800910 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800911
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300912 /* NOTE: also copy trailer from scratch (has status info) */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800913 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300914 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800915 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800916
Fabio Utzig94d998c2017-05-22 11:02:41 -0400917 if (bs->use_scratch) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300918 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
919 assert(rc == 0);
920
921 scratch_trailer_off = boot_status_off(fap);
922
923 flash_area_close(fap);
924
925 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
926 assert(rc == 0);
927
928 /* copy current status that is being maintained in scratch */
929 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Marti Bolivare10a7392017-06-14 16:20:07 -0400930 scratch_trailer_off,
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300931 img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -0400932 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300933 assert(rc == 0);
934
Fabio Utzig2473ac02017-05-02 12:45:02 -0300935 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
936 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300937 assert(rc == 0);
938
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400939 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300940 rc = boot_write_image_ok(fap);
941 assert(rc == 0);
942 }
943
944 rc = boot_write_magic(fap);
945 assert(rc == 0);
946
947 flash_area_close(fap);
948 }
949
Christopher Collins92ea77f2016-12-12 15:59:26 -0800950 bs->idx++;
951 bs->state = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300952 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -0800953 rc = boot_write_status(bs);
954 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800955 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800956}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300957#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800958
959/**
960 * Swaps the two images in flash. If a prior copy operation was interrupted
961 * by a system reset, this function completes that operation.
962 *
963 * @param bs The current boot status. This function reads
964 * this struct to determine if it is resuming
965 * an interrupted swap operation. This
966 * function writes the updated status to this
967 * function on return.
968 *
969 * @return 0 on success; nonzero on failure.
970 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300971#ifdef MCUBOOT_OVERWRITE_ONLY
David Brown17609d82017-05-05 09:41:34 -0600972static int
973boot_copy_image(struct boot_status *bs)
974{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400975 size_t sect_count;
976 size_t sect;
David Brown17609d82017-05-05 09:41:34 -0600977 int rc;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400978 size_t size = 0;
979 size_t this_size;
David Brown17609d82017-05-05 09:41:34 -0600980
981 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
982 BOOT_LOG_INF("Erasing slot0");
983
Marti Bolivard3269fd2017-06-12 16:31:12 -0400984 sect_count = boot_img_num_sectors(&boot_data, 0);
David Brown17609d82017-05-05 09:41:34 -0600985 for (sect = 0; sect < sect_count; sect++) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400986 this_size = boot_img_sector_size(&boot_data, 0, sect);
David Brown17609d82017-05-05 09:41:34 -0600987 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
988 size,
989 this_size);
990 assert(rc == 0);
991
992 size += this_size;
993 }
994
995 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%x bytes",
996 size);
997 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
998 0, 0, size);
999
1000 /* Erase slot 1 so that we don't do the upgrade on every boot.
1001 * TODO: Perhaps verify slot 0's signature again? */
1002 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
Marti Bolivard3269fd2017-06-12 16:31:12 -04001003 0, boot_img_sector_size(&boot_data, 1, 0));
David Brown17609d82017-05-05 09:41:34 -06001004 assert(rc == 0);
1005
1006 return 0;
1007}
1008#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001009static int
1010boot_copy_image(struct boot_status *bs)
1011{
1012 uint32_t sz;
1013 int first_sector_idx;
1014 int last_sector_idx;
1015 int swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001016 struct image_header *hdr;
1017 uint32_t size;
1018 uint32_t copy_size;
1019 struct image_header tmp_hdr;
1020 int rc;
1021
1022 /* FIXME: just do this if asked by user? */
1023
1024 size = copy_size = 0;
1025
Marti Bolivarf804f622017-06-12 15:41:48 -04001026 hdr = boot_img_hdr(&boot_data, 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001027 if (hdr->ih_magic == IMAGE_MAGIC) {
1028 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1029 }
1030
Marti Bolivarf804f622017-06-12 15:41:48 -04001031 hdr = boot_img_hdr(&boot_data, 1);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001032 if (hdr->ih_magic == IMAGE_MAGIC) {
1033 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1034 }
1035
1036 if (!size || !copy_size || size == copy_size) {
Fabio Utzigc08ed212017-06-20 19:28:36 -03001037 rc = boot_read_header_from_scratch(&tmp_hdr);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001038 assert(rc == 0);
1039
1040 hdr = &tmp_hdr;
1041 if (hdr->ih_magic == IMAGE_MAGIC) {
1042 if (!size) {
1043 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1044 } else {
1045 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1046 }
1047 }
1048 }
1049
1050 if (size > copy_size) {
1051 copy_size = size;
1052 }
1053
1054 size = 0;
1055 last_sector_idx = 0;
1056 while (1) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001057 size += boot_img_sector_size(&boot_data, 0, last_sector_idx);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001058 if (size >= copy_size) {
1059 break;
1060 }
1061 last_sector_idx++;
1062 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001063
1064 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001065 while (last_sector_idx >= 0) {
1066 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
1067 if (swap_idx >= bs->idx) {
1068 boot_swap_sectors(first_sector_idx, sz, bs);
1069 }
1070
1071 last_sector_idx = first_sector_idx - 1;
1072 swap_idx++;
1073 }
1074
1075 return 0;
1076}
David Brown17609d82017-05-05 09:41:34 -06001077#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001078
1079/**
1080 * Marks a test image in slot 0 as fully copied.
1081 */
1082static int
1083boot_finalize_test_swap(void)
1084{
1085 const struct flash_area *fap;
1086 int rc;
1087
1088 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1089 if (rc != 0) {
1090 return BOOT_EFLASH;
1091 }
1092
1093 rc = boot_write_copy_done(fap);
1094 if (rc != 0) {
1095 return rc;
1096 }
1097
1098 return 0;
1099}
1100
1101/**
1102 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1103 * the status bytes from the image revert operation don't get processed on a
1104 * subsequent boot.
1105 */
1106static int
1107boot_finalize_revert_swap(void)
1108{
1109 const struct flash_area *fap;
1110 struct boot_swap_state state_slot0;
1111 int rc;
1112
1113 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1114 if (rc != 0) {
1115 return BOOT_EFLASH;
1116 }
1117
1118 rc = boot_read_swap_state(fap, &state_slot0);
1119 if (rc != 0) {
1120 return BOOT_EFLASH;
1121 }
1122
1123 if (state_slot0.magic == BOOT_MAGIC_UNSET) {
1124 rc = boot_write_magic(fap);
1125 if (rc != 0) {
1126 return rc;
1127 }
1128 }
1129
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001130 if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001131 rc = boot_write_copy_done(fap);
1132 if (rc != 0) {
1133 return rc;
1134 }
1135 }
1136
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001137 if (state_slot0.image_ok == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001138 rc = boot_write_image_ok(fap);
1139 if (rc != 0) {
1140 return rc;
1141 }
1142 }
1143
1144 return 0;
1145}
1146
1147/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001148 * Performs an image swap if one is required.
1149 *
1150 * @param out_swap_type On success, the type of swap performed gets
1151 * written here.
1152 *
1153 * @return 0 on success; nonzero on failure.
1154 */
1155static int
1156boot_swap_if_needed(int *out_swap_type)
1157{
1158 struct boot_status bs;
1159 int swap_type;
1160 int rc;
1161
1162 /* Determine if we rebooted in the middle of an image swap
1163 * operation.
1164 */
1165 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001166 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001167 if (rc != 0) {
1168 return rc;
1169 }
1170
1171 /* If a partial swap was detected, complete it. */
1172 if (bs.idx != 0 || bs.state != 0) {
1173 rc = boot_copy_image(&bs);
1174 assert(rc == 0);
1175
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001176 /* NOTE: here we have finished a swap resume. The initial request
1177 * was either a TEST or PERM swap, which now after the completed
1178 * swap will be determined to be respectively REVERT (was TEST)
1179 * or NONE (was PERM).
1180 */
1181
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001182 /* Extrapolate the type of the partial swap. We need this
1183 * information to know how to mark the swap complete in flash.
1184 */
1185 swap_type = boot_previous_swap_type();
1186 } else {
1187 swap_type = boot_validated_swap_type();
1188 switch (swap_type) {
1189 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001190 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001191 case BOOT_SWAP_TYPE_REVERT:
1192 rc = boot_copy_image(&bs);
1193 assert(rc == 0);
1194 break;
1195 }
1196 }
1197
1198 *out_swap_type = swap_type;
1199 return 0;
1200}
1201
1202/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001203 * Prepares the booting process. This function moves images around in flash as
1204 * appropriate, and tells you what address to boot from.
1205 *
1206 * @param rsp On success, indicates how booting should occur.
1207 *
1208 * @return 0 on success; nonzero on failure.
1209 */
1210int
1211boot_go(struct boot_rsp *rsp)
1212{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001213 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001214 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001215 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001216 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001217 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001218
1219 /* The array of slot sectors are defined here (as opposed to file scope) so
1220 * that they don't get allocated for non-boot-loader apps. This is
1221 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001222 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001223 */
Marti Bolivarc50926f2017-06-14 09:35:40 -04001224 static boot_sector_t slot0_sectors[BOOT_MAX_IMG_SECTORS];
1225 static boot_sector_t slot1_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001226 boot_data.imgs[0].sectors = slot0_sectors;
1227 boot_data.imgs[1].sectors = slot1_sectors;
1228
Marti Bolivarc0b47912017-06-13 17:18:09 -04001229 /* Open boot_data image areas for the duration of this call. */
1230 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1231 fa_id = flash_area_id_from_image_slot(slot);
1232 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1233 assert(rc == 0);
1234 }
1235 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1236 &BOOT_SCRATCH_AREA(&boot_data));
1237 assert(rc == 0);
1238
Christopher Collins92ea77f2016-12-12 15:59:26 -08001239 /* Determine the sector layout of the image slots and scratch area. */
1240 rc = boot_read_sectors();
1241 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001242 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001243 }
1244
1245 /* Attempt to read an image header from each slot. */
1246 rc = boot_read_image_headers();
1247 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001248 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001249 }
1250
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001251 /* If the image slots aren't compatible, no swap is possible. Just boot
1252 * into slot 0.
1253 */
1254 if (boot_slots_compatible()) {
1255 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001256 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001257 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001258 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001259 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001260 } else {
1261 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001262 }
1263
1264 switch (swap_type) {
1265 case BOOT_SWAP_TYPE_NONE:
1266 slot = 0;
1267 break;
1268
1269 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001270 case BOOT_SWAP_TYPE_PERM:
Christopher Collins92ea77f2016-12-12 15:59:26 -08001271 slot = 1;
1272 boot_finalize_test_swap();
David Brown52eee562017-07-05 11:25:09 -06001273 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001274 break;
1275
1276 case BOOT_SWAP_TYPE_REVERT:
1277 slot = 1;
1278 boot_finalize_revert_swap();
David Brown52eee562017-07-05 11:25:09 -06001279 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001280 break;
1281
1282 case BOOT_SWAP_TYPE_FAIL:
1283 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1284 * try to boot into it again on the next reboot. Do this by pretending
1285 * we just reverted back to slot 0.
1286 */
1287 slot = 0;
1288 boot_finalize_revert_swap();
David Brown52eee562017-07-05 11:25:09 -06001289 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001290 break;
1291
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001292 case BOOT_SWAP_TYPE_PANIC:
1293 /* TODO: what to do it a fatal error like flash read/write error
1294 * happened?
1295 */
1296 assert(0);
1297 break;
1298
Christopher Collins92ea77f2016-12-12 15:59:26 -08001299 default:
1300 assert(0);
1301 slot = 0;
1302 break;
1303 }
1304
David Brown554c52e2017-06-30 16:01:07 -06001305#ifdef MCUBOOT_VALIDATE_SLOT0
David Brown52eee562017-07-05 11:25:09 -06001306 if (reload_headers) {
1307 rc = boot_read_image_headers();
1308 if (rc != 0) {
1309 goto out;
1310 }
1311 }
1312
David Brown554c52e2017-06-30 16:01:07 -06001313 rc = boot_validate_slot(0);
1314 assert(rc == 0);
1315 if (rc != 0) {
1316 rc = BOOT_EBADIMAGE;
1317 goto out;
1318 }
1319#endif
1320
Christopher Collins92ea77f2016-12-12 15:59:26 -08001321 /* Always boot from the primary slot. */
Marti Bolivar428cdbf2017-05-01 22:32:42 -04001322 rsp->br_flash_dev_id = boot_img_fa_device_id(&boot_data, 0);
Marti Bolivar88f48d92017-05-01 22:30:02 -04001323 rsp->br_image_off = boot_img_slot_off(&boot_data, 0);
Marti Bolivarf804f622017-06-12 15:41:48 -04001324 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001325
Marti Bolivarc0b47912017-06-13 17:18:09 -04001326 out:
1327 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1328 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1329 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1330 }
1331 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001332}
1333
1334int
1335split_go(int loader_slot, int split_slot, void **entry)
1336{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001337 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001338 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001339 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001340 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001341 int rc;
1342
Christopher Collins92ea77f2016-12-12 15:59:26 -08001343 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1344 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001345 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001346 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001347 boot_data.imgs[loader_slot].sectors = sectors + 0;
1348 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1349
1350 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1351 rc = flash_area_open(loader_flash_id,
1352 &BOOT_IMG_AREA(&boot_data, split_slot));
1353 assert(rc == 0);
1354 split_flash_id = flash_area_id_from_image_slot(split_slot);
1355 rc = flash_area_open(split_flash_id,
1356 &BOOT_IMG_AREA(&boot_data, split_slot));
1357 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001358
1359 /* Determine the sector layout of the image slots and scratch area. */
1360 rc = boot_read_sectors();
1361 if (rc != 0) {
1362 rc = SPLIT_GO_ERR;
1363 goto done;
1364 }
1365
1366 rc = boot_read_image_headers();
1367 if (rc != 0) {
1368 goto done;
1369 }
1370
Christopher Collins92ea77f2016-12-12 15:59:26 -08001371 /* Don't check the bootable image flag because we could really call a
1372 * bootable or non-bootable image. Just validate that the image check
1373 * passes which is distinct from the normal check.
1374 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001375 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001376 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001377 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001378 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001379 if (rc != 0) {
1380 rc = SPLIT_GO_NON_MATCHING;
1381 goto done;
1382 }
1383
Marti Bolivarea088872017-06-12 17:10:49 -04001384 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001385 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001386 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001387 rc = SPLIT_GO_OK;
1388
1389done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001390 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1391 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001392 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001393 return rc;
1394}