blob: 3aadd82c1e9ed90c7ef77ec37d06e08f0a4c9a64 [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;
David Brown9d725462017-01-23 15:50:58 -0700465 uint8_t buf[8];
466 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800467
468 if (bs->idx == 0) {
469 /* Write to scratch. */
470 area_id = FLASH_AREA_IMAGE_SCRATCH;
471 } else {
472 /* Write to slot 0. */
473 area_id = FLASH_AREA_IMAGE_0;
474 }
475
476 rc = flash_area_open(area_id, &fap);
477 if (rc != 0) {
478 rc = BOOT_EFLASH;
479 goto done;
480 }
481
482 off = boot_status_off(fap) +
483 boot_status_internal_off(bs->idx, bs->state, boot_data.write_sz);
484
David Brown9d725462017-01-23 15:50:58 -0700485 align = hal_flash_align(fap->fa_device_id);
486 // ASSERT(align <= 8);
487 memset(buf, 0xFF, 8);
488 buf[0] = bs->state;
489
490 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800491 if (rc != 0) {
492 rc = BOOT_EFLASH;
493 goto done;
494 }
495
496 rc = 0;
497
498done:
499 flash_area_close(fap);
500 return rc;
501}
502
503/*
504 * Validate image hash/signature in a slot.
505 */
506static int
507boot_image_check(struct image_header *hdr, const struct flash_area *fap)
508{
David Browndb1d9d32017-01-06 11:07:54 -0700509 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800510
Christopher Collins92ea77f2016-12-12 15:59:26 -0800511 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
512 NULL, 0, NULL)) {
513 return BOOT_EBADIMAGE;
514 }
515 return 0;
516}
517
518static int
519split_image_check(struct image_header *app_hdr,
520 const struct flash_area *app_fap,
521 struct image_header *loader_hdr,
522 const struct flash_area *loader_fap)
523{
524 static void *tmpbuf;
525 uint8_t loader_hash[32];
526
527 if (!tmpbuf) {
528 tmpbuf = malloc(BOOT_TMPBUF_SZ);
529 if (!tmpbuf) {
530 return BOOT_ENOMEM;
531 }
532 }
533
534 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
535 NULL, 0, loader_hash)) {
536 return BOOT_EBADIMAGE;
537 }
538
539 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
540 loader_hash, 32, NULL)) {
541 return BOOT_EBADIMAGE;
542 }
543
544 return 0;
545}
546
547static int
548boot_validate_slot1(void)
549{
550 const struct flash_area *fap;
551 int rc;
552
553 if (boot_data.imgs[1].hdr.ih_magic == 0xffffffff ||
554 boot_data.imgs[1].hdr.ih_flags & IMAGE_F_NON_BOOTABLE) {
555
556 /* No bootable image in slot 1; continue booting from slot 0. */
557 return -1;
558 }
559
560 /* Image in slot 1 is invalid. Erase the image and continue booting
561 * from slot 0.
562 */
563 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap);
564 if (rc != 0) {
565 return BOOT_EFLASH;
566 }
567
568 if (boot_data.imgs[1].hdr.ih_magic != IMAGE_MAGIC ||
569 boot_image_check(&boot_data.imgs[1].hdr, fap) != 0) {
570
571 /* Image in slot 1 is invalid. Erase the image and continue booting
572 * from slot 0.
573 */
574 flash_area_erase(fap, 0, fap->fa_size);
575 return -1;
576 }
577
578 flash_area_close(fap);
579
580 /* Image in slot 1 is valid. */
581 return 0;
582}
583
584/**
585 * Determines which swap operation to perform, if any. If it is determined
586 * that a swap operation is required, the image in the second slot is checked
587 * for validity. If the image in the second slot is invalid, it is erased, and
588 * a swap type of "none" is indicated.
589 *
590 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
591 */
592static int
593boot_validated_swap_type(void)
594{
595 int swap_type;
596 int rc;
597
598 swap_type = boot_swap_type();
599 if (swap_type == BOOT_SWAP_TYPE_NONE) {
600 /* Continue using slot 0. */
601 return BOOT_SWAP_TYPE_NONE;
602 }
603
604 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
605 rc = boot_validate_slot1();
606 if (rc != 0) {
607 return BOOT_SWAP_TYPE_FAIL;
608 }
609
610 return swap_type;
611}
612
613/**
614 * Calculates the number of sectors the scratch area can contain. A "last"
615 * source sector is specified because images are copied backwards in flash
616 * (final index to index number 0).
617 *
618 * @param last_sector_idx The index of the last source sector
619 * (inclusive).
620 * @param out_first_sector_idx The index of the first source sector
621 * (inclusive) gets written here.
622 *
623 * @return The number of bytes comprised by the
624 * [first-sector, last-sector] range.
625 */
626static uint32_t
627boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
628{
629 uint32_t new_sz;
630 uint32_t sz;
631 int i;
632
633 sz = 0;
634
635 for (i = last_sector_idx; i >= 0; i--) {
636 new_sz = sz + boot_data.imgs[0].sectors[i].fa_size;
637 if (new_sz > boot_data.scratch_sector.fa_size) {
638 break;
639 }
640 sz = new_sz;
641 }
642
643 /* i currently refers to a sector that doesn't fit or it is -1 because all
644 * sectors have been processed. In both cases, exclude sector i.
645 */
646 *out_first_sector_idx = i + 1;
647 return sz;
648}
649
650/**
651 * Erases a region of flash.
652 *
653 * @param flash_area_idx The ID of the flash area containing the region
654 * to erase.
655 * @param off The offset within the flash area to start the
656 * erase.
657 * @param sz The number of bytes to erase.
658 *
659 * @return 0 on success; nonzero on failure.
660 */
661static int
662boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
663{
664 const struct flash_area *fap;
665 int rc;
666
667 rc = flash_area_open(flash_area_id, &fap);
668 if (rc != 0) {
669 rc = BOOT_EFLASH;
670 goto done;
671 }
672
673 rc = flash_area_erase(fap, off, sz);
674 if (rc != 0) {
675 rc = BOOT_EFLASH;
676 goto done;
677 }
678
679 rc = 0;
680
681done:
682 flash_area_close(fap);
683 return rc;
684}
685
686/**
687 * Copies the contents of one flash region to another. You must erase the
688 * destination region prior to calling this function.
689 *
690 * @param flash_area_id_src The ID of the source flash area.
691 * @param flash_area_id_dst The ID of the destination flash area.
692 * @param off_src The offset within the source flash area to
693 * copy from.
694 * @param off_dst The offset within the destination flash area to
695 * copy to.
696 * @param sz The number of bytes to copy.
697 *
698 * @return 0 on success; nonzero on failure.
699 */
700static int
701boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
702 uint32_t off_src, uint32_t off_dst, uint32_t sz)
703{
704 const struct flash_area *fap_src;
705 const struct flash_area *fap_dst;
706 uint32_t bytes_copied;
707 int chunk_sz;
708 int rc;
709
710 static uint8_t buf[1024];
711
712 fap_src = NULL;
713 fap_dst = NULL;
714
715 rc = flash_area_open(flash_area_id_src, &fap_src);
716 if (rc != 0) {
717 rc = BOOT_EFLASH;
718 goto done;
719 }
720
721 rc = flash_area_open(flash_area_id_dst, &fap_dst);
722 if (rc != 0) {
723 rc = BOOT_EFLASH;
724 goto done;
725 }
726
727 bytes_copied = 0;
728 while (bytes_copied < sz) {
729 if (sz - bytes_copied > sizeof buf) {
730 chunk_sz = sizeof buf;
731 } else {
732 chunk_sz = sz - bytes_copied;
733 }
734
735 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
736 if (rc != 0) {
737 rc = BOOT_EFLASH;
738 goto done;
739 }
740
741 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
742 if (rc != 0) {
743 rc = BOOT_EFLASH;
744 goto done;
745 }
746
747 bytes_copied += chunk_sz;
748 }
749
750 rc = 0;
751
752done:
753 flash_area_close(fap_src);
754 flash_area_close(fap_dst);
755 return rc;
756}
757
758/**
759 * Swaps the contents of two flash regions within the two image slots.
760 *
761 * @param idx The index of the first sector in the range of
762 * sectors being swapped.
763 * @param sz The number of bytes to swap.
764 * @param bs The current boot status. This struct gets
765 * updated according to the outcome.
766 *
767 * @return 0 on success; nonzero on failure.
768 */
769static int
770boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
771{
772 uint32_t copy_sz;
773 uint32_t img_off;
774 int rc;
775
776 /* Calculate offset from start of image area. */
777 img_off = boot_data.imgs[0].sectors[idx].fa_off -
778 boot_data.imgs[0].sectors[0].fa_off;
779
780 if (bs->state == 0) {
781 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
782 if (rc != 0) {
783 return rc;
784 }
785
786 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
787 img_off, 0, sz);
788 if (rc != 0) {
789 return rc;
790 }
791
792 bs->state = 1;
793 (void)boot_write_status(bs);
794 }
795 if (bs->state == 1) {
796 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
797 if (rc != 0) {
798 return rc;
799 }
800
801 copy_sz = sz;
802 if (boot_data.imgs[0].sectors[idx].fa_off + sz >=
803 boot_data.imgs[1].sectors[0].fa_off) {
804
805 /* This is the end of the area. Don't copy the image state into
806 * slot 1.
807 */
808 copy_sz -= boot_trailer_sz(boot_data.write_sz);
809 }
810
811 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
812 img_off, img_off, copy_sz);
813 if (rc != 0) {
814 return rc;
815 }
816
817 bs->state = 2;
818 (void)boot_write_status(bs);
819 }
820 if (bs->state == 2) {
821 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
822 if (rc != 0) {
823 return rc;
824 }
825
826 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
827 0, img_off, sz);
828 if (rc != 0) {
829 return rc;
830 }
831
832 bs->idx++;
833 bs->state = 0;
834 (void)boot_write_status(bs);
835 }
836
837 return 0;
838}
839
840/**
841 * Swaps the two images in flash. If a prior copy operation was interrupted
842 * by a system reset, this function completes that operation.
843 *
844 * @param bs The current boot status. This function reads
845 * this struct to determine if it is resuming
846 * an interrupted swap operation. This
847 * function writes the updated status to this
848 * function on return.
849 *
850 * @return 0 on success; nonzero on failure.
851 */
852static int
853boot_copy_image(struct boot_status *bs)
854{
855 uint32_t sz;
856 int first_sector_idx;
857 int last_sector_idx;
858 int swap_idx;
859
860 swap_idx = 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800861 last_sector_idx = boot_data.imgs[0].num_sectors - 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800862 while (last_sector_idx >= 0) {
863 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
864 if (swap_idx >= bs->idx) {
865 boot_swap_sectors(first_sector_idx, sz, bs);
866 }
867
868 last_sector_idx = first_sector_idx - 1;
869 swap_idx++;
870 }
871
872 return 0;
873}
874
875/**
876 * Marks a test image in slot 0 as fully copied.
877 */
878static int
879boot_finalize_test_swap(void)
880{
881 const struct flash_area *fap;
882 int rc;
883
884 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
885 if (rc != 0) {
886 return BOOT_EFLASH;
887 }
888
889 rc = boot_write_copy_done(fap);
890 if (rc != 0) {
891 return rc;
892 }
893
894 return 0;
895}
896
897/**
898 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
899 * the status bytes from the image revert operation don't get processed on a
900 * subsequent boot.
901 */
902static int
903boot_finalize_revert_swap(void)
904{
905 const struct flash_area *fap;
906 struct boot_swap_state state_slot0;
907 int rc;
908
909 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
910 if (rc != 0) {
911 return BOOT_EFLASH;
912 }
913
914 rc = boot_read_swap_state(fap, &state_slot0);
915 if (rc != 0) {
916 return BOOT_EFLASH;
917 }
918
919 if (state_slot0.magic == BOOT_MAGIC_UNSET) {
920 rc = boot_write_magic(fap);
921 if (rc != 0) {
922 return rc;
923 }
924 }
925
926 if (state_slot0.copy_done == 0xff) {
927 rc = boot_write_copy_done(fap);
928 if (rc != 0) {
929 return rc;
930 }
931 }
932
933 if (state_slot0.image_ok == 0xff) {
934 rc = boot_write_image_ok(fap);
935 if (rc != 0) {
936 return rc;
937 }
938 }
939
940 return 0;
941}
942
943/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800944 * Performs an image swap if one is required.
945 *
946 * @param out_swap_type On success, the type of swap performed gets
947 * written here.
948 *
949 * @return 0 on success; nonzero on failure.
950 */
951static int
952boot_swap_if_needed(int *out_swap_type)
953{
954 struct boot_status bs;
955 int swap_type;
956 int rc;
957
958 /* Determine if we rebooted in the middle of an image swap
959 * operation.
960 */
961 rc = boot_read_status(&bs);
962 if (rc != 0) {
963 return rc;
964 }
965
966 /* If a partial swap was detected, complete it. */
967 if (bs.idx != 0 || bs.state != 0) {
968 rc = boot_copy_image(&bs);
969 assert(rc == 0);
970
971 /* Extrapolate the type of the partial swap. We need this
972 * information to know how to mark the swap complete in flash.
973 */
974 swap_type = boot_previous_swap_type();
975 } else {
976 swap_type = boot_validated_swap_type();
977 switch (swap_type) {
978 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800979 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800980 case BOOT_SWAP_TYPE_REVERT:
981 rc = boot_copy_image(&bs);
982 assert(rc == 0);
983 break;
984 }
985 }
986
987 *out_swap_type = swap_type;
988 return 0;
989}
990
991/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800992 * Prepares the booting process. This function moves images around in flash as
993 * appropriate, and tells you what address to boot from.
994 *
995 * @param rsp On success, indicates how booting should occur.
996 *
997 * @return 0 on success; nonzero on failure.
998 */
999int
1000boot_go(struct boot_rsp *rsp)
1001{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001002 int swap_type;
1003 int slot;
1004 int rc;
1005
1006 /* The array of slot sectors are defined here (as opposed to file scope) so
1007 * that they don't get allocated for non-boot-loader apps. This is
1008 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001009 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001010 */
1011 static struct flash_area slot0_sectors[BOOT_MAX_IMG_SECTORS];
1012 static struct flash_area slot1_sectors[BOOT_MAX_IMG_SECTORS];
1013 boot_data.imgs[0].sectors = slot0_sectors;
1014 boot_data.imgs[1].sectors = slot1_sectors;
1015
1016 /* Determine the sector layout of the image slots and scratch area. */
1017 rc = boot_read_sectors();
1018 if (rc != 0) {
1019 return rc;
1020 }
1021
1022 /* Attempt to read an image header from each slot. */
1023 rc = boot_read_image_headers();
1024 if (rc != 0) {
1025 return rc;
1026 }
1027
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001028 /* If the image slots aren't compatible, no swap is possible. Just boot
1029 * into slot 0.
1030 */
1031 if (boot_slots_compatible()) {
1032 rc = boot_swap_if_needed(&swap_type);
1033 if (rc != 0) {
1034 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001035 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001036 } else {
1037 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001038 }
1039
1040 switch (swap_type) {
1041 case BOOT_SWAP_TYPE_NONE:
1042 slot = 0;
1043 break;
1044
1045 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001046 case BOOT_SWAP_TYPE_PERM:
Christopher Collins92ea77f2016-12-12 15:59:26 -08001047 slot = 1;
1048 boot_finalize_test_swap();
1049 break;
1050
1051 case BOOT_SWAP_TYPE_REVERT:
1052 slot = 1;
1053 boot_finalize_revert_swap();
1054 break;
1055
1056 case BOOT_SWAP_TYPE_FAIL:
1057 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1058 * try to boot into it again on the next reboot. Do this by pretending
1059 * we just reverted back to slot 0.
1060 */
1061 slot = 0;
1062 boot_finalize_revert_swap();
1063 break;
1064
1065 default:
1066 assert(0);
1067 slot = 0;
1068 break;
1069 }
1070
1071 /* Always boot from the primary slot. */
1072 rsp->br_flash_id = boot_data.imgs[0].sectors[0].fa_device_id;
1073 rsp->br_image_addr = boot_data.imgs[0].sectors[0].fa_off;
1074 rsp->br_hdr = &boot_data.imgs[slot].hdr;
1075
1076 return 0;
1077}
1078
1079int
1080split_go(int loader_slot, int split_slot, void **entry)
1081{
1082 const struct flash_area *loader_fap;
1083 const struct flash_area *app_fap;
1084 struct flash_area *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001085 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001086 int loader_flash_id;
1087 int app_flash_id;
1088 int rc;
1089
1090 app_fap = NULL;
1091 loader_fap = NULL;
1092
1093 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1094 if (sectors == NULL) {
1095 rc = SPLIT_GO_ERR;
1096 goto done;
1097 }
1098 boot_data.imgs[0].sectors = sectors + 0;
1099 boot_data.imgs[1].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1100
1101 /* Determine the sector layout of the image slots and scratch area. */
1102 rc = boot_read_sectors();
1103 if (rc != 0) {
1104 rc = SPLIT_GO_ERR;
1105 goto done;
1106 }
1107
1108 rc = boot_read_image_headers();
1109 if (rc != 0) {
1110 goto done;
1111 }
1112
1113 app_flash_id = flash_area_id_from_image_slot(split_slot);
1114 rc = flash_area_open(app_flash_id, &app_fap);
1115 if (rc != 0) {
1116 rc = BOOT_EFLASH;
1117 goto done;
1118 }
1119
1120 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1121 rc = flash_area_open(loader_flash_id, &loader_fap);
1122 if (rc != 0) {
1123 rc = BOOT_EFLASH;
1124 goto done;
1125 }
1126
1127 /* Don't check the bootable image flag because we could really call a
1128 * bootable or non-bootable image. Just validate that the image check
1129 * passes which is distinct from the normal check.
1130 */
1131 rc = split_image_check(&boot_data.imgs[split_slot].hdr,
1132 app_fap,
1133 &boot_data.imgs[loader_slot].hdr,
1134 loader_fap);
1135 if (rc != 0) {
1136 rc = SPLIT_GO_NON_MATCHING;
1137 goto done;
1138 }
1139
1140 entry_val = boot_data.imgs[split_slot].sectors[0].fa_off +
1141 boot_data.imgs[split_slot].hdr.ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001142 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001143 rc = SPLIT_GO_OK;
1144
1145done:
1146 free(sectors);
1147 flash_area_close(app_fap);
1148 flash_area_close(loader_fap);
1149 return rc;
1150}