blob: 20a34b867f691fb30b362506c0a2e5533ae9a5e4 [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"
31#include "flash_map/flash_map.h"
32#include <hal/hal_flash.h>
33#include <os/os_malloc.h>
34#include "bootutil/bootutil.h"
35#include "bootutil/image.h"
36#include "bootutil_priv.h"
37
38#define BOOT_MAX_IMG_SECTORS 120
39
40/** Number of image slots in flash; currently limited to two. */
41#define BOOT_NUM_SLOTS 2
42
43static struct {
44 struct {
45 struct image_header hdr;
46 struct flash_area *sectors;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -080047 int num_sectors;
Christopher Collins92ea77f2016-12-12 15:59:26 -080048 } imgs[BOOT_NUM_SLOTS];
49
Christopher Collins92ea77f2016-12-12 15:59:26 -080050 struct flash_area scratch_sector;
51
52 uint8_t write_sz;
53} boot_data;
54
55struct boot_status_table {
56 /**
57 * For each field, a value of 0 means "any".
58 */
59 uint8_t bst_magic_slot0;
60 uint8_t bst_magic_scratch;
61 uint8_t bst_copy_done_slot0;
62 uint8_t bst_status_source;
63};
64
65/**
66 * This set of tables maps swap state contents to boot status location.
67 * When searching for a match, these tables must be iterated in order.
68 */
69static const struct boot_status_table boot_status_tables[] = {
70 {
71 /* | slot-0 | scratch |
72 * ----------+------------+------------|
73 * magic | Good | Any |
74 * copy-done | 0x01 | N/A |
75 * ----------+------------+------------'
76 * source: none |
77 * ------------------------------------'
78 */
79 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
80 .bst_magic_scratch = 0,
81 .bst_copy_done_slot0 = 0x01,
82 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
83 },
84
85 {
86 /* | slot-0 | scratch |
87 * ----------+------------+------------|
88 * magic | Good | Any |
89 * copy-done | 0xff | N/A |
90 * ----------+------------+------------'
91 * source: slot 0 |
92 * ------------------------------------'
93 */
94 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
95 .bst_magic_scratch = 0,
96 .bst_copy_done_slot0 = 0xff,
97 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
98 },
99
100 {
101 /* | slot-0 | scratch |
102 * ----------+------------+------------|
103 * magic | Any | Good |
104 * copy-done | Any | N/A |
105 * ----------+------------+------------'
106 * source: scratch |
107 * ------------------------------------'
108 */
109 .bst_magic_slot0 = 0,
110 .bst_magic_scratch = BOOT_MAGIC_GOOD,
111 .bst_copy_done_slot0 = 0,
112 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
113 },
114
115 {
116 /* | slot-0 | scratch |
117 * ----------+------------+------------|
118 * magic | Unset | Any |
119 * copy-done | 0xff | N/A |
120 * ----------+------------+------------|
121 * source: varies |
122 * ------------------------------------+------------------------------+
123 * This represents one of two cases: |
124 * o No swaps ever (no status to read, so no harm in checking). |
125 * o Mid-revert; status in slot 0. |
126 * -------------------------------------------------------------------'
127 */
128 .bst_magic_slot0 = BOOT_MAGIC_UNSET,
129 .bst_magic_scratch = 0,
130 .bst_copy_done_slot0 = 0xff,
131 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
132 },
133};
134
135#define BOOT_STATUS_TABLES_COUNT \
136 (sizeof boot_status_tables / sizeof boot_status_tables[0])
137
138/**
139 * This table indicates the next swap type that should be performed. The first
140 * column contains the current swap type. The second column contains the swap
141 * type that should be effected after the first completes.
142 */
143static const uint8_t boot_swap_trans_table[][2] = {
144 /* From To */
145 { BOOT_SWAP_TYPE_REVERT, BOOT_SWAP_TYPE_NONE },
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800146 { BOOT_SWAP_TYPE_PERM, BOOT_SWAP_TYPE_NONE },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800147 { BOOT_SWAP_TYPE_TEST, BOOT_SWAP_TYPE_REVERT },
148};
149
150#define BOOT_SWAP_TRANS_TABLE_SIZE \
151 (sizeof boot_swap_trans_table / sizeof boot_swap_trans_table[0])
152
153/**
154 * Determines where in flash the most recent boot status is stored. The boot
155 * status is necessary for completing a swap that was interrupted by a boot
156 * loader reset.
157 *
158 * @return A BOOT_STATUS_SOURCE_[...] code indicating where * status should be read from.
159 */
160static int
161boot_status_source(void)
162{
163 const struct boot_status_table *table;
164 struct boot_swap_state state_scratch;
165 struct boot_swap_state state_slot0;
166 struct boot_swap_state state_slot1;
167 int rc;
168 int i;
169
170 rc = boot_read_swap_state_img(0, &state_slot0);
171 assert(rc == 0);
172
173 rc = boot_read_swap_state_img(1, &state_slot1);
174 assert(rc == 0);
175
176 rc = boot_read_swap_state_scratch(&state_scratch);
177 assert(rc == 0);
178
179 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
180 table = boot_status_tables + i;
181
182 if ((table->bst_magic_slot0 == 0 ||
183 table->bst_magic_slot0 == state_slot0.magic) &&
184 (table->bst_magic_scratch == 0 ||
185 table->bst_magic_scratch == state_scratch.magic) &&
186 (table->bst_copy_done_slot0 == 0 ||
187 table->bst_copy_done_slot0 == state_slot0.copy_done)) {
188
189 return table->bst_status_source;
190 }
191 }
192
193 return BOOT_STATUS_SOURCE_NONE;
194}
195
196/**
197 * Calculates the type of swap that just completed.
198 */
199static int
200boot_previous_swap_type(void)
201{
202 int post_swap_type;
203 int i;
204
205 post_swap_type = boot_swap_type();
206
207 for (i = 0; i < BOOT_SWAP_TRANS_TABLE_SIZE; i++){
208 if (boot_swap_trans_table[i][1] == post_swap_type) {
209 return boot_swap_trans_table[i][0];
210 }
211 }
212
213 /* XXX: Temporary assert. */
214 assert(0);
215
216 return BOOT_SWAP_TYPE_REVERT;
217}
218
219static int
220boot_read_image_header(int slot, struct image_header *out_hdr)
221{
222 const struct flash_area *fap;
223 int area_id;
224 int rc;
225
226 area_id = flash_area_id_from_image_slot(slot);
227 rc = flash_area_open(area_id, &fap);
228 if (rc != 0) {
229 rc = BOOT_EFLASH;
230 goto done;
231 }
232
233 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
234 if (rc != 0) {
235 rc = BOOT_EFLASH;
236 goto done;
237 }
238
239 rc = 0;
240
241done:
242 flash_area_close(fap);
243 return rc;
244}
245
246static int
247boot_read_image_headers(void)
248{
249 int rc;
250 int i;
251
252 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
253 rc = boot_read_image_header(i, &boot_data.imgs[i].hdr);
254 if (rc != 0) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800255 /* If at least one header was read successfully, then the boot
256 * loader can attempt a boot. Failure to read any headers is a
257 * fatal error.
258 */
259 if (i > 0) {
260 return 0;
261 } else {
262 return rc;
263 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800264 }
265 }
266
267 return 0;
268}
269
270static uint8_t
271boot_write_sz(void)
272{
273 uint8_t elem_sz;
274 uint8_t align;
275
276 /* Figure out what size to write update status update as. The size depends
277 * on what the minimum write size is for scratch area, active image slot.
278 * We need to use the bigger of those 2 values.
279 */
280 elem_sz = hal_flash_align(boot_data.imgs[0].sectors[0].fa_device_id);
281 align = hal_flash_align(boot_data.scratch_sector.fa_device_id);
282 if (align > elem_sz) {
283 elem_sz = align;
284 }
285
286 return elem_sz;
287}
288
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800289static int
290boot_slots_compatible(void)
291{
292 const struct flash_area *sector0;
293 const struct flash_area *sector1;
294 int i;
295
296 /* Ensure both image slots have identical sector layouts. */
297 if (boot_data.imgs[0].num_sectors != boot_data.imgs[1].num_sectors) {
298 return 0;
299 }
300 for (i = 0; i < boot_data.imgs[0].num_sectors; i++) {
301 sector0 = boot_data.imgs[0].sectors + i;
302 sector1 = boot_data.imgs[1].sectors + i;
303 if (sector0->fa_size != sector1->fa_size) {
304 return 0;
305 }
306 }
307
308 return 1;
309}
310
Christopher Collins92ea77f2016-12-12 15:59:26 -0800311/**
312 * Determines the sector layout of both image slots and the scratch area.
313 * This information is necessary for calculating the number of bytes to erase
314 * and copy during an image swap. The information collected during this
315 * function is used to populate the boot_data global.
316 */
317static int
318boot_read_sectors(void)
319{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800320 const struct flash_area *scratch;
321 int num_sectors_slot0;
322 int num_sectors_slot1;
323 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800324
325 num_sectors_slot0 = BOOT_MAX_IMG_SECTORS;
326 rc = flash_area_to_sectors(FLASH_AREA_IMAGE_0, &num_sectors_slot0,
327 boot_data.imgs[0].sectors);
328 if (rc != 0) {
329 return BOOT_EFLASH;
330 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800331 boot_data.imgs[0].num_sectors = num_sectors_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800332
333 num_sectors_slot1 = BOOT_MAX_IMG_SECTORS;
334 rc = flash_area_to_sectors(FLASH_AREA_IMAGE_1, &num_sectors_slot1,
335 boot_data.imgs[1].sectors);
336 if (rc != 0) {
337 return BOOT_EFLASH;
338 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800339 boot_data.imgs[1].num_sectors = num_sectors_slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800340
341 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &scratch);
342 if (rc != 0) {
343 return BOOT_EFLASH;
344 }
345 boot_data.scratch_sector = *scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800346
347 boot_data.write_sz = boot_write_sz();
348
349 return 0;
350}
351
352static uint32_t
353boot_status_internal_off(int idx, int state, int elem_sz)
354{
355 int idx_sz;
356
357 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
358
359 return idx * idx_sz + state * elem_sz;
360}
361
362/**
363 * Reads the status of a partially-completed swap, if any. This is necessary
364 * to recover in case the boot lodaer was reset in the middle of a swap
365 * operation.
366 */
367static int
368boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
369{
370 uint32_t off;
371 uint8_t status;
372 int found;
373 int rc;
374 int i;
375
376 off = boot_status_off(fap);
377
378 found = 0;
379 for (i = 0; i < BOOT_STATUS_MAX_ENTRIES; i++) {
380 rc = flash_area_read(fap, off + i * boot_data.write_sz, &status, 1);
381 if (rc != 0) {
382 return BOOT_EFLASH;
383 }
384
385 if (status == 0xff) {
386 if (found) {
387 break;
388 }
389 } else if (!found) {
390 found = 1;
391 }
392 }
393
394 if (found) {
395 i--;
396 bs->idx = i / BOOT_STATUS_STATE_COUNT;
397 bs->state = i % BOOT_STATUS_STATE_COUNT;
398 }
399
400 return 0;
401}
402
403/**
404 * Reads the boot status from the flash. The boot status contains
405 * the current state of an interrupted image copy operation. If the boot
406 * status is not present, or it indicates that previous copy finished,
407 * there is no operation in progress.
408 */
409static int
410boot_read_status(struct boot_status *bs)
411{
412 const struct flash_area *fap;
413 int status_loc;
414 int area_id;
415 int rc;
416
417 memset(bs, 0, sizeof *bs);
418
419 status_loc = boot_status_source();
420 switch (status_loc) {
421 case BOOT_STATUS_SOURCE_NONE:
422 return 0;
423
424 case BOOT_STATUS_SOURCE_SCRATCH:
425 area_id = FLASH_AREA_IMAGE_SCRATCH;
426 break;
427
428 case BOOT_STATUS_SOURCE_SLOT0:
429 area_id = FLASH_AREA_IMAGE_0;
430 break;
431
432 default:
433 assert(0);
434 return BOOT_EBADARGS;
435 }
436
437 rc = flash_area_open(area_id, &fap);
438 if (rc != 0) {
439 return BOOT_EFLASH;
440 }
441
442 rc = boot_read_status_bytes(fap, bs);
443 if (rc != 0) {
444 return rc;
445 }
446
447 return 0;
448}
449
450/**
451 * Writes the supplied boot status to the flash file system. The boot status
452 * contains the current state of an in-progress image copy operation.
453 *
454 * @param bs The boot status to write.
455 *
456 * @return 0 on success; nonzero on failure.
457 */
458int
459boot_write_status(struct boot_status *bs)
460{
461 const struct flash_area *fap;
462 uint32_t off;
463 int area_id;
464 int rc;
465
466 if (bs->idx == 0) {
467 /* Write to scratch. */
468 area_id = FLASH_AREA_IMAGE_SCRATCH;
469 } else {
470 /* Write to slot 0. */
471 area_id = FLASH_AREA_IMAGE_0;
472 }
473
474 rc = flash_area_open(area_id, &fap);
475 if (rc != 0) {
476 rc = BOOT_EFLASH;
477 goto done;
478 }
479
480 off = boot_status_off(fap) +
481 boot_status_internal_off(bs->idx, bs->state, boot_data.write_sz);
482
483 rc = flash_area_write(fap, off, &bs->state, 1);
484 if (rc != 0) {
485 rc = BOOT_EFLASH;
486 goto done;
487 }
488
489 rc = 0;
490
491done:
492 flash_area_close(fap);
493 return rc;
494}
495
496/*
497 * Validate image hash/signature in a slot.
498 */
499static int
500boot_image_check(struct image_header *hdr, const struct flash_area *fap)
501{
David Browndb1d9d32017-01-06 11:07:54 -0700502 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800503
Christopher Collins92ea77f2016-12-12 15:59:26 -0800504 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
505 NULL, 0, NULL)) {
506 return BOOT_EBADIMAGE;
507 }
508 return 0;
509}
510
511static int
512split_image_check(struct image_header *app_hdr,
513 const struct flash_area *app_fap,
514 struct image_header *loader_hdr,
515 const struct flash_area *loader_fap)
516{
517 static void *tmpbuf;
518 uint8_t loader_hash[32];
519
520 if (!tmpbuf) {
521 tmpbuf = malloc(BOOT_TMPBUF_SZ);
522 if (!tmpbuf) {
523 return BOOT_ENOMEM;
524 }
525 }
526
527 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
528 NULL, 0, loader_hash)) {
529 return BOOT_EBADIMAGE;
530 }
531
532 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
533 loader_hash, 32, NULL)) {
534 return BOOT_EBADIMAGE;
535 }
536
537 return 0;
538}
539
540static int
541boot_validate_slot1(void)
542{
543 const struct flash_area *fap;
544 int rc;
545
546 if (boot_data.imgs[1].hdr.ih_magic == 0xffffffff ||
547 boot_data.imgs[1].hdr.ih_flags & IMAGE_F_NON_BOOTABLE) {
548
549 /* No bootable image in slot 1; continue booting from slot 0. */
550 return -1;
551 }
552
553 /* Image in slot 1 is invalid. Erase the image and continue booting
554 * from slot 0.
555 */
556 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap);
557 if (rc != 0) {
558 return BOOT_EFLASH;
559 }
560
561 if (boot_data.imgs[1].hdr.ih_magic != IMAGE_MAGIC ||
562 boot_image_check(&boot_data.imgs[1].hdr, fap) != 0) {
563
564 /* Image in slot 1 is invalid. Erase the image and continue booting
565 * from slot 0.
566 */
567 flash_area_erase(fap, 0, fap->fa_size);
568 return -1;
569 }
570
571 flash_area_close(fap);
572
573 /* Image in slot 1 is valid. */
574 return 0;
575}
576
577/**
578 * Determines which swap operation to perform, if any. If it is determined
579 * that a swap operation is required, the image in the second slot is checked
580 * for validity. If the image in the second slot is invalid, it is erased, and
581 * a swap type of "none" is indicated.
582 *
583 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
584 */
585static int
586boot_validated_swap_type(void)
587{
588 int swap_type;
589 int rc;
590
591 swap_type = boot_swap_type();
592 if (swap_type == BOOT_SWAP_TYPE_NONE) {
593 /* Continue using slot 0. */
594 return BOOT_SWAP_TYPE_NONE;
595 }
596
597 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
598 rc = boot_validate_slot1();
599 if (rc != 0) {
600 return BOOT_SWAP_TYPE_FAIL;
601 }
602
603 return swap_type;
604}
605
606/**
607 * Calculates the number of sectors the scratch area can contain. A "last"
608 * source sector is specified because images are copied backwards in flash
609 * (final index to index number 0).
610 *
611 * @param last_sector_idx The index of the last source sector
612 * (inclusive).
613 * @param out_first_sector_idx The index of the first source sector
614 * (inclusive) gets written here.
615 *
616 * @return The number of bytes comprised by the
617 * [first-sector, last-sector] range.
618 */
619static uint32_t
620boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
621{
622 uint32_t new_sz;
623 uint32_t sz;
624 int i;
625
626 sz = 0;
627
628 for (i = last_sector_idx; i >= 0; i--) {
629 new_sz = sz + boot_data.imgs[0].sectors[i].fa_size;
630 if (new_sz > boot_data.scratch_sector.fa_size) {
631 break;
632 }
633 sz = new_sz;
634 }
635
636 /* i currently refers to a sector that doesn't fit or it is -1 because all
637 * sectors have been processed. In both cases, exclude sector i.
638 */
639 *out_first_sector_idx = i + 1;
640 return sz;
641}
642
643/**
644 * Erases a region of flash.
645 *
646 * @param flash_area_idx The ID of the flash area containing the region
647 * to erase.
648 * @param off The offset within the flash area to start the
649 * erase.
650 * @param sz The number of bytes to erase.
651 *
652 * @return 0 on success; nonzero on failure.
653 */
654static int
655boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
656{
657 const struct flash_area *fap;
658 int rc;
659
660 rc = flash_area_open(flash_area_id, &fap);
661 if (rc != 0) {
662 rc = BOOT_EFLASH;
663 goto done;
664 }
665
666 rc = flash_area_erase(fap, off, sz);
667 if (rc != 0) {
668 rc = BOOT_EFLASH;
669 goto done;
670 }
671
672 rc = 0;
673
674done:
675 flash_area_close(fap);
676 return rc;
677}
678
679/**
680 * Copies the contents of one flash region to another. You must erase the
681 * destination region prior to calling this function.
682 *
683 * @param flash_area_id_src The ID of the source flash area.
684 * @param flash_area_id_dst The ID of the destination flash area.
685 * @param off_src The offset within the source flash area to
686 * copy from.
687 * @param off_dst The offset within the destination flash area to
688 * copy to.
689 * @param sz The number of bytes to copy.
690 *
691 * @return 0 on success; nonzero on failure.
692 */
693static int
694boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
695 uint32_t off_src, uint32_t off_dst, uint32_t sz)
696{
697 const struct flash_area *fap_src;
698 const struct flash_area *fap_dst;
699 uint32_t bytes_copied;
700 int chunk_sz;
701 int rc;
702
703 static uint8_t buf[1024];
704
705 fap_src = NULL;
706 fap_dst = NULL;
707
708 rc = flash_area_open(flash_area_id_src, &fap_src);
709 if (rc != 0) {
710 rc = BOOT_EFLASH;
711 goto done;
712 }
713
714 rc = flash_area_open(flash_area_id_dst, &fap_dst);
715 if (rc != 0) {
716 rc = BOOT_EFLASH;
717 goto done;
718 }
719
720 bytes_copied = 0;
721 while (bytes_copied < sz) {
722 if (sz - bytes_copied > sizeof buf) {
723 chunk_sz = sizeof buf;
724 } else {
725 chunk_sz = sz - bytes_copied;
726 }
727
728 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
729 if (rc != 0) {
730 rc = BOOT_EFLASH;
731 goto done;
732 }
733
734 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
735 if (rc != 0) {
736 rc = BOOT_EFLASH;
737 goto done;
738 }
739
740 bytes_copied += chunk_sz;
741 }
742
743 rc = 0;
744
745done:
746 flash_area_close(fap_src);
747 flash_area_close(fap_dst);
748 return rc;
749}
750
751/**
752 * Swaps the contents of two flash regions within the two image slots.
753 *
754 * @param idx The index of the first sector in the range of
755 * sectors being swapped.
756 * @param sz The number of bytes to swap.
757 * @param bs The current boot status. This struct gets
758 * updated according to the outcome.
759 *
760 * @return 0 on success; nonzero on failure.
761 */
762static int
763boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
764{
765 uint32_t copy_sz;
766 uint32_t img_off;
767 int rc;
768
769 /* Calculate offset from start of image area. */
770 img_off = boot_data.imgs[0].sectors[idx].fa_off -
771 boot_data.imgs[0].sectors[0].fa_off;
772
773 if (bs->state == 0) {
774 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
775 if (rc != 0) {
776 return rc;
777 }
778
779 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
780 img_off, 0, sz);
781 if (rc != 0) {
782 return rc;
783 }
784
785 bs->state = 1;
786 (void)boot_write_status(bs);
787 }
788 if (bs->state == 1) {
789 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
790 if (rc != 0) {
791 return rc;
792 }
793
794 copy_sz = sz;
795 if (boot_data.imgs[0].sectors[idx].fa_off + sz >=
796 boot_data.imgs[1].sectors[0].fa_off) {
797
798 /* This is the end of the area. Don't copy the image state into
799 * slot 1.
800 */
801 copy_sz -= boot_trailer_sz(boot_data.write_sz);
802 }
803
804 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
805 img_off, img_off, copy_sz);
806 if (rc != 0) {
807 return rc;
808 }
809
810 bs->state = 2;
811 (void)boot_write_status(bs);
812 }
813 if (bs->state == 2) {
814 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
815 if (rc != 0) {
816 return rc;
817 }
818
819 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
820 0, img_off, sz);
821 if (rc != 0) {
822 return rc;
823 }
824
825 bs->idx++;
826 bs->state = 0;
827 (void)boot_write_status(bs);
828 }
829
830 return 0;
831}
832
833/**
834 * Swaps the two images in flash. If a prior copy operation was interrupted
835 * by a system reset, this function completes that operation.
836 *
837 * @param bs The current boot status. This function reads
838 * this struct to determine if it is resuming
839 * an interrupted swap operation. This
840 * function writes the updated status to this
841 * function on return.
842 *
843 * @return 0 on success; nonzero on failure.
844 */
845static int
846boot_copy_image(struct boot_status *bs)
847{
848 uint32_t sz;
849 int first_sector_idx;
850 int last_sector_idx;
851 int swap_idx;
852
853 swap_idx = 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800854 last_sector_idx = boot_data.imgs[0].num_sectors - 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800855 while (last_sector_idx >= 0) {
856 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
857 if (swap_idx >= bs->idx) {
858 boot_swap_sectors(first_sector_idx, sz, bs);
859 }
860
861 last_sector_idx = first_sector_idx - 1;
862 swap_idx++;
863 }
864
865 return 0;
866}
867
868/**
869 * Marks a test image in slot 0 as fully copied.
870 */
871static int
872boot_finalize_test_swap(void)
873{
874 const struct flash_area *fap;
875 int rc;
876
877 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
878 if (rc != 0) {
879 return BOOT_EFLASH;
880 }
881
882 rc = boot_write_copy_done(fap);
883 if (rc != 0) {
884 return rc;
885 }
886
887 return 0;
888}
889
890/**
891 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
892 * the status bytes from the image revert operation don't get processed on a
893 * subsequent boot.
894 */
895static int
896boot_finalize_revert_swap(void)
897{
898 const struct flash_area *fap;
899 struct boot_swap_state state_slot0;
900 int rc;
901
902 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
903 if (rc != 0) {
904 return BOOT_EFLASH;
905 }
906
907 rc = boot_read_swap_state(fap, &state_slot0);
908 if (rc != 0) {
909 return BOOT_EFLASH;
910 }
911
912 if (state_slot0.magic == BOOT_MAGIC_UNSET) {
913 rc = boot_write_magic(fap);
914 if (rc != 0) {
915 return rc;
916 }
917 }
918
919 if (state_slot0.copy_done == 0xff) {
920 rc = boot_write_copy_done(fap);
921 if (rc != 0) {
922 return rc;
923 }
924 }
925
926 if (state_slot0.image_ok == 0xff) {
927 rc = boot_write_image_ok(fap);
928 if (rc != 0) {
929 return rc;
930 }
931 }
932
933 return 0;
934}
935
936/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800937 * Performs an image swap if one is required.
938 *
939 * @param out_swap_type On success, the type of swap performed gets
940 * written here.
941 *
942 * @return 0 on success; nonzero on failure.
943 */
944static int
945boot_swap_if_needed(int *out_swap_type)
946{
947 struct boot_status bs;
948 int swap_type;
949 int rc;
950
951 /* Determine if we rebooted in the middle of an image swap
952 * operation.
953 */
954 rc = boot_read_status(&bs);
955 if (rc != 0) {
956 return rc;
957 }
958
959 /* If a partial swap was detected, complete it. */
960 if (bs.idx != 0 || bs.state != 0) {
961 rc = boot_copy_image(&bs);
962 assert(rc == 0);
963
964 /* Extrapolate the type of the partial swap. We need this
965 * information to know how to mark the swap complete in flash.
966 */
967 swap_type = boot_previous_swap_type();
968 } else {
969 swap_type = boot_validated_swap_type();
970 switch (swap_type) {
971 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800972 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800973 case BOOT_SWAP_TYPE_REVERT:
974 rc = boot_copy_image(&bs);
975 assert(rc == 0);
976 break;
977 }
978 }
979
980 *out_swap_type = swap_type;
981 return 0;
982}
983
984/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800985 * Prepares the booting process. This function moves images around in flash as
986 * appropriate, and tells you what address to boot from.
987 *
988 * @param rsp On success, indicates how booting should occur.
989 *
990 * @return 0 on success; nonzero on failure.
991 */
992int
993boot_go(struct boot_rsp *rsp)
994{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800995 int swap_type;
996 int slot;
997 int rc;
998
999 /* The array of slot sectors are defined here (as opposed to file scope) so
1000 * that they don't get allocated for non-boot-loader apps. This is
1001 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001002 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001003 */
1004 static struct flash_area slot0_sectors[BOOT_MAX_IMG_SECTORS];
1005 static struct flash_area slot1_sectors[BOOT_MAX_IMG_SECTORS];
1006 boot_data.imgs[0].sectors = slot0_sectors;
1007 boot_data.imgs[1].sectors = slot1_sectors;
1008
1009 /* Determine the sector layout of the image slots and scratch area. */
1010 rc = boot_read_sectors();
1011 if (rc != 0) {
1012 return rc;
1013 }
1014
1015 /* Attempt to read an image header from each slot. */
1016 rc = boot_read_image_headers();
1017 if (rc != 0) {
1018 return rc;
1019 }
1020
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001021 /* If the image slots aren't compatible, no swap is possible. Just boot
1022 * into slot 0.
1023 */
1024 if (boot_slots_compatible()) {
1025 rc = boot_swap_if_needed(&swap_type);
1026 if (rc != 0) {
1027 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001028 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001029 } else {
1030 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001031 }
1032
1033 switch (swap_type) {
1034 case BOOT_SWAP_TYPE_NONE:
1035 slot = 0;
1036 break;
1037
1038 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001039 case BOOT_SWAP_TYPE_PERM:
Christopher Collins92ea77f2016-12-12 15:59:26 -08001040 slot = 1;
1041 boot_finalize_test_swap();
1042 break;
1043
1044 case BOOT_SWAP_TYPE_REVERT:
1045 slot = 1;
1046 boot_finalize_revert_swap();
1047 break;
1048
1049 case BOOT_SWAP_TYPE_FAIL:
1050 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1051 * try to boot into it again on the next reboot. Do this by pretending
1052 * we just reverted back to slot 0.
1053 */
1054 slot = 0;
1055 boot_finalize_revert_swap();
1056 break;
1057
1058 default:
1059 assert(0);
1060 slot = 0;
1061 break;
1062 }
1063
1064 /* Always boot from the primary slot. */
1065 rsp->br_flash_id = boot_data.imgs[0].sectors[0].fa_device_id;
1066 rsp->br_image_addr = boot_data.imgs[0].sectors[0].fa_off;
1067 rsp->br_hdr = &boot_data.imgs[slot].hdr;
1068
1069 return 0;
1070}
1071
1072int
1073split_go(int loader_slot, int split_slot, void **entry)
1074{
1075 const struct flash_area *loader_fap;
1076 const struct flash_area *app_fap;
1077 struct flash_area *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001078 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001079 int loader_flash_id;
1080 int app_flash_id;
1081 int rc;
1082
1083 app_fap = NULL;
1084 loader_fap = NULL;
1085
1086 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1087 if (sectors == NULL) {
1088 rc = SPLIT_GO_ERR;
1089 goto done;
1090 }
1091 boot_data.imgs[0].sectors = sectors + 0;
1092 boot_data.imgs[1].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1093
1094 /* Determine the sector layout of the image slots and scratch area. */
1095 rc = boot_read_sectors();
1096 if (rc != 0) {
1097 rc = SPLIT_GO_ERR;
1098 goto done;
1099 }
1100
1101 rc = boot_read_image_headers();
1102 if (rc != 0) {
1103 goto done;
1104 }
1105
1106 app_flash_id = flash_area_id_from_image_slot(split_slot);
1107 rc = flash_area_open(app_flash_id, &app_fap);
1108 if (rc != 0) {
1109 rc = BOOT_EFLASH;
1110 goto done;
1111 }
1112
1113 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1114 rc = flash_area_open(loader_flash_id, &loader_fap);
1115 if (rc != 0) {
1116 rc = BOOT_EFLASH;
1117 goto done;
1118 }
1119
1120 /* Don't check the bootable image flag because we could really call a
1121 * bootable or non-bootable image. Just validate that the image check
1122 * passes which is distinct from the normal check.
1123 */
1124 rc = split_image_check(&boot_data.imgs[split_slot].hdr,
1125 app_fap,
1126 &boot_data.imgs[loader_slot].hdr,
1127 loader_fap);
1128 if (rc != 0) {
1129 rc = SPLIT_GO_NON_MATCHING;
1130 goto done;
1131 }
1132
1133 entry_val = boot_data.imgs[split_slot].sectors[0].fa_off +
1134 boot_data.imgs[split_slot].hdr.ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001135 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001136 rc = SPLIT_GO_OK;
1137
1138done:
1139 free(sectors);
1140 flash_area_close(app_fap);
1141 flash_area_close(loader_fap);
1142 return rc;
1143}