blob: c0ecda26b7b19093d98d5ceb70bd05051d0cd6e3 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/**
21 * This file provides an interface to the boot loader. Functions defined in
22 * this file should only be called while the boot loader is running.
23 */
24
25#include <assert.h>
26#include <stddef.h>
27#include <inttypes.h>
28#include <stdlib.h>
29#include <string.h>
30#include "sysflash/sysflash.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
Christopher Collins92ea77f2016-12-12 15:59:26 -080044#define BOOT_MAX_IMG_SECTORS 120
45
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040046static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080047
48struct boot_status_table {
49 /**
50 * For each field, a value of 0 means "any".
51 */
52 uint8_t bst_magic_slot0;
53 uint8_t bst_magic_scratch;
54 uint8_t bst_copy_done_slot0;
55 uint8_t bst_status_source;
56};
57
58/**
59 * This set of tables maps swap state contents to boot status location.
60 * When searching for a match, these tables must be iterated in order.
61 */
62static const struct boot_status_table boot_status_tables[] = {
63 {
64 /* | slot-0 | scratch |
65 * ----------+------------+------------|
66 * magic | Good | Any |
67 * copy-done | 0x01 | N/A |
68 * ----------+------------+------------'
69 * source: none |
70 * ------------------------------------'
71 */
72 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
73 .bst_magic_scratch = 0,
74 .bst_copy_done_slot0 = 0x01,
75 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
76 },
77
78 {
79 /* | slot-0 | scratch |
80 * ----------+------------+------------|
81 * magic | Good | Any |
82 * copy-done | 0xff | N/A |
83 * ----------+------------+------------'
84 * source: slot 0 |
85 * ------------------------------------'
86 */
87 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
88 .bst_magic_scratch = 0,
89 .bst_copy_done_slot0 = 0xff,
90 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
91 },
92
93 {
94 /* | slot-0 | scratch |
95 * ----------+------------+------------|
96 * magic | Any | Good |
97 * copy-done | Any | N/A |
98 * ----------+------------+------------'
99 * source: scratch |
100 * ------------------------------------'
101 */
102 .bst_magic_slot0 = 0,
103 .bst_magic_scratch = BOOT_MAGIC_GOOD,
104 .bst_copy_done_slot0 = 0,
105 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
106 },
107
108 {
109 /* | slot-0 | scratch |
110 * ----------+------------+------------|
111 * magic | Unset | Any |
112 * copy-done | 0xff | N/A |
113 * ----------+------------+------------|
114 * source: varies |
115 * ------------------------------------+------------------------------+
116 * This represents one of two cases: |
117 * o No swaps ever (no status to read, so no harm in checking). |
118 * o Mid-revert; status in slot 0. |
119 * -------------------------------------------------------------------'
120 */
121 .bst_magic_slot0 = BOOT_MAGIC_UNSET,
122 .bst_magic_scratch = 0,
123 .bst_copy_done_slot0 = 0xff,
124 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
125 },
126};
127
128#define BOOT_STATUS_TABLES_COUNT \
129 (sizeof boot_status_tables / sizeof boot_status_tables[0])
130
131/**
132 * This table indicates the next swap type that should be performed. The first
133 * column contains the current swap type. The second column contains the swap
134 * type that should be effected after the first completes.
135 */
136static const uint8_t boot_swap_trans_table[][2] = {
137 /* From To */
138 { BOOT_SWAP_TYPE_REVERT, BOOT_SWAP_TYPE_NONE },
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800139 { BOOT_SWAP_TYPE_PERM, BOOT_SWAP_TYPE_NONE },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800140 { BOOT_SWAP_TYPE_TEST, BOOT_SWAP_TYPE_REVERT },
141};
142
143#define BOOT_SWAP_TRANS_TABLE_SIZE \
144 (sizeof boot_swap_trans_table / sizeof boot_swap_trans_table[0])
145
Marti Bolivarfd20c762017-02-07 16:52:50 -0500146#define BOOT_LOG_SWAP_STATE(area, state) \
147 BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \
148 (area), \
149 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
150 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
151 "bad"), \
152 (state)->copy_done, \
153 (state)->image_ok)
154
Christopher Collins92ea77f2016-12-12 15:59:26 -0800155/**
156 * Determines where in flash the most recent boot status is stored. The boot
157 * status is necessary for completing a swap that was interrupted by a boot
158 * loader reset.
159 *
160 * @return A BOOT_STATUS_SOURCE_[...] code indicating where * status should be read from.
161 */
162static int
163boot_status_source(void)
164{
165 const struct boot_status_table *table;
166 struct boot_swap_state state_scratch;
167 struct boot_swap_state state_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800168 int rc;
169 int i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500170 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800171
Fabio Utzig2473ac02017-05-02 12:45:02 -0300172 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800173 assert(rc == 0);
174
Fabio Utzig2473ac02017-05-02 12:45:02 -0300175 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800176 assert(rc == 0);
177
Marti Bolivarfd20c762017-02-07 16:52:50 -0500178 BOOT_LOG_SWAP_STATE("Image 0", &state_slot0);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500179 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
180
Christopher Collins92ea77f2016-12-12 15:59:26 -0800181 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300182 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800183
184 if ((table->bst_magic_slot0 == 0 ||
185 table->bst_magic_slot0 == state_slot0.magic) &&
186 (table->bst_magic_scratch == 0 ||
187 table->bst_magic_scratch == state_scratch.magic) &&
188 (table->bst_copy_done_slot0 == 0 ||
189 table->bst_copy_done_slot0 == state_slot0.copy_done)) {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500190 source = table->bst_status_source;
191 BOOT_LOG_INF("Boot source: %s",
192 source == BOOT_STATUS_SOURCE_NONE ? "none" :
193 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
194 source == BOOT_STATUS_SOURCE_SLOT0 ? "slot 0" :
195 "BUG; can't happen");
196 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800197 }
198 }
199
Marti Bolivarfd20c762017-02-07 16:52:50 -0500200 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800201 return BOOT_STATUS_SOURCE_NONE;
202}
203
204/**
205 * Calculates the type of swap that just completed.
206 */
207static int
208boot_previous_swap_type(void)
209{
210 int post_swap_type;
211 int i;
212
213 post_swap_type = boot_swap_type();
214
215 for (i = 0; i < BOOT_SWAP_TRANS_TABLE_SIZE; i++){
216 if (boot_swap_trans_table[i][1] == post_swap_type) {
217 return boot_swap_trans_table[i][0];
218 }
219 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300220
Christopher Collins92ea77f2016-12-12 15:59:26 -0800221 /* XXX: Temporary assert. */
222 assert(0);
223
224 return BOOT_SWAP_TYPE_REVERT;
225}
226
227static int
228boot_read_image_header(int slot, struct image_header *out_hdr)
229{
230 const struct flash_area *fap;
231 int area_id;
232 int rc;
233
234 area_id = flash_area_id_from_image_slot(slot);
235 rc = flash_area_open(area_id, &fap);
236 if (rc != 0) {
237 rc = BOOT_EFLASH;
238 goto done;
239 }
240
241 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
242 if (rc != 0) {
243 rc = BOOT_EFLASH;
244 goto done;
245 }
246
247 rc = 0;
248
249done:
250 flash_area_close(fap);
251 return rc;
252}
253
254static int
255boot_read_image_headers(void)
256{
257 int rc;
258 int i;
259
260 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400261 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800262 if (rc != 0) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300263 /* If at least the first slot's header was read successfully, then
264 * the boot loader can attempt a boot. Failure to read any headers
265 * is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800266 */
267 if (i > 0) {
268 return 0;
269 } else {
270 return rc;
271 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800272 }
273 }
274
275 return 0;
276}
277
278static uint8_t
279boot_write_sz(void)
280{
281 uint8_t elem_sz;
282 uint8_t align;
283
284 /* Figure out what size to write update status update as. The size depends
285 * on what the minimum write size is for scratch area, active image slot.
286 * We need to use the bigger of those 2 values.
287 */
Marti Bolivare2587152017-06-12 15:52:05 -0400288 elem_sz = hal_flash_align(boot_img_fa_device_id(&boot_data, 0));
289 align = hal_flash_align(boot_scratch_fa_device_id(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800290 if (align > elem_sz) {
291 elem_sz = align;
292 }
293
294 return elem_sz;
295}
296
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800297static int
298boot_slots_compatible(void)
299{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400300 size_t num_sectors_0 = boot_img_num_sectors(&boot_data, 0);
301 size_t num_sectors_1 = boot_img_num_sectors(&boot_data, 1);
302 size_t size_0, size_1;
303 size_t i;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800304
305 /* Ensure both image slots have identical sector layouts. */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400306 if (num_sectors_0 != num_sectors_1) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800307 return 0;
308 }
Marti Bolivard3269fd2017-06-12 16:31:12 -0400309 for (i = 0; i < num_sectors_0; i++) {
310 size_0 = boot_img_sector_size(&boot_data, 0, i);
311 size_1 = boot_img_sector_size(&boot_data, 1, i);
312 if (size_0 != size_1) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800313 return 0;
314 }
315 }
316
317 return 1;
318}
319
Christopher Collins92ea77f2016-12-12 15:59:26 -0800320/**
321 * Determines the sector layout of both image slots and the scratch area.
322 * This information is necessary for calculating the number of bytes to erase
323 * and copy during an image swap. The information collected during this
324 * function is used to populate the boot_data global.
325 */
326static int
327boot_read_sectors(void)
328{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800329 const struct flash_area *scratch;
330 int num_sectors_slot0;
331 int num_sectors_slot1;
332 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800333
334 num_sectors_slot0 = BOOT_MAX_IMG_SECTORS;
335 rc = flash_area_to_sectors(FLASH_AREA_IMAGE_0, &num_sectors_slot0,
336 boot_data.imgs[0].sectors);
337 if (rc != 0) {
338 return BOOT_EFLASH;
339 }
Marti Bolivard3269fd2017-06-12 16:31:12 -0400340 boot_img_set_num_sectors(&boot_data, 0, num_sectors_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800341
342 num_sectors_slot1 = BOOT_MAX_IMG_SECTORS;
343 rc = flash_area_to_sectors(FLASH_AREA_IMAGE_1, &num_sectors_slot1,
344 boot_data.imgs[1].sectors);
345 if (rc != 0) {
346 return BOOT_EFLASH;
347 }
Marti Bolivard3269fd2017-06-12 16:31:12 -0400348 boot_img_set_num_sectors(&boot_data, 1, num_sectors_slot1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800349
350 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &scratch);
351 if (rc != 0) {
352 return BOOT_EFLASH;
353 }
354 boot_data.scratch_sector = *scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800355
356 boot_data.write_sz = boot_write_sz();
357
358 return 0;
359}
360
361static uint32_t
362boot_status_internal_off(int idx, int state, int elem_sz)
363{
364 int idx_sz;
365
366 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
367
368 return idx * idx_sz + state * elem_sz;
369}
370
371/**
372 * Reads the status of a partially-completed swap, if any. This is necessary
373 * to recover in case the boot lodaer was reset in the middle of a swap
374 * operation.
375 */
376static int
377boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
378{
379 uint32_t off;
380 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300381 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800382 int found;
383 int rc;
384 int i;
385
386 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400387 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300388
Christopher Collins92ea77f2016-12-12 15:59:26 -0800389 found = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300390 for (i = 0; i < max_entries; i++) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800391 rc = flash_area_read(fap, off + i * boot_data.write_sz, &status, 1);
392 if (rc != 0) {
393 return BOOT_EFLASH;
394 }
395
396 if (status == 0xff) {
397 if (found) {
398 break;
399 }
400 } else if (!found) {
401 found = 1;
402 }
403 }
404
405 if (found) {
406 i--;
Fabio Utzig94d998c2017-05-22 11:02:41 -0400407 bs->idx = i / BOOT_STATUS_STATE_COUNT;
408 bs->state = i % BOOT_STATUS_STATE_COUNT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800409 }
410
411 return 0;
412}
413
414/**
415 * Reads the boot status from the flash. The boot status contains
416 * the current state of an interrupted image copy operation. If the boot
417 * status is not present, or it indicates that previous copy finished,
418 * there is no operation in progress.
419 */
420static int
421boot_read_status(struct boot_status *bs)
422{
423 const struct flash_area *fap;
424 int status_loc;
425 int area_id;
426 int rc;
427
428 memset(bs, 0, sizeof *bs);
429
430 status_loc = boot_status_source();
431 switch (status_loc) {
432 case BOOT_STATUS_SOURCE_NONE:
433 return 0;
434
435 case BOOT_STATUS_SOURCE_SCRATCH:
436 area_id = FLASH_AREA_IMAGE_SCRATCH;
437 break;
438
439 case BOOT_STATUS_SOURCE_SLOT0:
440 area_id = FLASH_AREA_IMAGE_0;
441 break;
442
443 default:
444 assert(0);
445 return BOOT_EBADARGS;
446 }
447
448 rc = flash_area_open(area_id, &fap);
449 if (rc != 0) {
450 return BOOT_EFLASH;
451 }
452
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300453 return boot_read_status_bytes(fap, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800454}
455
456/**
457 * Writes the supplied boot status to the flash file system. The boot status
458 * contains the current state of an in-progress image copy operation.
459 *
460 * @param bs The boot status to write.
461 *
462 * @return 0 on success; nonzero on failure.
463 */
464int
465boot_write_status(struct boot_status *bs)
466{
467 const struct flash_area *fap;
468 uint32_t off;
469 int area_id;
470 int rc;
David Brown9d725462017-01-23 15:50:58 -0700471 uint8_t buf[8];
472 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800473
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300474 /* NOTE: The first sector copied (that is the last sector on slot) contains
475 * the trailer. Since in the last step SLOT 0 is erased, the first
476 * two status writes go to the scratch which will be copied to SLOT 0!
477 */
478
Fabio Utzig2473ac02017-05-02 12:45:02 -0300479 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800480 /* Write to scratch. */
481 area_id = FLASH_AREA_IMAGE_SCRATCH;
482 } else {
483 /* Write to slot 0. */
484 area_id = FLASH_AREA_IMAGE_0;
485 }
486
487 rc = flash_area_open(area_id, &fap);
488 if (rc != 0) {
489 rc = BOOT_EFLASH;
490 goto done;
491 }
492
493 off = boot_status_off(fap) +
494 boot_status_internal_off(bs->idx, bs->state, boot_data.write_sz);
495
David Brown9d725462017-01-23 15:50:58 -0700496 align = hal_flash_align(fap->fa_device_id);
David Brown9d725462017-01-23 15:50:58 -0700497 memset(buf, 0xFF, 8);
498 buf[0] = bs->state;
499
500 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800501 if (rc != 0) {
502 rc = BOOT_EFLASH;
503 goto done;
504 }
505
506 rc = 0;
507
508done:
509 flash_area_close(fap);
510 return rc;
511}
512
513/*
514 * Validate image hash/signature in a slot.
515 */
516static int
517boot_image_check(struct image_header *hdr, const struct flash_area *fap)
518{
David Browndb1d9d32017-01-06 11:07:54 -0700519 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800520
Christopher Collins92ea77f2016-12-12 15:59:26 -0800521 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
522 NULL, 0, NULL)) {
523 return BOOT_EBADIMAGE;
524 }
525 return 0;
526}
527
528static int
529split_image_check(struct image_header *app_hdr,
530 const struct flash_area *app_fap,
531 struct image_header *loader_hdr,
532 const struct flash_area *loader_fap)
533{
534 static void *tmpbuf;
535 uint8_t loader_hash[32];
536
537 if (!tmpbuf) {
538 tmpbuf = malloc(BOOT_TMPBUF_SZ);
539 if (!tmpbuf) {
540 return BOOT_ENOMEM;
541 }
542 }
543
544 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
545 NULL, 0, loader_hash)) {
546 return BOOT_EBADIMAGE;
547 }
548
549 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
550 loader_hash, 32, NULL)) {
551 return BOOT_EBADIMAGE;
552 }
553
554 return 0;
555}
556
557static int
David Brownd930ec62016-12-14 07:59:48 -0700558boot_validate_slot(int slot)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800559{
560 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400561 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800562 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300563
Marti Bolivarf804f622017-06-12 15:41:48 -0400564 hdr = boot_img_hdr(&boot_data, slot);
565 if (hdr->ih_magic == 0xffffffff || hdr->ih_flags & IMAGE_F_NON_BOOTABLE) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300566 /* No bootable image in slot; continue booting from slot 0. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800567 return -1;
568 }
569
David Brownd930ec62016-12-14 07:59:48 -0700570 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800571 if (rc != 0) {
572 return BOOT_EFLASH;
573 }
574
Marti Bolivarf804f622017-06-12 15:41:48 -0400575 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap) != 0)) {
David Brownb38e0442017-02-24 13:57:12 -0700576 if (slot != 0) {
577 flash_area_erase(fap, 0, fap->fa_size);
578 /* Image in slot 1 is invalid. Erase the image and
579 * continue booting from slot 0.
580 */
581 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800582 return -1;
583 }
584
585 flash_area_close(fap);
586
587 /* Image in slot 1 is valid. */
588 return 0;
589}
590
591/**
592 * Determines which swap operation to perform, if any. If it is determined
593 * that a swap operation is required, the image in the second slot is checked
594 * for validity. If the image in the second slot is invalid, it is erased, and
595 * a swap type of "none" is indicated.
596 *
597 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
598 */
599static int
600boot_validated_swap_type(void)
601{
602 int swap_type;
603 int rc;
604
605 swap_type = boot_swap_type();
606 if (swap_type == BOOT_SWAP_TYPE_NONE) {
607 /* Continue using slot 0. */
608 return BOOT_SWAP_TYPE_NONE;
609 }
610
611 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
David Brownd930ec62016-12-14 07:59:48 -0700612 rc = boot_validate_slot(1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800613 if (rc != 0) {
614 return BOOT_SWAP_TYPE_FAIL;
615 }
616
617 return swap_type;
618}
619
620/**
621 * Calculates the number of sectors the scratch area can contain. A "last"
622 * source sector is specified because images are copied backwards in flash
623 * (final index to index number 0).
624 *
625 * @param last_sector_idx The index of the last source sector
626 * (inclusive).
627 * @param out_first_sector_idx The index of the first source sector
628 * (inclusive) gets written here.
629 *
630 * @return The number of bytes comprised by the
631 * [first-sector, last-sector] range.
632 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300633#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800634static uint32_t
635boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
636{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400637 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800638 uint32_t new_sz;
639 uint32_t sz;
640 int i;
641
642 sz = 0;
643
Marti Bolivard3269fd2017-06-12 16:31:12 -0400644 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800645 for (i = last_sector_idx; i >= 0; i--) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400646 new_sz = sz + boot_img_sector_size(&boot_data, 0, i);
647 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800648 break;
649 }
650 sz = new_sz;
651 }
652
653 /* i currently refers to a sector that doesn't fit or it is -1 because all
654 * sectors have been processed. In both cases, exclude sector i.
655 */
656 *out_first_sector_idx = i + 1;
657 return sz;
658}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300659#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800660
661/**
662 * Erases a region of flash.
663 *
664 * @param flash_area_idx The ID of the flash area containing the region
665 * to erase.
666 * @param off The offset within the flash area to start the
667 * erase.
668 * @param sz The number of bytes to erase.
669 *
670 * @return 0 on success; nonzero on failure.
671 */
672static int
673boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
674{
675 const struct flash_area *fap;
676 int rc;
677
678 rc = flash_area_open(flash_area_id, &fap);
679 if (rc != 0) {
680 rc = BOOT_EFLASH;
681 goto done;
682 }
683
684 rc = flash_area_erase(fap, off, sz);
685 if (rc != 0) {
686 rc = BOOT_EFLASH;
687 goto done;
688 }
689
690 rc = 0;
691
692done:
693 flash_area_close(fap);
694 return rc;
695}
696
697/**
698 * Copies the contents of one flash region to another. You must erase the
699 * destination region prior to calling this function.
700 *
701 * @param flash_area_id_src The ID of the source flash area.
702 * @param flash_area_id_dst The ID of the destination flash area.
703 * @param off_src The offset within the source flash area to
704 * copy from.
705 * @param off_dst The offset within the destination flash area to
706 * copy to.
707 * @param sz The number of bytes to copy.
708 *
709 * @return 0 on success; nonzero on failure.
710 */
711static int
712boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
713 uint32_t off_src, uint32_t off_dst, uint32_t sz)
714{
715 const struct flash_area *fap_src;
716 const struct flash_area *fap_dst;
717 uint32_t bytes_copied;
718 int chunk_sz;
719 int rc;
720
721 static uint8_t buf[1024];
722
723 fap_src = NULL;
724 fap_dst = NULL;
725
726 rc = flash_area_open(flash_area_id_src, &fap_src);
727 if (rc != 0) {
728 rc = BOOT_EFLASH;
729 goto done;
730 }
731
732 rc = flash_area_open(flash_area_id_dst, &fap_dst);
733 if (rc != 0) {
734 rc = BOOT_EFLASH;
735 goto done;
736 }
737
738 bytes_copied = 0;
739 while (bytes_copied < sz) {
740 if (sz - bytes_copied > sizeof buf) {
741 chunk_sz = sizeof buf;
742 } else {
743 chunk_sz = sz - bytes_copied;
744 }
745
746 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
747 if (rc != 0) {
748 rc = BOOT_EFLASH;
749 goto done;
750 }
751
752 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
753 if (rc != 0) {
754 rc = BOOT_EFLASH;
755 goto done;
756 }
757
758 bytes_copied += chunk_sz;
759 }
760
761 rc = 0;
762
763done:
764 flash_area_close(fap_src);
765 flash_area_close(fap_dst);
766 return rc;
767}
768
Fabio Utzig2473ac02017-05-02 12:45:02 -0300769static inline int
770boot_status_init_by_id(int flash_area_id)
771{
772 const struct flash_area *fap;
773 struct boot_swap_state swap_state;
774 int rc;
775
776 rc = flash_area_open(flash_area_id, &fap);
777 assert(rc == 0);
778
779 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
780 assert(rc == 0);
781
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400782 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300783 rc = boot_write_image_ok(fap);
784 assert(rc == 0);
785 }
786
787 rc = boot_write_magic(fap);
788 assert(rc == 0);
789
790 flash_area_close(fap);
791
792 return 0;
793}
794
795static int
796boot_erase_last_sector_by_id(int flash_area_id)
797{
798 uint8_t slot;
799 uint32_t last_sector;
800 struct flash_area *sectors;
801 int rc;
802
803 switch (flash_area_id) {
804 case FLASH_AREA_IMAGE_0:
805 slot = 0;
806 break;
807 case FLASH_AREA_IMAGE_1:
808 slot = 1;
809 break;
810 default:
811 return BOOT_EFLASH;
812 }
813
Marti Bolivard3269fd2017-06-12 16:31:12 -0400814 last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300815 sectors = boot_data.imgs[slot].sectors;
816 rc = boot_erase_sector(flash_area_id,
817 sectors[last_sector].fa_off - sectors[0].fa_off,
Marti Bolivard3269fd2017-06-12 16:31:12 -0400818 boot_img_sector_size(&boot_data, slot, last_sector));
Fabio Utzig2473ac02017-05-02 12:45:02 -0300819 assert(rc == 0);
820
821 return rc;
822}
823
Christopher Collins92ea77f2016-12-12 15:59:26 -0800824/**
825 * Swaps the contents of two flash regions within the two image slots.
826 *
827 * @param idx The index of the first sector in the range of
828 * sectors being swapped.
829 * @param sz The number of bytes to swap.
830 * @param bs The current boot status. This struct gets
831 * updated according to the outcome.
832 *
833 * @return 0 on success; nonzero on failure.
834 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300835#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800836static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800837boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
838{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300839 const struct flash_area *fap;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800840 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300841 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800842 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300843 uint32_t scratch_trailer_off;
844 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400845 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800846 int rc;
847
848 /* Calculate offset from start of image area. */
849 img_off = boot_data.imgs[0].sectors[idx].fa_off -
850 boot_data.imgs[0].sectors[0].fa_off;
851
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300852 copy_sz = sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300853 trailer_sz = boot_slots_trailer_sz(boot_data.write_sz);
Fabio Utzig9678c972017-05-23 11:28:56 -0400854
855 /* sz in this function is always is always sized on a multiple of the
856 * sector size. The check against the first address of the last sector
857 * is to determine if we're swapping the last sector. The last sector
858 * needs special handling because it's where the trailer lives. If we're
859 * copying it, we need to use scratch to write the trailer temporarily.
860 *
861 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
862 * controls if special handling is needed (swapping last sector).
863 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400864 last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300865 if (boot_data.imgs[0].sectors[idx].fa_off + sz >
Marti Bolivard3269fd2017-06-12 16:31:12 -0400866 boot_data.imgs[0].sectors[last_sector].fa_off) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300867 copy_sz -= trailer_sz;
868 }
869
Fabio Utzig2473ac02017-05-02 12:45:02 -0300870 bs->use_scratch = (bs->idx == 0 && copy_sz != sz);
871
Christopher Collins92ea77f2016-12-12 15:59:26 -0800872 if (bs->state == 0) {
873 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800874 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800875
876 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300877 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800878 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800879
Fabio Utzig2473ac02017-05-02 12:45:02 -0300880 if (bs->idx == 0) {
881 if (bs->use_scratch) {
882 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH);
883 } else {
884 /* Prepare the status area... here it is known that the
885 * last sector is not being used by the image data so it's
886 * safe to erase.
887 */
888 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300889 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300890
891 boot_status_init_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300892 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300893 }
894
Christopher Collins92ea77f2016-12-12 15:59:26 -0800895 bs->state = 1;
Christopher Collins4772ac42017-02-27 20:08:01 -0800896 rc = boot_write_status(bs);
897 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800898 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300899
Christopher Collins92ea77f2016-12-12 15:59:26 -0800900 if (bs->state == 1) {
901 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800902 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800903
Christopher Collins92ea77f2016-12-12 15:59:26 -0800904 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
905 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800906 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800907
Fabio Utzig2473ac02017-05-02 12:45:02 -0300908 if (bs->idx == 0 && !bs->use_scratch) {
909 /* If not all sectors of the slot are being swapped,
910 * guarantee here that only slot0 will have the state.
911 */
912 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1);
913 assert(rc == 0);
914 }
915
Christopher Collins92ea77f2016-12-12 15:59:26 -0800916 bs->state = 2;
Christopher Collins4772ac42017-02-27 20:08:01 -0800917 rc = boot_write_status(bs);
918 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800919 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300920
Christopher Collins92ea77f2016-12-12 15:59:26 -0800921 if (bs->state == 2) {
922 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800923 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800924
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300925 /* NOTE: also copy trailer from scratch (has status info) */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800926 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300927 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800928 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800929
Fabio Utzig94d998c2017-05-22 11:02:41 -0400930 if (bs->use_scratch) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300931 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
932 assert(rc == 0);
933
934 scratch_trailer_off = boot_status_off(fap);
935
936 flash_area_close(fap);
937
938 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
939 assert(rc == 0);
940
941 /* copy current status that is being maintained in scratch */
942 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
943 scratch_trailer_off,
944 img_off + copy_sz + BOOT_MAGIC_SZ,
945 BOOT_STATUS_STATE_COUNT * boot_data.write_sz);
946 assert(rc == 0);
947
Fabio Utzig2473ac02017-05-02 12:45:02 -0300948 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
949 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300950 assert(rc == 0);
951
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400952 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300953 rc = boot_write_image_ok(fap);
954 assert(rc == 0);
955 }
956
957 rc = boot_write_magic(fap);
958 assert(rc == 0);
959
960 flash_area_close(fap);
961 }
962
Christopher Collins92ea77f2016-12-12 15:59:26 -0800963 bs->idx++;
964 bs->state = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300965 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -0800966 rc = boot_write_status(bs);
967 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800968 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800969}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300970#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800971
972/**
973 * Swaps the two images in flash. If a prior copy operation was interrupted
974 * by a system reset, this function completes that operation.
975 *
976 * @param bs The current boot status. This function reads
977 * this struct to determine if it is resuming
978 * an interrupted swap operation. This
979 * function writes the updated status to this
980 * function on return.
981 *
982 * @return 0 on success; nonzero on failure.
983 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300984#ifdef MCUBOOT_OVERWRITE_ONLY
David Brown17609d82017-05-05 09:41:34 -0600985static int
986boot_copy_image(struct boot_status *bs)
987{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400988 size_t sect_count;
989 size_t sect;
David Brown17609d82017-05-05 09:41:34 -0600990 int rc;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400991 size_t size = 0;
992 size_t this_size;
David Brown17609d82017-05-05 09:41:34 -0600993
994 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
995 BOOT_LOG_INF("Erasing slot0");
996
Marti Bolivard3269fd2017-06-12 16:31:12 -0400997 sect_count = boot_img_num_sectors(&boot_data, 0);
David Brown17609d82017-05-05 09:41:34 -0600998 for (sect = 0; sect < sect_count; sect++) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400999 this_size = boot_img_sector_size(&boot_data, 0, sect);
David Brown17609d82017-05-05 09:41:34 -06001000 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
1001 size,
1002 this_size);
1003 assert(rc == 0);
1004
1005 size += this_size;
1006 }
1007
1008 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%x bytes",
1009 size);
1010 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
1011 0, 0, size);
1012
1013 /* Erase slot 1 so that we don't do the upgrade on every boot.
1014 * TODO: Perhaps verify slot 0's signature again? */
1015 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
Marti Bolivard3269fd2017-06-12 16:31:12 -04001016 0, boot_img_sector_size(&boot_data, 1, 0));
David Brown17609d82017-05-05 09:41:34 -06001017 assert(rc == 0);
1018
1019 return 0;
1020}
1021#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001022static int
1023boot_copy_image(struct boot_status *bs)
1024{
1025 uint32_t sz;
1026 int first_sector_idx;
1027 int last_sector_idx;
1028 int swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001029 struct image_header *hdr;
1030 uint32_t size;
1031 uint32_t copy_size;
1032 struct image_header tmp_hdr;
1033 int rc;
1034
1035 /* FIXME: just do this if asked by user? */
1036
1037 size = copy_size = 0;
1038
Marti Bolivarf804f622017-06-12 15:41:48 -04001039 hdr = boot_img_hdr(&boot_data, 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001040 if (hdr->ih_magic == IMAGE_MAGIC) {
1041 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1042 }
1043
Marti Bolivarf804f622017-06-12 15:41:48 -04001044 hdr = boot_img_hdr(&boot_data, 1);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001045 if (hdr->ih_magic == IMAGE_MAGIC) {
1046 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1047 }
1048
1049 if (!size || !copy_size || size == copy_size) {
1050 rc = boot_read_image_header(2, &tmp_hdr);
1051 assert(rc == 0);
1052
1053 hdr = &tmp_hdr;
1054 if (hdr->ih_magic == IMAGE_MAGIC) {
1055 if (!size) {
1056 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1057 } else {
1058 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1059 }
1060 }
1061 }
1062
1063 if (size > copy_size) {
1064 copy_size = size;
1065 }
1066
1067 size = 0;
1068 last_sector_idx = 0;
1069 while (1) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001070 size += boot_img_sector_size(&boot_data, 0, last_sector_idx);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001071 if (size >= copy_size) {
1072 break;
1073 }
1074 last_sector_idx++;
1075 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001076
1077 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001078 while (last_sector_idx >= 0) {
1079 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
1080 if (swap_idx >= bs->idx) {
1081 boot_swap_sectors(first_sector_idx, sz, bs);
1082 }
1083
1084 last_sector_idx = first_sector_idx - 1;
1085 swap_idx++;
1086 }
1087
1088 return 0;
1089}
David Brown17609d82017-05-05 09:41:34 -06001090#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001091
1092/**
1093 * Marks a test image in slot 0 as fully copied.
1094 */
1095static int
1096boot_finalize_test_swap(void)
1097{
1098 const struct flash_area *fap;
1099 int rc;
1100
1101 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1102 if (rc != 0) {
1103 return BOOT_EFLASH;
1104 }
1105
1106 rc = boot_write_copy_done(fap);
1107 if (rc != 0) {
1108 return rc;
1109 }
1110
1111 return 0;
1112}
1113
1114/**
1115 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1116 * the status bytes from the image revert operation don't get processed on a
1117 * subsequent boot.
1118 */
1119static int
1120boot_finalize_revert_swap(void)
1121{
1122 const struct flash_area *fap;
1123 struct boot_swap_state state_slot0;
1124 int rc;
1125
1126 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1127 if (rc != 0) {
1128 return BOOT_EFLASH;
1129 }
1130
1131 rc = boot_read_swap_state(fap, &state_slot0);
1132 if (rc != 0) {
1133 return BOOT_EFLASH;
1134 }
1135
1136 if (state_slot0.magic == BOOT_MAGIC_UNSET) {
1137 rc = boot_write_magic(fap);
1138 if (rc != 0) {
1139 return rc;
1140 }
1141 }
1142
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001143 if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001144 rc = boot_write_copy_done(fap);
1145 if (rc != 0) {
1146 return rc;
1147 }
1148 }
1149
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001150 if (state_slot0.image_ok == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001151 rc = boot_write_image_ok(fap);
1152 if (rc != 0) {
1153 return rc;
1154 }
1155 }
1156
1157 return 0;
1158}
1159
1160/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001161 * Performs an image swap if one is required.
1162 *
1163 * @param out_swap_type On success, the type of swap performed gets
1164 * written here.
1165 *
1166 * @return 0 on success; nonzero on failure.
1167 */
1168static int
1169boot_swap_if_needed(int *out_swap_type)
1170{
1171 struct boot_status bs;
1172 int swap_type;
1173 int rc;
1174
1175 /* Determine if we rebooted in the middle of an image swap
1176 * operation.
1177 */
1178 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001179 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001180 if (rc != 0) {
1181 return rc;
1182 }
1183
1184 /* If a partial swap was detected, complete it. */
1185 if (bs.idx != 0 || bs.state != 0) {
1186 rc = boot_copy_image(&bs);
1187 assert(rc == 0);
1188
1189 /* Extrapolate the type of the partial swap. We need this
1190 * information to know how to mark the swap complete in flash.
1191 */
1192 swap_type = boot_previous_swap_type();
1193 } else {
1194 swap_type = boot_validated_swap_type();
1195 switch (swap_type) {
1196 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001197 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001198 case BOOT_SWAP_TYPE_REVERT:
1199 rc = boot_copy_image(&bs);
1200 assert(rc == 0);
1201 break;
1202 }
1203 }
1204
1205 *out_swap_type = swap_type;
1206 return 0;
1207}
1208
1209/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001210 * Prepares the booting process. This function moves images around in flash as
1211 * appropriate, and tells you what address to boot from.
1212 *
1213 * @param rsp On success, indicates how booting should occur.
1214 *
1215 * @return 0 on success; nonzero on failure.
1216 */
1217int
1218boot_go(struct boot_rsp *rsp)
1219{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001220 int swap_type;
1221 int slot;
1222 int rc;
1223
1224 /* The array of slot sectors are defined here (as opposed to file scope) so
1225 * that they don't get allocated for non-boot-loader apps. This is
1226 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001227 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001228 */
1229 static struct flash_area slot0_sectors[BOOT_MAX_IMG_SECTORS];
1230 static struct flash_area slot1_sectors[BOOT_MAX_IMG_SECTORS];
1231 boot_data.imgs[0].sectors = slot0_sectors;
1232 boot_data.imgs[1].sectors = slot1_sectors;
1233
1234 /* Determine the sector layout of the image slots and scratch area. */
1235 rc = boot_read_sectors();
1236 if (rc != 0) {
1237 return rc;
1238 }
1239
1240 /* Attempt to read an image header from each slot. */
1241 rc = boot_read_image_headers();
1242 if (rc != 0) {
1243 return rc;
1244 }
1245
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001246 /* If the image slots aren't compatible, no swap is possible. Just boot
1247 * into slot 0.
1248 */
1249 if (boot_slots_compatible()) {
1250 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001251 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001252 if (rc != 0) {
1253 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001254 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001255 } else {
1256 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001257 }
1258
1259 switch (swap_type) {
1260 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig19356bf2017-05-11 16:19:36 -03001261#ifdef MCUBOOT_VALIDATE_SLOT0
David Brownd930ec62016-12-14 07:59:48 -07001262 rc = boot_validate_slot(0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001263 assert(rc == 0);
David Brownd930ec62016-12-14 07:59:48 -07001264 if (rc != 0) {
1265 return BOOT_EBADIMAGE;
1266 }
1267#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001268 slot = 0;
1269 break;
1270
1271 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001272 case BOOT_SWAP_TYPE_PERM:
Christopher Collins92ea77f2016-12-12 15:59:26 -08001273 slot = 1;
1274 boot_finalize_test_swap();
1275 break;
1276
1277 case BOOT_SWAP_TYPE_REVERT:
1278 slot = 1;
1279 boot_finalize_revert_swap();
1280 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();
1289 break;
1290
1291 default:
1292 assert(0);
1293 slot = 0;
1294 break;
1295 }
1296
1297 /* Always boot from the primary slot. */
Marti Bolivare2587152017-06-12 15:52:05 -04001298 rsp->br_flash_id = boot_img_fa_device_id(&boot_data, 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001299 rsp->br_image_addr = boot_data.imgs[0].sectors[0].fa_off;
Marti Bolivarf804f622017-06-12 15:41:48 -04001300 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001301
1302 return 0;
1303}
1304
1305int
1306split_go(int loader_slot, int split_slot, void **entry)
1307{
1308 const struct flash_area *loader_fap;
1309 const struct flash_area *app_fap;
1310 struct flash_area *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001311 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001312 int loader_flash_id;
1313 int app_flash_id;
1314 int rc;
1315
1316 app_fap = NULL;
1317 loader_fap = NULL;
1318
1319 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1320 if (sectors == NULL) {
1321 rc = SPLIT_GO_ERR;
1322 goto done;
1323 }
1324 boot_data.imgs[0].sectors = sectors + 0;
1325 boot_data.imgs[1].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1326
1327 /* Determine the sector layout of the image slots and scratch area. */
1328 rc = boot_read_sectors();
1329 if (rc != 0) {
1330 rc = SPLIT_GO_ERR;
1331 goto done;
1332 }
1333
1334 rc = boot_read_image_headers();
1335 if (rc != 0) {
1336 goto done;
1337 }
1338
1339 app_flash_id = flash_area_id_from_image_slot(split_slot);
1340 rc = flash_area_open(app_flash_id, &app_fap);
1341 if (rc != 0) {
1342 rc = BOOT_EFLASH;
1343 goto done;
1344 }
1345
1346 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1347 rc = flash_area_open(loader_flash_id, &loader_fap);
1348 if (rc != 0) {
1349 rc = BOOT_EFLASH;
1350 goto done;
1351 }
1352
1353 /* Don't check the bootable image flag because we could really call a
1354 * bootable or non-bootable image. Just validate that the image check
1355 * passes which is distinct from the normal check.
1356 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001357 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Christopher Collins92ea77f2016-12-12 15:59:26 -08001358 app_fap,
Marti Bolivarf804f622017-06-12 15:41:48 -04001359 boot_img_hdr(&boot_data, loader_slot),
Christopher Collins92ea77f2016-12-12 15:59:26 -08001360 loader_fap);
1361 if (rc != 0) {
1362 rc = SPLIT_GO_NON_MATCHING;
1363 goto done;
1364 }
1365
1366 entry_val = boot_data.imgs[split_slot].sectors[0].fa_off +
Marti Bolivarf804f622017-06-12 15:41:48 -04001367 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001368 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001369 rc = SPLIT_GO_OK;
1370
1371done:
1372 free(sectors);
1373 flash_area_close(app_fap);
1374 flash_area_close(loader_fap);
1375 return rc;
1376}