blob: 31efe7ff50a1e0c85130ff68adc5fdc0cfc51fa3 [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{
300 const struct flash_area *sector0;
301 const struct flash_area *sector1;
302 int i;
303
304 /* Ensure both image slots have identical sector layouts. */
305 if (boot_data.imgs[0].num_sectors != boot_data.imgs[1].num_sectors) {
306 return 0;
307 }
308 for (i = 0; i < boot_data.imgs[0].num_sectors; i++) {
309 sector0 = boot_data.imgs[0].sectors + i;
310 sector1 = boot_data.imgs[1].sectors + i;
311 if (sector0->fa_size != sector1->fa_size) {
312 return 0;
313 }
314 }
315
316 return 1;
317}
318
Christopher Collins92ea77f2016-12-12 15:59:26 -0800319/**
320 * Determines the sector layout of both image slots and the scratch area.
321 * This information is necessary for calculating the number of bytes to erase
322 * and copy during an image swap. The information collected during this
323 * function is used to populate the boot_data global.
324 */
325static int
326boot_read_sectors(void)
327{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800328 const struct flash_area *scratch;
329 int num_sectors_slot0;
330 int num_sectors_slot1;
331 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800332
333 num_sectors_slot0 = BOOT_MAX_IMG_SECTORS;
334 rc = flash_area_to_sectors(FLASH_AREA_IMAGE_0, &num_sectors_slot0,
335 boot_data.imgs[0].sectors);
336 if (rc != 0) {
337 return BOOT_EFLASH;
338 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800339 boot_data.imgs[0].num_sectors = num_sectors_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800340
341 num_sectors_slot1 = BOOT_MAX_IMG_SECTORS;
342 rc = flash_area_to_sectors(FLASH_AREA_IMAGE_1, &num_sectors_slot1,
343 boot_data.imgs[1].sectors);
344 if (rc != 0) {
345 return BOOT_EFLASH;
346 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800347 boot_data.imgs[1].num_sectors = num_sectors_slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800348
349 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &scratch);
350 if (rc != 0) {
351 return BOOT_EFLASH;
352 }
353 boot_data.scratch_sector = *scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800354
355 boot_data.write_sz = boot_write_sz();
356
357 return 0;
358}
359
360static uint32_t
361boot_status_internal_off(int idx, int state, int elem_sz)
362{
363 int idx_sz;
364
365 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
366
367 return idx * idx_sz + state * elem_sz;
368}
369
370/**
371 * Reads the status of a partially-completed swap, if any. This is necessary
372 * to recover in case the boot lodaer was reset in the middle of a swap
373 * operation.
374 */
375static int
376boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
377{
378 uint32_t off;
379 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300380 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800381 int found;
382 int rc;
383 int i;
384
385 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400386 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300387
Christopher Collins92ea77f2016-12-12 15:59:26 -0800388 found = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300389 for (i = 0; i < max_entries; i++) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800390 rc = flash_area_read(fap, off + i * boot_data.write_sz, &status, 1);
391 if (rc != 0) {
392 return BOOT_EFLASH;
393 }
394
395 if (status == 0xff) {
396 if (found) {
397 break;
398 }
399 } else if (!found) {
400 found = 1;
401 }
402 }
403
404 if (found) {
405 i--;
Fabio Utzig94d998c2017-05-22 11:02:41 -0400406 bs->idx = i / BOOT_STATUS_STATE_COUNT;
407 bs->state = i % BOOT_STATUS_STATE_COUNT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800408 }
409
410 return 0;
411}
412
413/**
414 * Reads the boot status from the flash. The boot status contains
415 * the current state of an interrupted image copy operation. If the boot
416 * status is not present, or it indicates that previous copy finished,
417 * there is no operation in progress.
418 */
419static int
420boot_read_status(struct boot_status *bs)
421{
422 const struct flash_area *fap;
423 int status_loc;
424 int area_id;
425 int rc;
426
427 memset(bs, 0, sizeof *bs);
428
429 status_loc = boot_status_source();
430 switch (status_loc) {
431 case BOOT_STATUS_SOURCE_NONE:
432 return 0;
433
434 case BOOT_STATUS_SOURCE_SCRATCH:
435 area_id = FLASH_AREA_IMAGE_SCRATCH;
436 break;
437
438 case BOOT_STATUS_SOURCE_SLOT0:
439 area_id = FLASH_AREA_IMAGE_0;
440 break;
441
442 default:
443 assert(0);
444 return BOOT_EBADARGS;
445 }
446
447 rc = flash_area_open(area_id, &fap);
448 if (rc != 0) {
449 return BOOT_EFLASH;
450 }
451
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300452 return boot_read_status_bytes(fap, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800453}
454
455/**
456 * Writes the supplied boot status to the flash file system. The boot status
457 * contains the current state of an in-progress image copy operation.
458 *
459 * @param bs The boot status to write.
460 *
461 * @return 0 on success; nonzero on failure.
462 */
463int
464boot_write_status(struct boot_status *bs)
465{
466 const struct flash_area *fap;
467 uint32_t off;
468 int area_id;
469 int rc;
David Brown9d725462017-01-23 15:50:58 -0700470 uint8_t buf[8];
471 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800472
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300473 /* NOTE: The first sector copied (that is the last sector on slot) contains
474 * the trailer. Since in the last step SLOT 0 is erased, the first
475 * two status writes go to the scratch which will be copied to SLOT 0!
476 */
477
Fabio Utzig2473ac02017-05-02 12:45:02 -0300478 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800479 /* Write to scratch. */
480 area_id = FLASH_AREA_IMAGE_SCRATCH;
481 } else {
482 /* Write to slot 0. */
483 area_id = FLASH_AREA_IMAGE_0;
484 }
485
486 rc = flash_area_open(area_id, &fap);
487 if (rc != 0) {
488 rc = BOOT_EFLASH;
489 goto done;
490 }
491
492 off = boot_status_off(fap) +
493 boot_status_internal_off(bs->idx, bs->state, boot_data.write_sz);
494
David Brown9d725462017-01-23 15:50:58 -0700495 align = hal_flash_align(fap->fa_device_id);
David Brown9d725462017-01-23 15:50:58 -0700496 memset(buf, 0xFF, 8);
497 buf[0] = bs->state;
498
499 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800500 if (rc != 0) {
501 rc = BOOT_EFLASH;
502 goto done;
503 }
504
505 rc = 0;
506
507done:
508 flash_area_close(fap);
509 return rc;
510}
511
512/*
513 * Validate image hash/signature in a slot.
514 */
515static int
516boot_image_check(struct image_header *hdr, const struct flash_area *fap)
517{
David Browndb1d9d32017-01-06 11:07:54 -0700518 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800519
Christopher Collins92ea77f2016-12-12 15:59:26 -0800520 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
521 NULL, 0, NULL)) {
522 return BOOT_EBADIMAGE;
523 }
524 return 0;
525}
526
527static int
528split_image_check(struct image_header *app_hdr,
529 const struct flash_area *app_fap,
530 struct image_header *loader_hdr,
531 const struct flash_area *loader_fap)
532{
533 static void *tmpbuf;
534 uint8_t loader_hash[32];
535
536 if (!tmpbuf) {
537 tmpbuf = malloc(BOOT_TMPBUF_SZ);
538 if (!tmpbuf) {
539 return BOOT_ENOMEM;
540 }
541 }
542
543 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
544 NULL, 0, loader_hash)) {
545 return BOOT_EBADIMAGE;
546 }
547
548 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
549 loader_hash, 32, NULL)) {
550 return BOOT_EBADIMAGE;
551 }
552
553 return 0;
554}
555
556static int
David Brownd930ec62016-12-14 07:59:48 -0700557boot_validate_slot(int slot)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800558{
559 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400560 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800561 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300562
Marti Bolivarf804f622017-06-12 15:41:48 -0400563 hdr = boot_img_hdr(&boot_data, slot);
564 if (hdr->ih_magic == 0xffffffff || hdr->ih_flags & IMAGE_F_NON_BOOTABLE) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300565 /* No bootable image in slot; continue booting from slot 0. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800566 return -1;
567 }
568
David Brownd930ec62016-12-14 07:59:48 -0700569 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800570 if (rc != 0) {
571 return BOOT_EFLASH;
572 }
573
Marti Bolivarf804f622017-06-12 15:41:48 -0400574 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap) != 0)) {
David Brownb38e0442017-02-24 13:57:12 -0700575 if (slot != 0) {
576 flash_area_erase(fap, 0, fap->fa_size);
577 /* Image in slot 1 is invalid. Erase the image and
578 * continue booting from slot 0.
579 */
580 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800581 return -1;
582 }
583
584 flash_area_close(fap);
585
586 /* Image in slot 1 is valid. */
587 return 0;
588}
589
590/**
591 * Determines which swap operation to perform, if any. If it is determined
592 * that a swap operation is required, the image in the second slot is checked
593 * for validity. If the image in the second slot is invalid, it is erased, and
594 * a swap type of "none" is indicated.
595 *
596 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
597 */
598static int
599boot_validated_swap_type(void)
600{
601 int swap_type;
602 int rc;
603
604 swap_type = boot_swap_type();
605 if (swap_type == BOOT_SWAP_TYPE_NONE) {
606 /* Continue using slot 0. */
607 return BOOT_SWAP_TYPE_NONE;
608 }
609
610 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
David Brownd930ec62016-12-14 07:59:48 -0700611 rc = boot_validate_slot(1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800612 if (rc != 0) {
613 return BOOT_SWAP_TYPE_FAIL;
614 }
615
616 return swap_type;
617}
618
619/**
620 * Calculates the number of sectors the scratch area can contain. A "last"
621 * source sector is specified because images are copied backwards in flash
622 * (final index to index number 0).
623 *
624 * @param last_sector_idx The index of the last source sector
625 * (inclusive).
626 * @param out_first_sector_idx The index of the first source sector
627 * (inclusive) gets written here.
628 *
629 * @return The number of bytes comprised by the
630 * [first-sector, last-sector] range.
631 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300632#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800633static uint32_t
634boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
635{
636 uint32_t new_sz;
637 uint32_t sz;
638 int i;
639
640 sz = 0;
641
642 for (i = last_sector_idx; i >= 0; i--) {
643 new_sz = sz + boot_data.imgs[0].sectors[i].fa_size;
644 if (new_sz > boot_data.scratch_sector.fa_size) {
645 break;
646 }
647 sz = new_sz;
648 }
649
650 /* i currently refers to a sector that doesn't fit or it is -1 because all
651 * sectors have been processed. In both cases, exclude sector i.
652 */
653 *out_first_sector_idx = i + 1;
654 return sz;
655}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300656#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800657
658/**
659 * Erases a region of flash.
660 *
661 * @param flash_area_idx The ID of the flash area containing the region
662 * to erase.
663 * @param off The offset within the flash area to start the
664 * erase.
665 * @param sz The number of bytes to erase.
666 *
667 * @return 0 on success; nonzero on failure.
668 */
669static int
670boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
671{
672 const struct flash_area *fap;
673 int rc;
674
675 rc = flash_area_open(flash_area_id, &fap);
676 if (rc != 0) {
677 rc = BOOT_EFLASH;
678 goto done;
679 }
680
681 rc = flash_area_erase(fap, off, sz);
682 if (rc != 0) {
683 rc = BOOT_EFLASH;
684 goto done;
685 }
686
687 rc = 0;
688
689done:
690 flash_area_close(fap);
691 return rc;
692}
693
694/**
695 * Copies the contents of one flash region to another. You must erase the
696 * destination region prior to calling this function.
697 *
698 * @param flash_area_id_src The ID of the source flash area.
699 * @param flash_area_id_dst The ID of the destination flash area.
700 * @param off_src The offset within the source flash area to
701 * copy from.
702 * @param off_dst The offset within the destination flash area to
703 * copy to.
704 * @param sz The number of bytes to copy.
705 *
706 * @return 0 on success; nonzero on failure.
707 */
708static int
709boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
710 uint32_t off_src, uint32_t off_dst, uint32_t sz)
711{
712 const struct flash_area *fap_src;
713 const struct flash_area *fap_dst;
714 uint32_t bytes_copied;
715 int chunk_sz;
716 int rc;
717
718 static uint8_t buf[1024];
719
720 fap_src = NULL;
721 fap_dst = NULL;
722
723 rc = flash_area_open(flash_area_id_src, &fap_src);
724 if (rc != 0) {
725 rc = BOOT_EFLASH;
726 goto done;
727 }
728
729 rc = flash_area_open(flash_area_id_dst, &fap_dst);
730 if (rc != 0) {
731 rc = BOOT_EFLASH;
732 goto done;
733 }
734
735 bytes_copied = 0;
736 while (bytes_copied < sz) {
737 if (sz - bytes_copied > sizeof buf) {
738 chunk_sz = sizeof buf;
739 } else {
740 chunk_sz = sz - bytes_copied;
741 }
742
743 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
744 if (rc != 0) {
745 rc = BOOT_EFLASH;
746 goto done;
747 }
748
749 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
750 if (rc != 0) {
751 rc = BOOT_EFLASH;
752 goto done;
753 }
754
755 bytes_copied += chunk_sz;
756 }
757
758 rc = 0;
759
760done:
761 flash_area_close(fap_src);
762 flash_area_close(fap_dst);
763 return rc;
764}
765
Fabio Utzig2473ac02017-05-02 12:45:02 -0300766static inline int
767boot_status_init_by_id(int flash_area_id)
768{
769 const struct flash_area *fap;
770 struct boot_swap_state swap_state;
771 int rc;
772
773 rc = flash_area_open(flash_area_id, &fap);
774 assert(rc == 0);
775
776 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
777 assert(rc == 0);
778
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400779 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300780 rc = boot_write_image_ok(fap);
781 assert(rc == 0);
782 }
783
784 rc = boot_write_magic(fap);
785 assert(rc == 0);
786
787 flash_area_close(fap);
788
789 return 0;
790}
791
792static int
793boot_erase_last_sector_by_id(int flash_area_id)
794{
795 uint8_t slot;
796 uint32_t last_sector;
797 struct flash_area *sectors;
798 int rc;
799
800 switch (flash_area_id) {
801 case FLASH_AREA_IMAGE_0:
802 slot = 0;
803 break;
804 case FLASH_AREA_IMAGE_1:
805 slot = 1;
806 break;
807 default:
808 return BOOT_EFLASH;
809 }
810
811 last_sector = boot_data.imgs[slot].num_sectors - 1;
812 sectors = boot_data.imgs[slot].sectors;
813 rc = boot_erase_sector(flash_area_id,
814 sectors[last_sector].fa_off - sectors[0].fa_off,
815 sectors[last_sector].fa_size);
816 assert(rc == 0);
817
818 return rc;
819}
820
Christopher Collins92ea77f2016-12-12 15:59:26 -0800821/**
822 * Swaps the contents of two flash regions within the two image slots.
823 *
824 * @param idx The index of the first sector in the range of
825 * sectors being swapped.
826 * @param sz The number of bytes to swap.
827 * @param bs The current boot status. This struct gets
828 * updated according to the outcome.
829 *
830 * @return 0 on success; nonzero on failure.
831 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300832#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800833static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800834boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
835{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300836 const struct flash_area *fap;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800837 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300838 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800839 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300840 uint32_t scratch_trailer_off;
841 struct boot_swap_state swap_state;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800842 int rc;
843
844 /* Calculate offset from start of image area. */
845 img_off = boot_data.imgs[0].sectors[idx].fa_off -
846 boot_data.imgs[0].sectors[0].fa_off;
847
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300848 copy_sz = sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300849 trailer_sz = boot_slots_trailer_sz(boot_data.write_sz);
Fabio Utzig9678c972017-05-23 11:28:56 -0400850
851 /* sz in this function is always is always sized on a multiple of the
852 * sector size. The check against the first address of the last sector
853 * is to determine if we're swapping the last sector. The last sector
854 * needs special handling because it's where the trailer lives. If we're
855 * copying it, we need to use scratch to write the trailer temporarily.
856 *
857 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
858 * controls if special handling is needed (swapping last sector).
859 */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300860 if (boot_data.imgs[0].sectors[idx].fa_off + sz >
861 boot_data.imgs[0].sectors[boot_data.imgs[0].num_sectors - 1].fa_off) {
862 copy_sz -= trailer_sz;
863 }
864
Fabio Utzig2473ac02017-05-02 12:45:02 -0300865 bs->use_scratch = (bs->idx == 0 && copy_sz != sz);
866
Christopher Collins92ea77f2016-12-12 15:59:26 -0800867 if (bs->state == 0) {
868 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800869 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800870
871 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300872 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800873 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800874
Fabio Utzig2473ac02017-05-02 12:45:02 -0300875 if (bs->idx == 0) {
876 if (bs->use_scratch) {
877 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH);
878 } else {
879 /* Prepare the status area... here it is known that the
880 * last sector is not being used by the image data so it's
881 * safe to erase.
882 */
883 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300884 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300885
886 boot_status_init_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300887 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300888 }
889
Christopher Collins92ea77f2016-12-12 15:59:26 -0800890 bs->state = 1;
Christopher Collins4772ac42017-02-27 20:08:01 -0800891 rc = boot_write_status(bs);
892 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800893 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300894
Christopher Collins92ea77f2016-12-12 15:59:26 -0800895 if (bs->state == 1) {
896 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800897 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800898
Christopher Collins92ea77f2016-12-12 15:59:26 -0800899 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
900 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800901 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800902
Fabio Utzig2473ac02017-05-02 12:45:02 -0300903 if (bs->idx == 0 && !bs->use_scratch) {
904 /* If not all sectors of the slot are being swapped,
905 * guarantee here that only slot0 will have the state.
906 */
907 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1);
908 assert(rc == 0);
909 }
910
Christopher Collins92ea77f2016-12-12 15:59:26 -0800911 bs->state = 2;
Christopher Collins4772ac42017-02-27 20:08:01 -0800912 rc = boot_write_status(bs);
913 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800914 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300915
Christopher Collins92ea77f2016-12-12 15:59:26 -0800916 if (bs->state == 2) {
917 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800918 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800919
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300920 /* NOTE: also copy trailer from scratch (has status info) */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800921 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300922 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800923 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800924
Fabio Utzig94d998c2017-05-22 11:02:41 -0400925 if (bs->use_scratch) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300926 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
927 assert(rc == 0);
928
929 scratch_trailer_off = boot_status_off(fap);
930
931 flash_area_close(fap);
932
933 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
934 assert(rc == 0);
935
936 /* copy current status that is being maintained in scratch */
937 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
938 scratch_trailer_off,
939 img_off + copy_sz + BOOT_MAGIC_SZ,
940 BOOT_STATUS_STATE_COUNT * boot_data.write_sz);
941 assert(rc == 0);
942
Fabio Utzig2473ac02017-05-02 12:45:02 -0300943 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
944 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300945 assert(rc == 0);
946
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400947 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300948 rc = boot_write_image_ok(fap);
949 assert(rc == 0);
950 }
951
952 rc = boot_write_magic(fap);
953 assert(rc == 0);
954
955 flash_area_close(fap);
956 }
957
Christopher Collins92ea77f2016-12-12 15:59:26 -0800958 bs->idx++;
959 bs->state = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300960 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -0800961 rc = boot_write_status(bs);
962 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800963 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800964}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300965#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800966
967/**
968 * Swaps the two images in flash. If a prior copy operation was interrupted
969 * by a system reset, this function completes that operation.
970 *
971 * @param bs The current boot status. This function reads
972 * this struct to determine if it is resuming
973 * an interrupted swap operation. This
974 * function writes the updated status to this
975 * function on return.
976 *
977 * @return 0 on success; nonzero on failure.
978 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300979#ifdef MCUBOOT_OVERWRITE_ONLY
David Brown17609d82017-05-05 09:41:34 -0600980static int
981boot_copy_image(struct boot_status *bs)
982{
983 int sect_count;
984 int sect;
985 int rc;
986 uint32_t size = 0;
987 uint32_t this_size;
988
989 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
990 BOOT_LOG_INF("Erasing slot0");
991
992 sect_count = boot_data.imgs[0].num_sectors;
993 for (sect = 0; sect < sect_count; sect++) {
994 this_size = boot_data.imgs[0].sectors[sect].fa_size;
995 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
996 size,
997 this_size);
998 assert(rc == 0);
999
1000 size += this_size;
1001 }
1002
1003 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%x bytes",
1004 size);
1005 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
1006 0, 0, size);
1007
1008 /* Erase slot 1 so that we don't do the upgrade on every boot.
1009 * TODO: Perhaps verify slot 0's signature again? */
1010 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
1011 0, boot_data.imgs[1].sectors[0].fa_size);
1012 assert(rc == 0);
1013
1014 return 0;
1015}
1016#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001017static int
1018boot_copy_image(struct boot_status *bs)
1019{
1020 uint32_t sz;
1021 int first_sector_idx;
1022 int last_sector_idx;
1023 int swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001024 struct image_header *hdr;
1025 uint32_t size;
1026 uint32_t copy_size;
1027 struct image_header tmp_hdr;
1028 int rc;
1029
1030 /* FIXME: just do this if asked by user? */
1031
1032 size = copy_size = 0;
1033
Marti Bolivarf804f622017-06-12 15:41:48 -04001034 hdr = boot_img_hdr(&boot_data, 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001035 if (hdr->ih_magic == IMAGE_MAGIC) {
1036 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1037 }
1038
Marti Bolivarf804f622017-06-12 15:41:48 -04001039 hdr = boot_img_hdr(&boot_data, 1);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001040 if (hdr->ih_magic == IMAGE_MAGIC) {
1041 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1042 }
1043
1044 if (!size || !copy_size || size == copy_size) {
1045 rc = boot_read_image_header(2, &tmp_hdr);
1046 assert(rc == 0);
1047
1048 hdr = &tmp_hdr;
1049 if (hdr->ih_magic == IMAGE_MAGIC) {
1050 if (!size) {
1051 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1052 } else {
1053 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1054 }
1055 }
1056 }
1057
1058 if (size > copy_size) {
1059 copy_size = size;
1060 }
1061
1062 size = 0;
1063 last_sector_idx = 0;
1064 while (1) {
1065 size += boot_data.imgs[0].sectors[last_sector_idx].fa_size;
1066 if (size >= copy_size) {
1067 break;
1068 }
1069 last_sector_idx++;
1070 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001071
1072 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001073 while (last_sector_idx >= 0) {
1074 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
1075 if (swap_idx >= bs->idx) {
1076 boot_swap_sectors(first_sector_idx, sz, bs);
1077 }
1078
1079 last_sector_idx = first_sector_idx - 1;
1080 swap_idx++;
1081 }
1082
1083 return 0;
1084}
David Brown17609d82017-05-05 09:41:34 -06001085#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001086
1087/**
1088 * Marks a test image in slot 0 as fully copied.
1089 */
1090static int
1091boot_finalize_test_swap(void)
1092{
1093 const struct flash_area *fap;
1094 int rc;
1095
1096 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1097 if (rc != 0) {
1098 return BOOT_EFLASH;
1099 }
1100
1101 rc = boot_write_copy_done(fap);
1102 if (rc != 0) {
1103 return rc;
1104 }
1105
1106 return 0;
1107}
1108
1109/**
1110 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1111 * the status bytes from the image revert operation don't get processed on a
1112 * subsequent boot.
1113 */
1114static int
1115boot_finalize_revert_swap(void)
1116{
1117 const struct flash_area *fap;
1118 struct boot_swap_state state_slot0;
1119 int rc;
1120
1121 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1122 if (rc != 0) {
1123 return BOOT_EFLASH;
1124 }
1125
1126 rc = boot_read_swap_state(fap, &state_slot0);
1127 if (rc != 0) {
1128 return BOOT_EFLASH;
1129 }
1130
1131 if (state_slot0.magic == BOOT_MAGIC_UNSET) {
1132 rc = boot_write_magic(fap);
1133 if (rc != 0) {
1134 return rc;
1135 }
1136 }
1137
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001138 if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001139 rc = boot_write_copy_done(fap);
1140 if (rc != 0) {
1141 return rc;
1142 }
1143 }
1144
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001145 if (state_slot0.image_ok == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001146 rc = boot_write_image_ok(fap);
1147 if (rc != 0) {
1148 return rc;
1149 }
1150 }
1151
1152 return 0;
1153}
1154
1155/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001156 * Performs an image swap if one is required.
1157 *
1158 * @param out_swap_type On success, the type of swap performed gets
1159 * written here.
1160 *
1161 * @return 0 on success; nonzero on failure.
1162 */
1163static int
1164boot_swap_if_needed(int *out_swap_type)
1165{
1166 struct boot_status bs;
1167 int swap_type;
1168 int rc;
1169
1170 /* Determine if we rebooted in the middle of an image swap
1171 * operation.
1172 */
1173 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001174 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001175 if (rc != 0) {
1176 return rc;
1177 }
1178
1179 /* If a partial swap was detected, complete it. */
1180 if (bs.idx != 0 || bs.state != 0) {
1181 rc = boot_copy_image(&bs);
1182 assert(rc == 0);
1183
1184 /* Extrapolate the type of the partial swap. We need this
1185 * information to know how to mark the swap complete in flash.
1186 */
1187 swap_type = boot_previous_swap_type();
1188 } else {
1189 swap_type = boot_validated_swap_type();
1190 switch (swap_type) {
1191 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001192 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001193 case BOOT_SWAP_TYPE_REVERT:
1194 rc = boot_copy_image(&bs);
1195 assert(rc == 0);
1196 break;
1197 }
1198 }
1199
1200 *out_swap_type = swap_type;
1201 return 0;
1202}
1203
1204/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001205 * Prepares the booting process. This function moves images around in flash as
1206 * appropriate, and tells you what address to boot from.
1207 *
1208 * @param rsp On success, indicates how booting should occur.
1209 *
1210 * @return 0 on success; nonzero on failure.
1211 */
1212int
1213boot_go(struct boot_rsp *rsp)
1214{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001215 int swap_type;
1216 int slot;
1217 int rc;
1218
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 */
1224 static struct flash_area slot0_sectors[BOOT_MAX_IMG_SECTORS];
1225 static struct flash_area slot1_sectors[BOOT_MAX_IMG_SECTORS];
1226 boot_data.imgs[0].sectors = slot0_sectors;
1227 boot_data.imgs[1].sectors = slot1_sectors;
1228
1229 /* Determine the sector layout of the image slots and scratch area. */
1230 rc = boot_read_sectors();
1231 if (rc != 0) {
1232 return rc;
1233 }
1234
1235 /* Attempt to read an image header from each slot. */
1236 rc = boot_read_image_headers();
1237 if (rc != 0) {
1238 return rc;
1239 }
1240
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001241 /* If the image slots aren't compatible, no swap is possible. Just boot
1242 * into slot 0.
1243 */
1244 if (boot_slots_compatible()) {
1245 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001246 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001247 if (rc != 0) {
1248 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001249 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001250 } else {
1251 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001252 }
1253
1254 switch (swap_type) {
1255 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig19356bf2017-05-11 16:19:36 -03001256#ifdef MCUBOOT_VALIDATE_SLOT0
David Brownd930ec62016-12-14 07:59:48 -07001257 rc = boot_validate_slot(0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001258 assert(rc == 0);
David Brownd930ec62016-12-14 07:59:48 -07001259 if (rc != 0) {
1260 return BOOT_EBADIMAGE;
1261 }
1262#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001263 slot = 0;
1264 break;
1265
1266 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001267 case BOOT_SWAP_TYPE_PERM:
Christopher Collins92ea77f2016-12-12 15:59:26 -08001268 slot = 1;
1269 boot_finalize_test_swap();
1270 break;
1271
1272 case BOOT_SWAP_TYPE_REVERT:
1273 slot = 1;
1274 boot_finalize_revert_swap();
1275 break;
1276
1277 case BOOT_SWAP_TYPE_FAIL:
1278 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1279 * try to boot into it again on the next reboot. Do this by pretending
1280 * we just reverted back to slot 0.
1281 */
1282 slot = 0;
1283 boot_finalize_revert_swap();
1284 break;
1285
1286 default:
1287 assert(0);
1288 slot = 0;
1289 break;
1290 }
1291
1292 /* Always boot from the primary slot. */
Marti Bolivare2587152017-06-12 15:52:05 -04001293 rsp->br_flash_id = boot_img_fa_device_id(&boot_data, 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001294 rsp->br_image_addr = boot_data.imgs[0].sectors[0].fa_off;
Marti Bolivarf804f622017-06-12 15:41:48 -04001295 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001296
1297 return 0;
1298}
1299
1300int
1301split_go(int loader_slot, int split_slot, void **entry)
1302{
1303 const struct flash_area *loader_fap;
1304 const struct flash_area *app_fap;
1305 struct flash_area *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001306 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001307 int loader_flash_id;
1308 int app_flash_id;
1309 int rc;
1310
1311 app_fap = NULL;
1312 loader_fap = NULL;
1313
1314 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1315 if (sectors == NULL) {
1316 rc = SPLIT_GO_ERR;
1317 goto done;
1318 }
1319 boot_data.imgs[0].sectors = sectors + 0;
1320 boot_data.imgs[1].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1321
1322 /* Determine the sector layout of the image slots and scratch area. */
1323 rc = boot_read_sectors();
1324 if (rc != 0) {
1325 rc = SPLIT_GO_ERR;
1326 goto done;
1327 }
1328
1329 rc = boot_read_image_headers();
1330 if (rc != 0) {
1331 goto done;
1332 }
1333
1334 app_flash_id = flash_area_id_from_image_slot(split_slot);
1335 rc = flash_area_open(app_flash_id, &app_fap);
1336 if (rc != 0) {
1337 rc = BOOT_EFLASH;
1338 goto done;
1339 }
1340
1341 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1342 rc = flash_area_open(loader_flash_id, &loader_fap);
1343 if (rc != 0) {
1344 rc = BOOT_EFLASH;
1345 goto done;
1346 }
1347
1348 /* Don't check the bootable image flag because we could really call a
1349 * bootable or non-bootable image. Just validate that the image check
1350 * passes which is distinct from the normal check.
1351 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001352 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Christopher Collins92ea77f2016-12-12 15:59:26 -08001353 app_fap,
Marti Bolivarf804f622017-06-12 15:41:48 -04001354 boot_img_hdr(&boot_data, loader_slot),
Christopher Collins92ea77f2016-12-12 15:59:26 -08001355 loader_fap);
1356 if (rc != 0) {
1357 rc = SPLIT_GO_NON_MATCHING;
1358 goto done;
1359 }
1360
1361 entry_val = boot_data.imgs[split_slot].sectors[0].fa_off +
Marti Bolivarf804f622017-06-12 15:41:48 -04001362 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001363 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001364 rc = SPLIT_GO_OK;
1365
1366done:
1367 free(sectors);
1368 flash_area_close(app_fap);
1369 flash_area_close(loader_fap);
1370 return rc;
1371}