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