blob: e0867d69915713e831bcd670862fee27abcf6ea7 [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
Marti Bolivarfd20c762017-02-07 16:52:50 -050038#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
39#include "bootutil/bootutil_log.h"
40
Christopher Collins92ea77f2016-12-12 15:59:26 -080041#define BOOT_MAX_IMG_SECTORS 120
42
43/** Number of image slots in flash; currently limited to two. */
44#define BOOT_NUM_SLOTS 2
45
46static struct {
47 struct {
48 struct image_header hdr;
49 struct flash_area *sectors;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -080050 int num_sectors;
Christopher Collins92ea77f2016-12-12 15:59:26 -080051 } imgs[BOOT_NUM_SLOTS];
52
Christopher Collins92ea77f2016-12-12 15:59:26 -080053 struct flash_area scratch_sector;
54
55 uint8_t write_sz;
56} boot_data;
57
58struct boot_status_table {
59 /**
60 * For each field, a value of 0 means "any".
61 */
62 uint8_t bst_magic_slot0;
63 uint8_t bst_magic_scratch;
64 uint8_t bst_copy_done_slot0;
65 uint8_t bst_status_source;
66};
67
68/**
69 * This set of tables maps swap state contents to boot status location.
70 * When searching for a match, these tables must be iterated in order.
71 */
72static const struct boot_status_table boot_status_tables[] = {
73 {
74 /* | slot-0 | scratch |
75 * ----------+------------+------------|
76 * magic | Good | Any |
77 * copy-done | 0x01 | N/A |
78 * ----------+------------+------------'
79 * source: none |
80 * ------------------------------------'
81 */
82 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
83 .bst_magic_scratch = 0,
84 .bst_copy_done_slot0 = 0x01,
85 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
86 },
87
88 {
89 /* | slot-0 | scratch |
90 * ----------+------------+------------|
91 * magic | Good | Any |
92 * copy-done | 0xff | N/A |
93 * ----------+------------+------------'
94 * source: slot 0 |
95 * ------------------------------------'
96 */
97 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
98 .bst_magic_scratch = 0,
99 .bst_copy_done_slot0 = 0xff,
100 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
101 },
102
103 {
104 /* | slot-0 | scratch |
105 * ----------+------------+------------|
106 * magic | Any | Good |
107 * copy-done | Any | N/A |
108 * ----------+------------+------------'
109 * source: scratch |
110 * ------------------------------------'
111 */
112 .bst_magic_slot0 = 0,
113 .bst_magic_scratch = BOOT_MAGIC_GOOD,
114 .bst_copy_done_slot0 = 0,
115 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
116 },
117
118 {
119 /* | slot-0 | scratch |
120 * ----------+------------+------------|
121 * magic | Unset | Any |
122 * copy-done | 0xff | N/A |
123 * ----------+------------+------------|
124 * source: varies |
125 * ------------------------------------+------------------------------+
126 * This represents one of two cases: |
127 * o No swaps ever (no status to read, so no harm in checking). |
128 * o Mid-revert; status in slot 0. |
129 * -------------------------------------------------------------------'
130 */
131 .bst_magic_slot0 = BOOT_MAGIC_UNSET,
132 .bst_magic_scratch = 0,
133 .bst_copy_done_slot0 = 0xff,
134 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
135 },
136};
137
138#define BOOT_STATUS_TABLES_COUNT \
139 (sizeof boot_status_tables / sizeof boot_status_tables[0])
140
141/**
142 * This table indicates the next swap type that should be performed. The first
143 * column contains the current swap type. The second column contains the swap
144 * type that should be effected after the first completes.
145 */
146static const uint8_t boot_swap_trans_table[][2] = {
147 /* From To */
148 { BOOT_SWAP_TYPE_REVERT, BOOT_SWAP_TYPE_NONE },
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800149 { BOOT_SWAP_TYPE_PERM, BOOT_SWAP_TYPE_NONE },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800150 { BOOT_SWAP_TYPE_TEST, BOOT_SWAP_TYPE_REVERT },
151};
152
153#define BOOT_SWAP_TRANS_TABLE_SIZE \
154 (sizeof boot_swap_trans_table / sizeof boot_swap_trans_table[0])
155
Marti Bolivarfd20c762017-02-07 16:52:50 -0500156#define BOOT_LOG_SWAP_STATE(area, state) \
157 BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \
158 (area), \
159 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
160 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
161 "bad"), \
162 (state)->copy_done, \
163 (state)->image_ok)
164
Christopher Collins92ea77f2016-12-12 15:59:26 -0800165/**
166 * Determines where in flash the most recent boot status is stored. The boot
167 * status is necessary for completing a swap that was interrupted by a boot
168 * loader reset.
169 *
170 * @return A BOOT_STATUS_SOURCE_[...] code indicating where * status should be read from.
171 */
172static int
173boot_status_source(void)
174{
175 const struct boot_status_table *table;
176 struct boot_swap_state state_scratch;
177 struct boot_swap_state state_slot0;
178 struct boot_swap_state state_slot1;
179 int rc;
180 int i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500181 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182
183 rc = boot_read_swap_state_img(0, &state_slot0);
184 assert(rc == 0);
185
186 rc = boot_read_swap_state_img(1, &state_slot1);
187 assert(rc == 0);
188
189 rc = boot_read_swap_state_scratch(&state_scratch);
190 assert(rc == 0);
191
Marti Bolivarfd20c762017-02-07 16:52:50 -0500192 BOOT_LOG_SWAP_STATE("Image 0", &state_slot0);
193 BOOT_LOG_SWAP_STATE("Image 1", &state_slot1);
194 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
195
Christopher Collins92ea77f2016-12-12 15:59:26 -0800196 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
197 table = boot_status_tables + i;
198
199 if ((table->bst_magic_slot0 == 0 ||
200 table->bst_magic_slot0 == state_slot0.magic) &&
201 (table->bst_magic_scratch == 0 ||
202 table->bst_magic_scratch == state_scratch.magic) &&
203 (table->bst_copy_done_slot0 == 0 ||
204 table->bst_copy_done_slot0 == state_slot0.copy_done)) {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500205 source = table->bst_status_source;
206 BOOT_LOG_INF("Boot source: %s",
207 source == BOOT_STATUS_SOURCE_NONE ? "none" :
208 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
209 source == BOOT_STATUS_SOURCE_SLOT0 ? "slot 0" :
210 "BUG; can't happen");
211 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800212 }
213 }
214
Marti Bolivarfd20c762017-02-07 16:52:50 -0500215 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800216 return BOOT_STATUS_SOURCE_NONE;
217}
218
219/**
220 * Calculates the type of swap that just completed.
221 */
222static int
223boot_previous_swap_type(void)
224{
225 int post_swap_type;
226 int i;
227
228 post_swap_type = boot_swap_type();
229
230 for (i = 0; i < BOOT_SWAP_TRANS_TABLE_SIZE; i++){
231 if (boot_swap_trans_table[i][1] == post_swap_type) {
232 return boot_swap_trans_table[i][0];
233 }
234 }
235
236 /* XXX: Temporary assert. */
237 assert(0);
238
239 return BOOT_SWAP_TYPE_REVERT;
240}
241
242static int
243boot_read_image_header(int slot, struct image_header *out_hdr)
244{
245 const struct flash_area *fap;
246 int area_id;
247 int rc;
248
249 area_id = flash_area_id_from_image_slot(slot);
250 rc = flash_area_open(area_id, &fap);
251 if (rc != 0) {
252 rc = BOOT_EFLASH;
253 goto done;
254 }
255
256 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
257 if (rc != 0) {
258 rc = BOOT_EFLASH;
259 goto done;
260 }
261
262 rc = 0;
263
264done:
265 flash_area_close(fap);
266 return rc;
267}
268
269static int
270boot_read_image_headers(void)
271{
272 int rc;
273 int i;
274
275 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
276 rc = boot_read_image_header(i, &boot_data.imgs[i].hdr);
277 if (rc != 0) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800278 /* If at least one header was read successfully, then the boot
279 * loader can attempt a boot. Failure to read any headers is a
280 * fatal error.
281 */
282 if (i > 0) {
283 return 0;
284 } else {
285 return rc;
286 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800287 }
288 }
289
290 return 0;
291}
292
293static uint8_t
294boot_write_sz(void)
295{
296 uint8_t elem_sz;
297 uint8_t align;
298
299 /* Figure out what size to write update status update as. The size depends
300 * on what the minimum write size is for scratch area, active image slot.
301 * We need to use the bigger of those 2 values.
302 */
303 elem_sz = hal_flash_align(boot_data.imgs[0].sectors[0].fa_device_id);
304 align = hal_flash_align(boot_data.scratch_sector.fa_device_id);
305 if (align > elem_sz) {
306 elem_sz = align;
307 }
308
309 return elem_sz;
310}
311
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800312static int
313boot_slots_compatible(void)
314{
315 const struct flash_area *sector0;
316 const struct flash_area *sector1;
317 int i;
318
319 /* Ensure both image slots have identical sector layouts. */
320 if (boot_data.imgs[0].num_sectors != boot_data.imgs[1].num_sectors) {
321 return 0;
322 }
323 for (i = 0; i < boot_data.imgs[0].num_sectors; i++) {
324 sector0 = boot_data.imgs[0].sectors + i;
325 sector1 = boot_data.imgs[1].sectors + i;
326 if (sector0->fa_size != sector1->fa_size) {
327 return 0;
328 }
329 }
330
331 return 1;
332}
333
Christopher Collins92ea77f2016-12-12 15:59:26 -0800334/**
335 * Determines the sector layout of both image slots and the scratch area.
336 * This information is necessary for calculating the number of bytes to erase
337 * and copy during an image swap. The information collected during this
338 * function is used to populate the boot_data global.
339 */
340static int
341boot_read_sectors(void)
342{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800343 const struct flash_area *scratch;
344 int num_sectors_slot0;
345 int num_sectors_slot1;
346 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800347
348 num_sectors_slot0 = BOOT_MAX_IMG_SECTORS;
349 rc = flash_area_to_sectors(FLASH_AREA_IMAGE_0, &num_sectors_slot0,
350 boot_data.imgs[0].sectors);
351 if (rc != 0) {
352 return BOOT_EFLASH;
353 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800354 boot_data.imgs[0].num_sectors = num_sectors_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800355
356 num_sectors_slot1 = BOOT_MAX_IMG_SECTORS;
357 rc = flash_area_to_sectors(FLASH_AREA_IMAGE_1, &num_sectors_slot1,
358 boot_data.imgs[1].sectors);
359 if (rc != 0) {
360 return BOOT_EFLASH;
361 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800362 boot_data.imgs[1].num_sectors = num_sectors_slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800363
364 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &scratch);
365 if (rc != 0) {
366 return BOOT_EFLASH;
367 }
368 boot_data.scratch_sector = *scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800369
370 boot_data.write_sz = boot_write_sz();
371
372 return 0;
373}
374
375static uint32_t
376boot_status_internal_off(int idx, int state, int elem_sz)
377{
378 int idx_sz;
379
380 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
381
382 return idx * idx_sz + state * elem_sz;
383}
384
385/**
386 * Reads the status of a partially-completed swap, if any. This is necessary
387 * to recover in case the boot lodaer was reset in the middle of a swap
388 * operation.
389 */
390static int
391boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
392{
393 uint32_t off;
394 uint8_t status;
395 int found;
396 int rc;
397 int i;
398
399 off = boot_status_off(fap);
400
401 found = 0;
402 for (i = 0; i < BOOT_STATUS_MAX_ENTRIES; i++) {
403 rc = flash_area_read(fap, off + i * boot_data.write_sz, &status, 1);
404 if (rc != 0) {
405 return BOOT_EFLASH;
406 }
407
408 if (status == 0xff) {
409 if (found) {
410 break;
411 }
412 } else if (!found) {
413 found = 1;
414 }
415 }
416
417 if (found) {
418 i--;
419 bs->idx = i / BOOT_STATUS_STATE_COUNT;
420 bs->state = i % BOOT_STATUS_STATE_COUNT;
421 }
422
423 return 0;
424}
425
426/**
427 * Reads the boot status from the flash. The boot status contains
428 * the current state of an interrupted image copy operation. If the boot
429 * status is not present, or it indicates that previous copy finished,
430 * there is no operation in progress.
431 */
432static int
433boot_read_status(struct boot_status *bs)
434{
435 const struct flash_area *fap;
436 int status_loc;
437 int area_id;
438 int rc;
439
440 memset(bs, 0, sizeof *bs);
441
442 status_loc = boot_status_source();
443 switch (status_loc) {
444 case BOOT_STATUS_SOURCE_NONE:
445 return 0;
446
447 case BOOT_STATUS_SOURCE_SCRATCH:
448 area_id = FLASH_AREA_IMAGE_SCRATCH;
449 break;
450
451 case BOOT_STATUS_SOURCE_SLOT0:
452 area_id = FLASH_AREA_IMAGE_0;
453 break;
454
455 default:
456 assert(0);
457 return BOOT_EBADARGS;
458 }
459
460 rc = flash_area_open(area_id, &fap);
461 if (rc != 0) {
462 return BOOT_EFLASH;
463 }
464
465 rc = boot_read_status_bytes(fap, bs);
466 if (rc != 0) {
467 return rc;
468 }
469
470 return 0;
471}
472
473/**
474 * Writes the supplied boot status to the flash file system. The boot status
475 * contains the current state of an in-progress image copy operation.
476 *
477 * @param bs The boot status to write.
478 *
479 * @return 0 on success; nonzero on failure.
480 */
481int
482boot_write_status(struct boot_status *bs)
483{
484 const struct flash_area *fap;
485 uint32_t off;
486 int area_id;
487 int rc;
David Brown9d725462017-01-23 15:50:58 -0700488 uint8_t buf[8];
489 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800490
491 if (bs->idx == 0) {
492 /* Write to scratch. */
493 area_id = FLASH_AREA_IMAGE_SCRATCH;
494 } else {
495 /* Write to slot 0. */
496 area_id = FLASH_AREA_IMAGE_0;
497 }
498
499 rc = flash_area_open(area_id, &fap);
500 if (rc != 0) {
501 rc = BOOT_EFLASH;
502 goto done;
503 }
504
505 off = boot_status_off(fap) +
506 boot_status_internal_off(bs->idx, bs->state, boot_data.write_sz);
507
David Brown9d725462017-01-23 15:50:58 -0700508 align = hal_flash_align(fap->fa_device_id);
David Brown9d725462017-01-23 15:50:58 -0700509 memset(buf, 0xFF, 8);
510 buf[0] = bs->state;
511
512 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800513 if (rc != 0) {
514 rc = BOOT_EFLASH;
515 goto done;
516 }
517
518 rc = 0;
519
520done:
521 flash_area_close(fap);
522 return rc;
523}
524
525/*
526 * Validate image hash/signature in a slot.
527 */
528static int
529boot_image_check(struct image_header *hdr, const struct flash_area *fap)
530{
David Browndb1d9d32017-01-06 11:07:54 -0700531 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800532
Christopher Collins92ea77f2016-12-12 15:59:26 -0800533 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
534 NULL, 0, NULL)) {
535 return BOOT_EBADIMAGE;
536 }
537 return 0;
538}
539
540static int
541split_image_check(struct image_header *app_hdr,
542 const struct flash_area *app_fap,
543 struct image_header *loader_hdr,
544 const struct flash_area *loader_fap)
545{
546 static void *tmpbuf;
547 uint8_t loader_hash[32];
548
549 if (!tmpbuf) {
550 tmpbuf = malloc(BOOT_TMPBUF_SZ);
551 if (!tmpbuf) {
552 return BOOT_ENOMEM;
553 }
554 }
555
556 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
557 NULL, 0, loader_hash)) {
558 return BOOT_EBADIMAGE;
559 }
560
561 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
562 loader_hash, 32, NULL)) {
563 return BOOT_EBADIMAGE;
564 }
565
566 return 0;
567}
568
569static int
David Brownd930ec62016-12-14 07:59:48 -0700570boot_validate_slot(int slot)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800571{
572 const struct flash_area *fap;
573 int rc;
574
David Brownd930ec62016-12-14 07:59:48 -0700575 if (boot_data.imgs[slot].hdr.ih_magic == 0xffffffff ||
576 boot_data.imgs[slot].hdr.ih_flags & IMAGE_F_NON_BOOTABLE) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800577
578 /* No bootable image in slot 1; continue booting from slot 0. */
579 return -1;
580 }
581
David Brownd930ec62016-12-14 07:59:48 -0700582 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800583 if (rc != 0) {
584 return BOOT_EFLASH;
585 }
586
David Brownd930ec62016-12-14 07:59:48 -0700587 if ((boot_data.imgs[slot].hdr.ih_magic != IMAGE_MAGIC ||
David Brownb38e0442017-02-24 13:57:12 -0700588 boot_image_check(&boot_data.imgs[slot].hdr, fap) != 0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800589
David Brownb38e0442017-02-24 13:57:12 -0700590 if (slot != 0) {
591 flash_area_erase(fap, 0, fap->fa_size);
592 /* Image in slot 1 is invalid. Erase the image and
593 * continue booting from slot 0.
594 */
595 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800596 return -1;
597 }
598
599 flash_area_close(fap);
600
601 /* Image in slot 1 is valid. */
602 return 0;
603}
604
605/**
606 * Determines which swap operation to perform, if any. If it is determined
607 * that a swap operation is required, the image in the second slot is checked
608 * for validity. If the image in the second slot is invalid, it is erased, and
609 * a swap type of "none" is indicated.
610 *
611 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
612 */
613static int
614boot_validated_swap_type(void)
615{
616 int swap_type;
617 int rc;
618
619 swap_type = boot_swap_type();
620 if (swap_type == BOOT_SWAP_TYPE_NONE) {
621 /* Continue using slot 0. */
622 return BOOT_SWAP_TYPE_NONE;
623 }
624
625 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
David Brownd930ec62016-12-14 07:59:48 -0700626 rc = boot_validate_slot(1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800627 if (rc != 0) {
628 return BOOT_SWAP_TYPE_FAIL;
629 }
630
631 return swap_type;
632}
633
634/**
635 * Calculates the number of sectors the scratch area can contain. A "last"
636 * source sector is specified because images are copied backwards in flash
637 * (final index to index number 0).
638 *
639 * @param last_sector_idx The index of the last source sector
640 * (inclusive).
641 * @param out_first_sector_idx The index of the first source sector
642 * (inclusive) gets written here.
643 *
644 * @return The number of bytes comprised by the
645 * [first-sector, last-sector] range.
646 */
David Brown17609d82017-05-05 09:41:34 -0600647#ifndef BOOTUTIL_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800648static uint32_t
649boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
650{
651 uint32_t new_sz;
652 uint32_t sz;
653 int i;
654
655 sz = 0;
656
657 for (i = last_sector_idx; i >= 0; i--) {
658 new_sz = sz + boot_data.imgs[0].sectors[i].fa_size;
659 if (new_sz > boot_data.scratch_sector.fa_size) {
660 break;
661 }
662 sz = new_sz;
663 }
664
665 /* i currently refers to a sector that doesn't fit or it is -1 because all
666 * sectors have been processed. In both cases, exclude sector i.
667 */
668 *out_first_sector_idx = i + 1;
669 return sz;
670}
David Brown17609d82017-05-05 09:41:34 -0600671#endif /* not BOOTUTIL_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800672
673/**
674 * Erases a region of flash.
675 *
676 * @param flash_area_idx The ID of the flash area containing the region
677 * to erase.
678 * @param off The offset within the flash area to start the
679 * erase.
680 * @param sz The number of bytes to erase.
681 *
682 * @return 0 on success; nonzero on failure.
683 */
684static int
685boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
686{
687 const struct flash_area *fap;
688 int rc;
689
690 rc = flash_area_open(flash_area_id, &fap);
691 if (rc != 0) {
692 rc = BOOT_EFLASH;
693 goto done;
694 }
695
696 rc = flash_area_erase(fap, off, sz);
697 if (rc != 0) {
698 rc = BOOT_EFLASH;
699 goto done;
700 }
701
702 rc = 0;
703
704done:
705 flash_area_close(fap);
706 return rc;
707}
708
709/**
710 * Copies the contents of one flash region to another. You must erase the
711 * destination region prior to calling this function.
712 *
713 * @param flash_area_id_src The ID of the source flash area.
714 * @param flash_area_id_dst The ID of the destination flash area.
715 * @param off_src The offset within the source flash area to
716 * copy from.
717 * @param off_dst The offset within the destination flash area to
718 * copy to.
719 * @param sz The number of bytes to copy.
720 *
721 * @return 0 on success; nonzero on failure.
722 */
723static int
724boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
725 uint32_t off_src, uint32_t off_dst, uint32_t sz)
726{
727 const struct flash_area *fap_src;
728 const struct flash_area *fap_dst;
729 uint32_t bytes_copied;
730 int chunk_sz;
731 int rc;
732
733 static uint8_t buf[1024];
734
735 fap_src = NULL;
736 fap_dst = NULL;
737
738 rc = flash_area_open(flash_area_id_src, &fap_src);
739 if (rc != 0) {
740 rc = BOOT_EFLASH;
741 goto done;
742 }
743
744 rc = flash_area_open(flash_area_id_dst, &fap_dst);
745 if (rc != 0) {
746 rc = BOOT_EFLASH;
747 goto done;
748 }
749
750 bytes_copied = 0;
751 while (bytes_copied < sz) {
752 if (sz - bytes_copied > sizeof buf) {
753 chunk_sz = sizeof buf;
754 } else {
755 chunk_sz = sz - bytes_copied;
756 }
757
758 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
759 if (rc != 0) {
760 rc = BOOT_EFLASH;
761 goto done;
762 }
763
764 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
765 if (rc != 0) {
766 rc = BOOT_EFLASH;
767 goto done;
768 }
769
770 bytes_copied += chunk_sz;
771 }
772
773 rc = 0;
774
775done:
776 flash_area_close(fap_src);
777 flash_area_close(fap_dst);
778 return rc;
779}
780
781/**
782 * Swaps the contents of two flash regions within the two image slots.
783 *
784 * @param idx The index of the first sector in the range of
785 * sectors being swapped.
786 * @param sz The number of bytes to swap.
787 * @param bs The current boot status. This struct gets
788 * updated according to the outcome.
789 *
790 * @return 0 on success; nonzero on failure.
791 */
David Brown17609d82017-05-05 09:41:34 -0600792#ifndef BOOTUTIL_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800793static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800794boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
795{
796 uint32_t copy_sz;
797 uint32_t img_off;
798 int rc;
799
800 /* Calculate offset from start of image area. */
801 img_off = boot_data.imgs[0].sectors[idx].fa_off -
802 boot_data.imgs[0].sectors[0].fa_off;
803
804 if (bs->state == 0) {
805 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800806 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800807
808 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
809 img_off, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800810 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800811
812 bs->state = 1;
Christopher Collins4772ac42017-02-27 20:08:01 -0800813 rc = boot_write_status(bs);
814 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800815 }
816 if (bs->state == 1) {
817 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800818 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800819
820 copy_sz = sz;
821 if (boot_data.imgs[0].sectors[idx].fa_off + sz >=
822 boot_data.imgs[1].sectors[0].fa_off) {
823
824 /* This is the end of the area. Don't copy the image state into
825 * slot 1.
826 */
827 copy_sz -= boot_trailer_sz(boot_data.write_sz);
828 }
829
830 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
831 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800832 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800833
834 bs->state = 2;
Christopher Collins4772ac42017-02-27 20:08:01 -0800835 rc = boot_write_status(bs);
836 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800837 }
838 if (bs->state == 2) {
839 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800840 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800841
842 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
843 0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800844 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800845
846 bs->idx++;
847 bs->state = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -0800848 rc = boot_write_status(bs);
849 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800850 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800851}
David Brown17609d82017-05-05 09:41:34 -0600852#endif /* not BOOTUTIL_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800853
854/**
855 * Swaps the two images in flash. If a prior copy operation was interrupted
856 * by a system reset, this function completes that operation.
857 *
858 * @param bs The current boot status. This function reads
859 * this struct to determine if it is resuming
860 * an interrupted swap operation. This
861 * function writes the updated status to this
862 * function on return.
863 *
864 * @return 0 on success; nonzero on failure.
865 */
David Brown17609d82017-05-05 09:41:34 -0600866#ifdef BOOTUTIL_OVERWRITE_ONLY
867static int
868boot_copy_image(struct boot_status *bs)
869{
870 int sect_count;
871 int sect;
872 int rc;
873 uint32_t size = 0;
874 uint32_t this_size;
875
876 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
877 BOOT_LOG_INF("Erasing slot0");
878
879 sect_count = boot_data.imgs[0].num_sectors;
880 for (sect = 0; sect < sect_count; sect++) {
881 this_size = boot_data.imgs[0].sectors[sect].fa_size;
882 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
883 size,
884 this_size);
885 assert(rc == 0);
886
887 size += this_size;
888 }
889
890 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%x bytes",
891 size);
892 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
893 0, 0, size);
894
895 /* Erase slot 1 so that we don't do the upgrade on every boot.
896 * TODO: Perhaps verify slot 0's signature again? */
897 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
898 0, boot_data.imgs[1].sectors[0].fa_size);
899 assert(rc == 0);
900
901 return 0;
902}
903#else
Christopher Collins92ea77f2016-12-12 15:59:26 -0800904static int
905boot_copy_image(struct boot_status *bs)
906{
907 uint32_t sz;
908 int first_sector_idx;
909 int last_sector_idx;
910 int swap_idx;
911
912 swap_idx = 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800913 last_sector_idx = boot_data.imgs[0].num_sectors - 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800914 while (last_sector_idx >= 0) {
915 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
916 if (swap_idx >= bs->idx) {
917 boot_swap_sectors(first_sector_idx, sz, bs);
918 }
919
920 last_sector_idx = first_sector_idx - 1;
921 swap_idx++;
922 }
923
924 return 0;
925}
David Brown17609d82017-05-05 09:41:34 -0600926#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800927
928/**
929 * Marks a test image in slot 0 as fully copied.
930 */
931static int
932boot_finalize_test_swap(void)
933{
934 const struct flash_area *fap;
935 int rc;
936
937 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
938 if (rc != 0) {
939 return BOOT_EFLASH;
940 }
941
942 rc = boot_write_copy_done(fap);
943 if (rc != 0) {
944 return rc;
945 }
946
947 return 0;
948}
949
950/**
951 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
952 * the status bytes from the image revert operation don't get processed on a
953 * subsequent boot.
954 */
955static int
956boot_finalize_revert_swap(void)
957{
958 const struct flash_area *fap;
959 struct boot_swap_state state_slot0;
960 int rc;
961
962 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
963 if (rc != 0) {
964 return BOOT_EFLASH;
965 }
966
967 rc = boot_read_swap_state(fap, &state_slot0);
968 if (rc != 0) {
969 return BOOT_EFLASH;
970 }
971
972 if (state_slot0.magic == BOOT_MAGIC_UNSET) {
973 rc = boot_write_magic(fap);
974 if (rc != 0) {
975 return rc;
976 }
977 }
978
979 if (state_slot0.copy_done == 0xff) {
980 rc = boot_write_copy_done(fap);
981 if (rc != 0) {
982 return rc;
983 }
984 }
985
986 if (state_slot0.image_ok == 0xff) {
987 rc = boot_write_image_ok(fap);
988 if (rc != 0) {
989 return rc;
990 }
991 }
992
993 return 0;
994}
995
996/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800997 * Performs an image swap if one is required.
998 *
999 * @param out_swap_type On success, the type of swap performed gets
1000 * written here.
1001 *
1002 * @return 0 on success; nonzero on failure.
1003 */
1004static int
1005boot_swap_if_needed(int *out_swap_type)
1006{
1007 struct boot_status bs;
1008 int swap_type;
1009 int rc;
1010
1011 /* Determine if we rebooted in the middle of an image swap
1012 * operation.
1013 */
1014 rc = boot_read_status(&bs);
1015 if (rc != 0) {
1016 return rc;
1017 }
1018
1019 /* If a partial swap was detected, complete it. */
1020 if (bs.idx != 0 || bs.state != 0) {
1021 rc = boot_copy_image(&bs);
1022 assert(rc == 0);
1023
1024 /* Extrapolate the type of the partial swap. We need this
1025 * information to know how to mark the swap complete in flash.
1026 */
1027 swap_type = boot_previous_swap_type();
1028 } else {
1029 swap_type = boot_validated_swap_type();
1030 switch (swap_type) {
1031 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001032 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001033 case BOOT_SWAP_TYPE_REVERT:
1034 rc = boot_copy_image(&bs);
1035 assert(rc == 0);
1036 break;
1037 }
1038 }
1039
1040 *out_swap_type = swap_type;
1041 return 0;
1042}
1043
1044/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001045 * Prepares the booting process. This function moves images around in flash as
1046 * appropriate, and tells you what address to boot from.
1047 *
1048 * @param rsp On success, indicates how booting should occur.
1049 *
1050 * @return 0 on success; nonzero on failure.
1051 */
1052int
1053boot_go(struct boot_rsp *rsp)
1054{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001055 int swap_type;
1056 int slot;
1057 int rc;
1058
1059 /* The array of slot sectors are defined here (as opposed to file scope) so
1060 * that they don't get allocated for non-boot-loader apps. This is
1061 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001062 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001063 */
1064 static struct flash_area slot0_sectors[BOOT_MAX_IMG_SECTORS];
1065 static struct flash_area slot1_sectors[BOOT_MAX_IMG_SECTORS];
1066 boot_data.imgs[0].sectors = slot0_sectors;
1067 boot_data.imgs[1].sectors = slot1_sectors;
1068
1069 /* Determine the sector layout of the image slots and scratch area. */
1070 rc = boot_read_sectors();
1071 if (rc != 0) {
1072 return rc;
1073 }
1074
1075 /* Attempt to read an image header from each slot. */
1076 rc = boot_read_image_headers();
1077 if (rc != 0) {
1078 return rc;
1079 }
1080
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001081 /* If the image slots aren't compatible, no swap is possible. Just boot
1082 * into slot 0.
1083 */
1084 if (boot_slots_compatible()) {
1085 rc = boot_swap_if_needed(&swap_type);
1086 if (rc != 0) {
1087 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001088 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001089 } else {
1090 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001091 }
1092
1093 switch (swap_type) {
1094 case BOOT_SWAP_TYPE_NONE:
David Brownd930ec62016-12-14 07:59:48 -07001095#ifdef BOOTUTIL_VALIDATE_SLOT0
1096 rc = boot_validate_slot(0);
1097 if (rc != 0) {
1098 return BOOT_EBADIMAGE;
1099 }
1100#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001101 slot = 0;
1102 break;
1103
1104 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001105 case BOOT_SWAP_TYPE_PERM:
Christopher Collins92ea77f2016-12-12 15:59:26 -08001106 slot = 1;
1107 boot_finalize_test_swap();
1108 break;
1109
1110 case BOOT_SWAP_TYPE_REVERT:
1111 slot = 1;
1112 boot_finalize_revert_swap();
1113 break;
1114
1115 case BOOT_SWAP_TYPE_FAIL:
1116 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1117 * try to boot into it again on the next reboot. Do this by pretending
1118 * we just reverted back to slot 0.
1119 */
1120 slot = 0;
1121 boot_finalize_revert_swap();
1122 break;
1123
1124 default:
1125 assert(0);
1126 slot = 0;
1127 break;
1128 }
1129
1130 /* Always boot from the primary slot. */
1131 rsp->br_flash_id = boot_data.imgs[0].sectors[0].fa_device_id;
1132 rsp->br_image_addr = boot_data.imgs[0].sectors[0].fa_off;
1133 rsp->br_hdr = &boot_data.imgs[slot].hdr;
1134
1135 return 0;
1136}
1137
1138int
1139split_go(int loader_slot, int split_slot, void **entry)
1140{
1141 const struct flash_area *loader_fap;
1142 const struct flash_area *app_fap;
1143 struct flash_area *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001144 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001145 int loader_flash_id;
1146 int app_flash_id;
1147 int rc;
1148
1149 app_fap = NULL;
1150 loader_fap = NULL;
1151
1152 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1153 if (sectors == NULL) {
1154 rc = SPLIT_GO_ERR;
1155 goto done;
1156 }
1157 boot_data.imgs[0].sectors = sectors + 0;
1158 boot_data.imgs[1].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1159
1160 /* Determine the sector layout of the image slots and scratch area. */
1161 rc = boot_read_sectors();
1162 if (rc != 0) {
1163 rc = SPLIT_GO_ERR;
1164 goto done;
1165 }
1166
1167 rc = boot_read_image_headers();
1168 if (rc != 0) {
1169 goto done;
1170 }
1171
1172 app_flash_id = flash_area_id_from_image_slot(split_slot);
1173 rc = flash_area_open(app_flash_id, &app_fap);
1174 if (rc != 0) {
1175 rc = BOOT_EFLASH;
1176 goto done;
1177 }
1178
1179 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1180 rc = flash_area_open(loader_flash_id, &loader_fap);
1181 if (rc != 0) {
1182 rc = BOOT_EFLASH;
1183 goto done;
1184 }
1185
1186 /* Don't check the bootable image flag because we could really call a
1187 * bootable or non-bootable image. Just validate that the image check
1188 * passes which is distinct from the normal check.
1189 */
1190 rc = split_image_check(&boot_data.imgs[split_slot].hdr,
1191 app_fap,
1192 &boot_data.imgs[loader_slot].hdr,
1193 loader_fap);
1194 if (rc != 0) {
1195 rc = SPLIT_GO_NON_MATCHING;
1196 goto done;
1197 }
1198
1199 entry_val = boot_data.imgs[split_slot].sectors[0].fa_off +
1200 boot_data.imgs[split_slot].hdr.ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001201 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001202 rc = SPLIT_GO_OK;
1203
1204done:
1205 free(sectors);
1206 flash_area_close(app_fap);
1207 flash_area_close(loader_fap);
1208 return rc;
1209}