blob: d56cade7ebcf805a8d585373f3fb336baef1962d [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/**
21 * This file provides an interface to the boot loader. Functions defined in
22 * this file should only be called while the boot loader is running.
23 */
24
25#include <assert.h>
26#include <stddef.h>
27#include <inttypes.h>
28#include <stdlib.h>
29#include <string.h>
30#include "sysflash/sysflash.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080031#include <hal/hal_flash.h>
32#include <os/os_malloc.h>
33#include "bootutil/bootutil.h"
34#include "bootutil/image.h"
35#include "bootutil_priv.h"
36
Marti Bolivarfd20c762017-02-07 16:52:50 -050037#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
38#include "bootutil/bootutil_log.h"
39
Fabio Utzigeed80b62017-06-10 08:03:05 -030040#ifdef APP_mynewt
41#include "mynewt/config.h"
42#endif
43
Christopher Collins92ea77f2016-12-12 15:59:26 -080044#define BOOT_MAX_IMG_SECTORS 120
45
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040046static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080047
48struct boot_status_table {
49 /**
50 * For each field, a value of 0 means "any".
51 */
52 uint8_t bst_magic_slot0;
53 uint8_t bst_magic_scratch;
54 uint8_t bst_copy_done_slot0;
55 uint8_t bst_status_source;
56};
57
58/**
59 * This set of tables maps swap state contents to boot status location.
60 * When searching for a match, these tables must be iterated in order.
61 */
62static const struct boot_status_table boot_status_tables[] = {
63 {
64 /* | slot-0 | scratch |
65 * ----------+------------+------------|
66 * magic | Good | Any |
67 * copy-done | 0x01 | N/A |
68 * ----------+------------+------------'
69 * source: none |
70 * ------------------------------------'
71 */
72 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
73 .bst_magic_scratch = 0,
74 .bst_copy_done_slot0 = 0x01,
75 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
76 },
77
78 {
79 /* | slot-0 | scratch |
80 * ----------+------------+------------|
81 * magic | Good | Any |
82 * copy-done | 0xff | N/A |
83 * ----------+------------+------------'
84 * source: slot 0 |
85 * ------------------------------------'
86 */
87 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
88 .bst_magic_scratch = 0,
89 .bst_copy_done_slot0 = 0xff,
90 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
91 },
92
93 {
94 /* | slot-0 | scratch |
95 * ----------+------------+------------|
96 * magic | Any | Good |
97 * copy-done | Any | N/A |
98 * ----------+------------+------------'
99 * source: scratch |
100 * ------------------------------------'
101 */
102 .bst_magic_slot0 = 0,
103 .bst_magic_scratch = BOOT_MAGIC_GOOD,
104 .bst_copy_done_slot0 = 0,
105 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
106 },
107
108 {
109 /* | slot-0 | scratch |
110 * ----------+------------+------------|
111 * magic | Unset | Any |
112 * copy-done | 0xff | N/A |
113 * ----------+------------+------------|
114 * source: varies |
115 * ------------------------------------+------------------------------+
116 * This represents one of two cases: |
117 * o No swaps ever (no status to read, so no harm in checking). |
118 * o Mid-revert; status in slot 0. |
119 * -------------------------------------------------------------------'
120 */
121 .bst_magic_slot0 = BOOT_MAGIC_UNSET,
122 .bst_magic_scratch = 0,
123 .bst_copy_done_slot0 = 0xff,
124 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
125 },
126};
127
128#define BOOT_STATUS_TABLES_COUNT \
129 (sizeof boot_status_tables / sizeof boot_status_tables[0])
130
131/**
132 * This table indicates the next swap type that should be performed. The first
133 * column contains the current swap type. The second column contains the swap
134 * type that should be effected after the first completes.
135 */
136static const uint8_t boot_swap_trans_table[][2] = {
137 /* From To */
138 { BOOT_SWAP_TYPE_REVERT, BOOT_SWAP_TYPE_NONE },
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800139 { BOOT_SWAP_TYPE_PERM, BOOT_SWAP_TYPE_NONE },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800140 { BOOT_SWAP_TYPE_TEST, BOOT_SWAP_TYPE_REVERT },
141};
142
143#define BOOT_SWAP_TRANS_TABLE_SIZE \
144 (sizeof boot_swap_trans_table / sizeof boot_swap_trans_table[0])
145
Marti Bolivarfd20c762017-02-07 16:52:50 -0500146#define BOOT_LOG_SWAP_STATE(area, state) \
147 BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \
148 (area), \
149 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
150 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
151 "bad"), \
152 (state)->copy_done, \
153 (state)->image_ok)
154
Christopher Collins92ea77f2016-12-12 15:59:26 -0800155/**
156 * Determines where in flash the most recent boot status is stored. The boot
157 * status is necessary for completing a swap that was interrupted by a boot
158 * loader reset.
159 *
160 * @return A BOOT_STATUS_SOURCE_[...] code indicating where * status should be read from.
161 */
162static int
163boot_status_source(void)
164{
165 const struct boot_status_table *table;
166 struct boot_swap_state state_scratch;
167 struct boot_swap_state state_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800168 int rc;
169 int i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500170 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800171
Fabio Utzig2473ac02017-05-02 12:45:02 -0300172 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800173 assert(rc == 0);
174
Fabio Utzig2473ac02017-05-02 12:45:02 -0300175 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800176 assert(rc == 0);
177
Marti Bolivarfd20c762017-02-07 16:52:50 -0500178 BOOT_LOG_SWAP_STATE("Image 0", &state_slot0);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500179 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
180
Christopher Collins92ea77f2016-12-12 15:59:26 -0800181 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300182 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800183
184 if ((table->bst_magic_slot0 == 0 ||
185 table->bst_magic_slot0 == state_slot0.magic) &&
186 (table->bst_magic_scratch == 0 ||
187 table->bst_magic_scratch == state_scratch.magic) &&
188 (table->bst_copy_done_slot0 == 0 ||
189 table->bst_copy_done_slot0 == state_slot0.copy_done)) {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500190 source = table->bst_status_source;
191 BOOT_LOG_INF("Boot source: %s",
192 source == BOOT_STATUS_SOURCE_NONE ? "none" :
193 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
194 source == BOOT_STATUS_SOURCE_SLOT0 ? "slot 0" :
195 "BUG; can't happen");
196 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800197 }
198 }
199
Marti Bolivarfd20c762017-02-07 16:52:50 -0500200 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800201 return BOOT_STATUS_SOURCE_NONE;
202}
203
204/**
205 * Calculates the type of swap that just completed.
206 */
207static int
208boot_previous_swap_type(void)
209{
210 int post_swap_type;
211 int i;
212
213 post_swap_type = boot_swap_type();
214
215 for (i = 0; i < BOOT_SWAP_TRANS_TABLE_SIZE; i++){
216 if (boot_swap_trans_table[i][1] == post_swap_type) {
217 return boot_swap_trans_table[i][0];
218 }
219 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300220
Christopher Collins92ea77f2016-12-12 15:59:26 -0800221 /* XXX: Temporary assert. */
222 assert(0);
223
224 return BOOT_SWAP_TYPE_REVERT;
225}
226
227static int
228boot_read_image_header(int slot, struct image_header *out_hdr)
229{
230 const struct flash_area *fap;
231 int area_id;
232 int rc;
233
234 area_id = flash_area_id_from_image_slot(slot);
235 rc = flash_area_open(area_id, &fap);
236 if (rc != 0) {
237 rc = BOOT_EFLASH;
238 goto done;
239 }
240
241 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
242 if (rc != 0) {
243 rc = BOOT_EFLASH;
244 goto done;
245 }
246
247 rc = 0;
248
249done:
250 flash_area_close(fap);
251 return rc;
252}
253
254static int
255boot_read_image_headers(void)
256{
257 int rc;
258 int i;
259
260 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
261 rc = boot_read_image_header(i, &boot_data.imgs[i].hdr);
262 if (rc != 0) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300263 /* If at least the first slot's header was read successfully, then
264 * the boot loader can attempt a boot. Failure to read any headers
265 * is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800266 */
267 if (i > 0) {
268 return 0;
269 } else {
270 return rc;
271 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800272 }
273 }
274
275 return 0;
276}
277
278static uint8_t
279boot_write_sz(void)
280{
281 uint8_t elem_sz;
282 uint8_t align;
283
284 /* Figure out what size to write update status update as. The size depends
285 * on what the minimum write size is for scratch area, active image slot.
286 * We need to use the bigger of those 2 values.
287 */
288 elem_sz = hal_flash_align(boot_data.imgs[0].sectors[0].fa_device_id);
289 align = hal_flash_align(boot_data.scratch_sector.fa_device_id);
290 if (align > elem_sz) {
291 elem_sz = align;
292 }
293
294 return elem_sz;
295}
296
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800297static int
298boot_slots_compatible(void)
299{
300 const struct flash_area *sector0;
301 const struct flash_area *sector1;
302 int i;
303
304 /* Ensure both image slots have identical sector layouts. */
305 if (boot_data.imgs[0].num_sectors != boot_data.imgs[1].num_sectors) {
306 return 0;
307 }
308 for (i = 0; i < boot_data.imgs[0].num_sectors; i++) {
309 sector0 = boot_data.imgs[0].sectors + i;
310 sector1 = boot_data.imgs[1].sectors + i;
311 if (sector0->fa_size != sector1->fa_size) {
312 return 0;
313 }
314 }
315
316 return 1;
317}
318
Christopher Collins92ea77f2016-12-12 15:59:26 -0800319/**
320 * Determines the sector layout of both image slots and the scratch area.
321 * This information is necessary for calculating the number of bytes to erase
322 * and copy during an image swap. The information collected during this
323 * function is used to populate the boot_data global.
324 */
325static int
326boot_read_sectors(void)
327{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800328 const struct flash_area *scratch;
329 int num_sectors_slot0;
330 int num_sectors_slot1;
331 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800332
333 num_sectors_slot0 = BOOT_MAX_IMG_SECTORS;
334 rc = flash_area_to_sectors(FLASH_AREA_IMAGE_0, &num_sectors_slot0,
335 boot_data.imgs[0].sectors);
336 if (rc != 0) {
337 return BOOT_EFLASH;
338 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800339 boot_data.imgs[0].num_sectors = num_sectors_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800340
341 num_sectors_slot1 = BOOT_MAX_IMG_SECTORS;
342 rc = flash_area_to_sectors(FLASH_AREA_IMAGE_1, &num_sectors_slot1,
343 boot_data.imgs[1].sectors);
344 if (rc != 0) {
345 return BOOT_EFLASH;
346 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800347 boot_data.imgs[1].num_sectors = num_sectors_slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800348
349 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &scratch);
350 if (rc != 0) {
351 return BOOT_EFLASH;
352 }
353 boot_data.scratch_sector = *scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800354
355 boot_data.write_sz = boot_write_sz();
356
357 return 0;
358}
359
360static uint32_t
361boot_status_internal_off(int idx, int state, int elem_sz)
362{
363 int idx_sz;
364
365 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
366
367 return idx * idx_sz + state * elem_sz;
368}
369
370/**
371 * Reads the status of a partially-completed swap, if any. This is necessary
372 * to recover in case the boot lodaer was reset in the middle of a swap
373 * operation.
374 */
375static int
376boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
377{
378 uint32_t off;
379 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300380 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800381 int found;
382 int rc;
383 int i;
384
385 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400386 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300387
Christopher Collins92ea77f2016-12-12 15:59:26 -0800388 found = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300389 for (i = 0; i < max_entries; i++) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800390 rc = flash_area_read(fap, off + i * boot_data.write_sz, &status, 1);
391 if (rc != 0) {
392 return BOOT_EFLASH;
393 }
394
395 if (status == 0xff) {
396 if (found) {
397 break;
398 }
399 } else if (!found) {
400 found = 1;
401 }
402 }
403
404 if (found) {
405 i--;
Fabio Utzig94d998c2017-05-22 11:02:41 -0400406 bs->idx = i / BOOT_STATUS_STATE_COUNT;
407 bs->state = i % BOOT_STATUS_STATE_COUNT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800408 }
409
410 return 0;
411}
412
413/**
414 * Reads the boot status from the flash. The boot status contains
415 * the current state of an interrupted image copy operation. If the boot
416 * status is not present, or it indicates that previous copy finished,
417 * there is no operation in progress.
418 */
419static int
420boot_read_status(struct boot_status *bs)
421{
422 const struct flash_area *fap;
423 int status_loc;
424 int area_id;
425 int rc;
426
427 memset(bs, 0, sizeof *bs);
428
429 status_loc = boot_status_source();
430 switch (status_loc) {
431 case BOOT_STATUS_SOURCE_NONE:
432 return 0;
433
434 case BOOT_STATUS_SOURCE_SCRATCH:
435 area_id = FLASH_AREA_IMAGE_SCRATCH;
436 break;
437
438 case BOOT_STATUS_SOURCE_SLOT0:
439 area_id = FLASH_AREA_IMAGE_0;
440 break;
441
442 default:
443 assert(0);
444 return BOOT_EBADARGS;
445 }
446
447 rc = flash_area_open(area_id, &fap);
448 if (rc != 0) {
449 return BOOT_EFLASH;
450 }
451
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300452 return boot_read_status_bytes(fap, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800453}
454
455/**
456 * Writes the supplied boot status to the flash file system. The boot status
457 * contains the current state of an in-progress image copy operation.
458 *
459 * @param bs The boot status to write.
460 *
461 * @return 0 on success; nonzero on failure.
462 */
463int
464boot_write_status(struct boot_status *bs)
465{
466 const struct flash_area *fap;
467 uint32_t off;
468 int area_id;
469 int rc;
David Brown9d725462017-01-23 15:50:58 -0700470 uint8_t buf[8];
471 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800472
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300473 /* NOTE: The first sector copied (that is the last sector on slot) contains
474 * the trailer. Since in the last step SLOT 0 is erased, the first
475 * two status writes go to the scratch which will be copied to SLOT 0!
476 */
477
Fabio Utzig2473ac02017-05-02 12:45:02 -0300478 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800479 /* Write to scratch. */
480 area_id = FLASH_AREA_IMAGE_SCRATCH;
481 } else {
482 /* Write to slot 0. */
483 area_id = FLASH_AREA_IMAGE_0;
484 }
485
486 rc = flash_area_open(area_id, &fap);
487 if (rc != 0) {
488 rc = BOOT_EFLASH;
489 goto done;
490 }
491
492 off = boot_status_off(fap) +
493 boot_status_internal_off(bs->idx, bs->state, boot_data.write_sz);
494
David Brown9d725462017-01-23 15:50:58 -0700495 align = hal_flash_align(fap->fa_device_id);
David Brown9d725462017-01-23 15:50:58 -0700496 memset(buf, 0xFF, 8);
497 buf[0] = bs->state;
498
499 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800500 if (rc != 0) {
501 rc = BOOT_EFLASH;
502 goto done;
503 }
504
505 rc = 0;
506
507done:
508 flash_area_close(fap);
509 return rc;
510}
511
512/*
513 * Validate image hash/signature in a slot.
514 */
515static int
516boot_image_check(struct image_header *hdr, const struct flash_area *fap)
517{
David Browndb1d9d32017-01-06 11:07:54 -0700518 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800519
Christopher Collins92ea77f2016-12-12 15:59:26 -0800520 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
521 NULL, 0, NULL)) {
522 return BOOT_EBADIMAGE;
523 }
524 return 0;
525}
526
527static int
528split_image_check(struct image_header *app_hdr,
529 const struct flash_area *app_fap,
530 struct image_header *loader_hdr,
531 const struct flash_area *loader_fap)
532{
533 static void *tmpbuf;
534 uint8_t loader_hash[32];
535
536 if (!tmpbuf) {
537 tmpbuf = malloc(BOOT_TMPBUF_SZ);
538 if (!tmpbuf) {
539 return BOOT_ENOMEM;
540 }
541 }
542
543 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
544 NULL, 0, loader_hash)) {
545 return BOOT_EBADIMAGE;
546 }
547
548 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
549 loader_hash, 32, NULL)) {
550 return BOOT_EBADIMAGE;
551 }
552
553 return 0;
554}
555
556static int
David Brownd930ec62016-12-14 07:59:48 -0700557boot_validate_slot(int slot)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800558{
559 const struct flash_area *fap;
560 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300561
David Brownd930ec62016-12-14 07:59:48 -0700562 if (boot_data.imgs[slot].hdr.ih_magic == 0xffffffff ||
563 boot_data.imgs[slot].hdr.ih_flags & IMAGE_F_NON_BOOTABLE) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800564
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300565 /* No bootable image in slot; continue booting from slot 0. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800566 return -1;
567 }
568
David Brownd930ec62016-12-14 07:59:48 -0700569 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800570 if (rc != 0) {
571 return BOOT_EFLASH;
572 }
573
David Brownd930ec62016-12-14 07:59:48 -0700574 if ((boot_data.imgs[slot].hdr.ih_magic != IMAGE_MAGIC ||
David Brownb38e0442017-02-24 13:57:12 -0700575 boot_image_check(&boot_data.imgs[slot].hdr, fap) != 0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800576
David Brownb38e0442017-02-24 13:57:12 -0700577 if (slot != 0) {
578 flash_area_erase(fap, 0, fap->fa_size);
579 /* Image in slot 1 is invalid. Erase the image and
580 * continue booting from slot 0.
581 */
582 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800583 return -1;
584 }
585
586 flash_area_close(fap);
587
588 /* Image in slot 1 is valid. */
589 return 0;
590}
591
592/**
593 * Determines which swap operation to perform, if any. If it is determined
594 * that a swap operation is required, the image in the second slot is checked
595 * for validity. If the image in the second slot is invalid, it is erased, and
596 * a swap type of "none" is indicated.
597 *
598 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
599 */
600static int
601boot_validated_swap_type(void)
602{
603 int swap_type;
604 int rc;
605
606 swap_type = boot_swap_type();
607 if (swap_type == BOOT_SWAP_TYPE_NONE) {
608 /* Continue using slot 0. */
609 return BOOT_SWAP_TYPE_NONE;
610 }
611
612 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
David Brownd930ec62016-12-14 07:59:48 -0700613 rc = boot_validate_slot(1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800614 if (rc != 0) {
615 return BOOT_SWAP_TYPE_FAIL;
616 }
617
618 return swap_type;
619}
620
621/**
622 * Calculates the number of sectors the scratch area can contain. A "last"
623 * source sector is specified because images are copied backwards in flash
624 * (final index to index number 0).
625 *
626 * @param last_sector_idx The index of the last source sector
627 * (inclusive).
628 * @param out_first_sector_idx The index of the first source sector
629 * (inclusive) gets written here.
630 *
631 * @return The number of bytes comprised by the
632 * [first-sector, last-sector] range.
633 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300634#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800635static uint32_t
636boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
637{
638 uint32_t new_sz;
639 uint32_t sz;
640 int i;
641
642 sz = 0;
643
644 for (i = last_sector_idx; i >= 0; i--) {
645 new_sz = sz + boot_data.imgs[0].sectors[i].fa_size;
646 if (new_sz > boot_data.scratch_sector.fa_size) {
647 break;
648 }
649 sz = new_sz;
650 }
651
652 /* i currently refers to a sector that doesn't fit or it is -1 because all
653 * sectors have been processed. In both cases, exclude sector i.
654 */
655 *out_first_sector_idx = i + 1;
656 return sz;
657}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300658#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800659
660/**
661 * Erases a region of flash.
662 *
663 * @param flash_area_idx The ID of the flash area containing the region
664 * to erase.
665 * @param off The offset within the flash area to start the
666 * erase.
667 * @param sz The number of bytes to erase.
668 *
669 * @return 0 on success; nonzero on failure.
670 */
671static int
672boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
673{
674 const struct flash_area *fap;
675 int rc;
676
677 rc = flash_area_open(flash_area_id, &fap);
678 if (rc != 0) {
679 rc = BOOT_EFLASH;
680 goto done;
681 }
682
683 rc = flash_area_erase(fap, off, sz);
684 if (rc != 0) {
685 rc = BOOT_EFLASH;
686 goto done;
687 }
688
689 rc = 0;
690
691done:
692 flash_area_close(fap);
693 return rc;
694}
695
696/**
697 * Copies the contents of one flash region to another. You must erase the
698 * destination region prior to calling this function.
699 *
700 * @param flash_area_id_src The ID of the source flash area.
701 * @param flash_area_id_dst The ID of the destination flash area.
702 * @param off_src The offset within the source flash area to
703 * copy from.
704 * @param off_dst The offset within the destination flash area to
705 * copy to.
706 * @param sz The number of bytes to copy.
707 *
708 * @return 0 on success; nonzero on failure.
709 */
710static int
711boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
712 uint32_t off_src, uint32_t off_dst, uint32_t sz)
713{
714 const struct flash_area *fap_src;
715 const struct flash_area *fap_dst;
716 uint32_t bytes_copied;
717 int chunk_sz;
718 int rc;
719
720 static uint8_t buf[1024];
721
722 fap_src = NULL;
723 fap_dst = NULL;
724
725 rc = flash_area_open(flash_area_id_src, &fap_src);
726 if (rc != 0) {
727 rc = BOOT_EFLASH;
728 goto done;
729 }
730
731 rc = flash_area_open(flash_area_id_dst, &fap_dst);
732 if (rc != 0) {
733 rc = BOOT_EFLASH;
734 goto done;
735 }
736
737 bytes_copied = 0;
738 while (bytes_copied < sz) {
739 if (sz - bytes_copied > sizeof buf) {
740 chunk_sz = sizeof buf;
741 } else {
742 chunk_sz = sz - bytes_copied;
743 }
744
745 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
746 if (rc != 0) {
747 rc = BOOT_EFLASH;
748 goto done;
749 }
750
751 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
752 if (rc != 0) {
753 rc = BOOT_EFLASH;
754 goto done;
755 }
756
757 bytes_copied += chunk_sz;
758 }
759
760 rc = 0;
761
762done:
763 flash_area_close(fap_src);
764 flash_area_close(fap_dst);
765 return rc;
766}
767
Fabio Utzig2473ac02017-05-02 12:45:02 -0300768static inline int
769boot_status_init_by_id(int flash_area_id)
770{
771 const struct flash_area *fap;
772 struct boot_swap_state swap_state;
773 int rc;
774
775 rc = flash_area_open(flash_area_id, &fap);
776 assert(rc == 0);
777
778 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
779 assert(rc == 0);
780
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400781 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300782 rc = boot_write_image_ok(fap);
783 assert(rc == 0);
784 }
785
786 rc = boot_write_magic(fap);
787 assert(rc == 0);
788
789 flash_area_close(fap);
790
791 return 0;
792}
793
794static int
795boot_erase_last_sector_by_id(int flash_area_id)
796{
797 uint8_t slot;
798 uint32_t last_sector;
799 struct flash_area *sectors;
800 int rc;
801
802 switch (flash_area_id) {
803 case FLASH_AREA_IMAGE_0:
804 slot = 0;
805 break;
806 case FLASH_AREA_IMAGE_1:
807 slot = 1;
808 break;
809 default:
810 return BOOT_EFLASH;
811 }
812
813 last_sector = boot_data.imgs[slot].num_sectors - 1;
814 sectors = boot_data.imgs[slot].sectors;
815 rc = boot_erase_sector(flash_area_id,
816 sectors[last_sector].fa_off - sectors[0].fa_off,
817 sectors[last_sector].fa_size);
818 assert(rc == 0);
819
820 return rc;
821}
822
Christopher Collins92ea77f2016-12-12 15:59:26 -0800823/**
824 * Swaps the contents of two flash regions within the two image slots.
825 *
826 * @param idx The index of the first sector in the range of
827 * sectors being swapped.
828 * @param sz The number of bytes to swap.
829 * @param bs The current boot status. This struct gets
830 * updated according to the outcome.
831 *
832 * @return 0 on success; nonzero on failure.
833 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300834#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800835static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800836boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
837{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300838 const struct flash_area *fap;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800839 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300840 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800841 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300842 uint32_t scratch_trailer_off;
843 struct boot_swap_state swap_state;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800844 int rc;
845
846 /* Calculate offset from start of image area. */
847 img_off = boot_data.imgs[0].sectors[idx].fa_off -
848 boot_data.imgs[0].sectors[0].fa_off;
849
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300850 copy_sz = sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300851 trailer_sz = boot_slots_trailer_sz(boot_data.write_sz);
Fabio Utzig9678c972017-05-23 11:28:56 -0400852
853 /* sz in this function is always is always sized on a multiple of the
854 * sector size. The check against the first address of the last sector
855 * is to determine if we're swapping the last sector. The last sector
856 * needs special handling because it's where the trailer lives. If we're
857 * copying it, we need to use scratch to write the trailer temporarily.
858 *
859 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
860 * controls if special handling is needed (swapping last sector).
861 */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300862 if (boot_data.imgs[0].sectors[idx].fa_off + sz >
863 boot_data.imgs[0].sectors[boot_data.imgs[0].num_sectors - 1].fa_off) {
864 copy_sz -= trailer_sz;
865 }
866
Fabio Utzig2473ac02017-05-02 12:45:02 -0300867 bs->use_scratch = (bs->idx == 0 && copy_sz != sz);
868
Christopher Collins92ea77f2016-12-12 15:59:26 -0800869 if (bs->state == 0) {
870 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800871 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800872
873 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300874 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800875 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800876
Fabio Utzig2473ac02017-05-02 12:45:02 -0300877 if (bs->idx == 0) {
878 if (bs->use_scratch) {
879 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH);
880 } else {
881 /* Prepare the status area... here it is known that the
882 * last sector is not being used by the image data so it's
883 * safe to erase.
884 */
885 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300886 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300887
888 boot_status_init_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300889 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300890 }
891
Christopher Collins92ea77f2016-12-12 15:59:26 -0800892 bs->state = 1;
Christopher Collins4772ac42017-02-27 20:08:01 -0800893 rc = boot_write_status(bs);
894 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800895 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300896
Christopher Collins92ea77f2016-12-12 15:59:26 -0800897 if (bs->state == 1) {
898 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800899 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800900
Christopher Collins92ea77f2016-12-12 15:59:26 -0800901 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
902 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800903 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800904
Fabio Utzig2473ac02017-05-02 12:45:02 -0300905 if (bs->idx == 0 && !bs->use_scratch) {
906 /* If not all sectors of the slot are being swapped,
907 * guarantee here that only slot0 will have the state.
908 */
909 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1);
910 assert(rc == 0);
911 }
912
Christopher Collins92ea77f2016-12-12 15:59:26 -0800913 bs->state = 2;
Christopher Collins4772ac42017-02-27 20:08:01 -0800914 rc = boot_write_status(bs);
915 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800916 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300917
Christopher Collins92ea77f2016-12-12 15:59:26 -0800918 if (bs->state == 2) {
919 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800920 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800921
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300922 /* NOTE: also copy trailer from scratch (has status info) */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800923 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300924 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800925 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800926
Fabio Utzig94d998c2017-05-22 11:02:41 -0400927 if (bs->use_scratch) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300928 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
929 assert(rc == 0);
930
931 scratch_trailer_off = boot_status_off(fap);
932
933 flash_area_close(fap);
934
935 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
936 assert(rc == 0);
937
938 /* copy current status that is being maintained in scratch */
939 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
940 scratch_trailer_off,
941 img_off + copy_sz + BOOT_MAGIC_SZ,
942 BOOT_STATUS_STATE_COUNT * boot_data.write_sz);
943 assert(rc == 0);
944
Fabio Utzig2473ac02017-05-02 12:45:02 -0300945 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
946 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300947 assert(rc == 0);
948
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400949 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300950 rc = boot_write_image_ok(fap);
951 assert(rc == 0);
952 }
953
954 rc = boot_write_magic(fap);
955 assert(rc == 0);
956
957 flash_area_close(fap);
958 }
959
Christopher Collins92ea77f2016-12-12 15:59:26 -0800960 bs->idx++;
961 bs->state = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300962 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -0800963 rc = boot_write_status(bs);
964 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800965 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800966}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300967#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800968
969/**
970 * Swaps the two images in flash. If a prior copy operation was interrupted
971 * by a system reset, this function completes that operation.
972 *
973 * @param bs The current boot status. This function reads
974 * this struct to determine if it is resuming
975 * an interrupted swap operation. This
976 * function writes the updated status to this
977 * function on return.
978 *
979 * @return 0 on success; nonzero on failure.
980 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300981#ifdef MCUBOOT_OVERWRITE_ONLY
David Brown17609d82017-05-05 09:41:34 -0600982static int
983boot_copy_image(struct boot_status *bs)
984{
985 int sect_count;
986 int sect;
987 int rc;
988 uint32_t size = 0;
989 uint32_t this_size;
990
991 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
992 BOOT_LOG_INF("Erasing slot0");
993
994 sect_count = boot_data.imgs[0].num_sectors;
995 for (sect = 0; sect < sect_count; sect++) {
996 this_size = boot_data.imgs[0].sectors[sect].fa_size;
997 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
998 size,
999 this_size);
1000 assert(rc == 0);
1001
1002 size += this_size;
1003 }
1004
1005 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%x bytes",
1006 size);
1007 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
1008 0, 0, size);
1009
1010 /* Erase slot 1 so that we don't do the upgrade on every boot.
1011 * TODO: Perhaps verify slot 0's signature again? */
1012 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
1013 0, boot_data.imgs[1].sectors[0].fa_size);
1014 assert(rc == 0);
1015
1016 return 0;
1017}
1018#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001019static int
1020boot_copy_image(struct boot_status *bs)
1021{
1022 uint32_t sz;
1023 int first_sector_idx;
1024 int last_sector_idx;
1025 int swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001026 struct image_header *hdr;
1027 uint32_t size;
1028 uint32_t copy_size;
1029 struct image_header tmp_hdr;
1030 int rc;
1031
1032 /* FIXME: just do this if asked by user? */
1033
1034 size = copy_size = 0;
1035
1036 hdr = &boot_data.imgs[0].hdr;
1037 if (hdr->ih_magic == IMAGE_MAGIC) {
1038 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1039 }
1040
1041 hdr = &boot_data.imgs[1].hdr;
1042 if (hdr->ih_magic == IMAGE_MAGIC) {
1043 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1044 }
1045
1046 if (!size || !copy_size || size == copy_size) {
1047 rc = boot_read_image_header(2, &tmp_hdr);
1048 assert(rc == 0);
1049
1050 hdr = &tmp_hdr;
1051 if (hdr->ih_magic == IMAGE_MAGIC) {
1052 if (!size) {
1053 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1054 } else {
1055 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1056 }
1057 }
1058 }
1059
1060 if (size > copy_size) {
1061 copy_size = size;
1062 }
1063
1064 size = 0;
1065 last_sector_idx = 0;
1066 while (1) {
1067 size += boot_data.imgs[0].sectors[last_sector_idx].fa_size;
1068 if (size >= copy_size) {
1069 break;
1070 }
1071 last_sector_idx++;
1072 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001073
1074 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001075 while (last_sector_idx >= 0) {
1076 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
1077 if (swap_idx >= bs->idx) {
1078 boot_swap_sectors(first_sector_idx, sz, bs);
1079 }
1080
1081 last_sector_idx = first_sector_idx - 1;
1082 swap_idx++;
1083 }
1084
1085 return 0;
1086}
David Brown17609d82017-05-05 09:41:34 -06001087#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001088
1089/**
1090 * Marks a test image in slot 0 as fully copied.
1091 */
1092static int
1093boot_finalize_test_swap(void)
1094{
1095 const struct flash_area *fap;
1096 int rc;
1097
1098 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1099 if (rc != 0) {
1100 return BOOT_EFLASH;
1101 }
1102
1103 rc = boot_write_copy_done(fap);
1104 if (rc != 0) {
1105 return rc;
1106 }
1107
1108 return 0;
1109}
1110
1111/**
1112 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1113 * the status bytes from the image revert operation don't get processed on a
1114 * subsequent boot.
1115 */
1116static int
1117boot_finalize_revert_swap(void)
1118{
1119 const struct flash_area *fap;
1120 struct boot_swap_state state_slot0;
1121 int rc;
1122
1123 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1124 if (rc != 0) {
1125 return BOOT_EFLASH;
1126 }
1127
1128 rc = boot_read_swap_state(fap, &state_slot0);
1129 if (rc != 0) {
1130 return BOOT_EFLASH;
1131 }
1132
1133 if (state_slot0.magic == BOOT_MAGIC_UNSET) {
1134 rc = boot_write_magic(fap);
1135 if (rc != 0) {
1136 return rc;
1137 }
1138 }
1139
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001140 if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001141 rc = boot_write_copy_done(fap);
1142 if (rc != 0) {
1143 return rc;
1144 }
1145 }
1146
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001147 if (state_slot0.image_ok == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001148 rc = boot_write_image_ok(fap);
1149 if (rc != 0) {
1150 return rc;
1151 }
1152 }
1153
1154 return 0;
1155}
1156
1157/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001158 * Performs an image swap if one is required.
1159 *
1160 * @param out_swap_type On success, the type of swap performed gets
1161 * written here.
1162 *
1163 * @return 0 on success; nonzero on failure.
1164 */
1165static int
1166boot_swap_if_needed(int *out_swap_type)
1167{
1168 struct boot_status bs;
1169 int swap_type;
1170 int rc;
1171
1172 /* Determine if we rebooted in the middle of an image swap
1173 * operation.
1174 */
1175 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001176 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001177 if (rc != 0) {
1178 return rc;
1179 }
1180
1181 /* If a partial swap was detected, complete it. */
1182 if (bs.idx != 0 || bs.state != 0) {
1183 rc = boot_copy_image(&bs);
1184 assert(rc == 0);
1185
1186 /* Extrapolate the type of the partial swap. We need this
1187 * information to know how to mark the swap complete in flash.
1188 */
1189 swap_type = boot_previous_swap_type();
1190 } else {
1191 swap_type = boot_validated_swap_type();
1192 switch (swap_type) {
1193 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001194 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001195 case BOOT_SWAP_TYPE_REVERT:
1196 rc = boot_copy_image(&bs);
1197 assert(rc == 0);
1198 break;
1199 }
1200 }
1201
1202 *out_swap_type = swap_type;
1203 return 0;
1204}
1205
1206/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001207 * Prepares the booting process. This function moves images around in flash as
1208 * appropriate, and tells you what address to boot from.
1209 *
1210 * @param rsp On success, indicates how booting should occur.
1211 *
1212 * @return 0 on success; nonzero on failure.
1213 */
1214int
1215boot_go(struct boot_rsp *rsp)
1216{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001217 int swap_type;
1218 int slot;
1219 int rc;
1220
1221 /* The array of slot sectors are defined here (as opposed to file scope) so
1222 * that they don't get allocated for non-boot-loader apps. This is
1223 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001224 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001225 */
1226 static struct flash_area slot0_sectors[BOOT_MAX_IMG_SECTORS];
1227 static struct flash_area slot1_sectors[BOOT_MAX_IMG_SECTORS];
1228 boot_data.imgs[0].sectors = slot0_sectors;
1229 boot_data.imgs[1].sectors = slot1_sectors;
1230
1231 /* Determine the sector layout of the image slots and scratch area. */
1232 rc = boot_read_sectors();
1233 if (rc != 0) {
1234 return rc;
1235 }
1236
1237 /* Attempt to read an image header from each slot. */
1238 rc = boot_read_image_headers();
1239 if (rc != 0) {
1240 return rc;
1241 }
1242
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001243 /* If the image slots aren't compatible, no swap is possible. Just boot
1244 * into slot 0.
1245 */
1246 if (boot_slots_compatible()) {
1247 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001248 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001249 if (rc != 0) {
1250 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001251 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001252 } else {
1253 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001254 }
1255
1256 switch (swap_type) {
1257 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig19356bf2017-05-11 16:19:36 -03001258#ifdef MCUBOOT_VALIDATE_SLOT0
David Brownd930ec62016-12-14 07:59:48 -07001259 rc = boot_validate_slot(0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001260 assert(rc == 0);
David Brownd930ec62016-12-14 07:59:48 -07001261 if (rc != 0) {
1262 return BOOT_EBADIMAGE;
1263 }
1264#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001265 slot = 0;
1266 break;
1267
1268 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001269 case BOOT_SWAP_TYPE_PERM:
Christopher Collins92ea77f2016-12-12 15:59:26 -08001270 slot = 1;
1271 boot_finalize_test_swap();
1272 break;
1273
1274 case BOOT_SWAP_TYPE_REVERT:
1275 slot = 1;
1276 boot_finalize_revert_swap();
1277 break;
1278
1279 case BOOT_SWAP_TYPE_FAIL:
1280 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1281 * try to boot into it again on the next reboot. Do this by pretending
1282 * we just reverted back to slot 0.
1283 */
1284 slot = 0;
1285 boot_finalize_revert_swap();
1286 break;
1287
1288 default:
1289 assert(0);
1290 slot = 0;
1291 break;
1292 }
1293
1294 /* Always boot from the primary slot. */
1295 rsp->br_flash_id = boot_data.imgs[0].sectors[0].fa_device_id;
1296 rsp->br_image_addr = boot_data.imgs[0].sectors[0].fa_off;
1297 rsp->br_hdr = &boot_data.imgs[slot].hdr;
1298
1299 return 0;
1300}
1301
1302int
1303split_go(int loader_slot, int split_slot, void **entry)
1304{
1305 const struct flash_area *loader_fap;
1306 const struct flash_area *app_fap;
1307 struct flash_area *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001308 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001309 int loader_flash_id;
1310 int app_flash_id;
1311 int rc;
1312
1313 app_fap = NULL;
1314 loader_fap = NULL;
1315
1316 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1317 if (sectors == NULL) {
1318 rc = SPLIT_GO_ERR;
1319 goto done;
1320 }
1321 boot_data.imgs[0].sectors = sectors + 0;
1322 boot_data.imgs[1].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1323
1324 /* Determine the sector layout of the image slots and scratch area. */
1325 rc = boot_read_sectors();
1326 if (rc != 0) {
1327 rc = SPLIT_GO_ERR;
1328 goto done;
1329 }
1330
1331 rc = boot_read_image_headers();
1332 if (rc != 0) {
1333 goto done;
1334 }
1335
1336 app_flash_id = flash_area_id_from_image_slot(split_slot);
1337 rc = flash_area_open(app_flash_id, &app_fap);
1338 if (rc != 0) {
1339 rc = BOOT_EFLASH;
1340 goto done;
1341 }
1342
1343 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1344 rc = flash_area_open(loader_flash_id, &loader_fap);
1345 if (rc != 0) {
1346 rc = BOOT_EFLASH;
1347 goto done;
1348 }
1349
1350 /* Don't check the bootable image flag because we could really call a
1351 * bootable or non-bootable image. Just validate that the image check
1352 * passes which is distinct from the normal check.
1353 */
1354 rc = split_image_check(&boot_data.imgs[split_slot].hdr,
1355 app_fap,
1356 &boot_data.imgs[loader_slot].hdr,
1357 loader_fap);
1358 if (rc != 0) {
1359 rc = SPLIT_GO_NON_MATCHING;
1360 goto done;
1361 }
1362
1363 entry_val = boot_data.imgs[split_slot].sectors[0].fa_off +
1364 boot_data.imgs[split_slot].hdr.ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001365 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001366 rc = SPLIT_GO_OK;
1367
1368done:
1369 free(sectors);
1370 flash_area_close(app_fap);
1371 flash_area_close(loader_fap);
1372 return rc;
1373}