blob: 5990b80877d44f3fe258766a9c35a90e48624c73 [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>
Christopher Collins92ea77f2016-12-12 15:59:26 -080030#include <hal/hal_flash.h>
31#include <os/os_malloc.h>
32#include "bootutil/bootutil.h"
33#include "bootutil/image.h"
34#include "bootutil_priv.h"
35
Marti Bolivarfd20c762017-02-07 16:52:50 -050036#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
37#include "bootutil/bootutil_log.h"
38
Fabio Utzigeed80b62017-06-10 08:03:05 -030039#ifdef APP_mynewt
40#include "mynewt/config.h"
41#endif
42
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040043static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080044
45struct boot_status_table {
46 /**
47 * For each field, a value of 0 means "any".
48 */
49 uint8_t bst_magic_slot0;
50 uint8_t bst_magic_scratch;
51 uint8_t bst_copy_done_slot0;
52 uint8_t bst_status_source;
53};
54
55/**
56 * This set of tables maps swap state contents to boot status location.
57 * When searching for a match, these tables must be iterated in order.
58 */
59static const struct boot_status_table boot_status_tables[] = {
60 {
61 /* | slot-0 | scratch |
62 * ----------+------------+------------|
63 * magic | Good | Any |
64 * copy-done | 0x01 | N/A |
65 * ----------+------------+------------'
66 * source: none |
67 * ------------------------------------'
68 */
69 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
70 .bst_magic_scratch = 0,
71 .bst_copy_done_slot0 = 0x01,
72 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
73 },
74
75 {
76 /* | slot-0 | scratch |
77 * ----------+------------+------------|
78 * magic | Good | Any |
79 * copy-done | 0xff | N/A |
80 * ----------+------------+------------'
81 * source: slot 0 |
82 * ------------------------------------'
83 */
84 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
85 .bst_magic_scratch = 0,
86 .bst_copy_done_slot0 = 0xff,
87 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
88 },
89
90 {
91 /* | slot-0 | scratch |
92 * ----------+------------+------------|
93 * magic | Any | Good |
94 * copy-done | Any | N/A |
95 * ----------+------------+------------'
96 * source: scratch |
97 * ------------------------------------'
98 */
99 .bst_magic_slot0 = 0,
100 .bst_magic_scratch = BOOT_MAGIC_GOOD,
101 .bst_copy_done_slot0 = 0,
102 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
103 },
104
105 {
106 /* | slot-0 | scratch |
107 * ----------+------------+------------|
108 * magic | Unset | Any |
109 * copy-done | 0xff | N/A |
110 * ----------+------------+------------|
111 * source: varies |
112 * ------------------------------------+------------------------------+
113 * This represents one of two cases: |
114 * o No swaps ever (no status to read, so no harm in checking). |
115 * o Mid-revert; status in slot 0. |
116 * -------------------------------------------------------------------'
117 */
118 .bst_magic_slot0 = BOOT_MAGIC_UNSET,
119 .bst_magic_scratch = 0,
120 .bst_copy_done_slot0 = 0xff,
121 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
122 },
123};
124
125#define BOOT_STATUS_TABLES_COUNT \
126 (sizeof boot_status_tables / sizeof boot_status_tables[0])
127
128/**
129 * This table indicates the next swap type that should be performed. The first
130 * column contains the current swap type. The second column contains the swap
131 * type that should be effected after the first completes.
132 */
133static const uint8_t boot_swap_trans_table[][2] = {
134 /* From To */
135 { BOOT_SWAP_TYPE_REVERT, BOOT_SWAP_TYPE_NONE },
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800136 { BOOT_SWAP_TYPE_PERM, BOOT_SWAP_TYPE_NONE },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800137 { BOOT_SWAP_TYPE_TEST, BOOT_SWAP_TYPE_REVERT },
138};
139
140#define BOOT_SWAP_TRANS_TABLE_SIZE \
141 (sizeof boot_swap_trans_table / sizeof boot_swap_trans_table[0])
142
Marti Bolivarfd20c762017-02-07 16:52:50 -0500143#define BOOT_LOG_SWAP_STATE(area, state) \
144 BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \
145 (area), \
146 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
147 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
148 "bad"), \
149 (state)->copy_done, \
150 (state)->image_ok)
151
Christopher Collins92ea77f2016-12-12 15:59:26 -0800152/**
153 * Determines where in flash the most recent boot status is stored. The boot
154 * status is necessary for completing a swap that was interrupted by a boot
155 * loader reset.
156 *
157 * @return A BOOT_STATUS_SOURCE_[...] code indicating where * status should be read from.
158 */
159static int
160boot_status_source(void)
161{
162 const struct boot_status_table *table;
163 struct boot_swap_state state_scratch;
164 struct boot_swap_state state_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800165 int rc;
166 int i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500167 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800168
Fabio Utzig2473ac02017-05-02 12:45:02 -0300169 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800170 assert(rc == 0);
171
Fabio Utzig2473ac02017-05-02 12:45:02 -0300172 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800173 assert(rc == 0);
174
Marti Bolivarfd20c762017-02-07 16:52:50 -0500175 BOOT_LOG_SWAP_STATE("Image 0", &state_slot0);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500176 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
177
Christopher Collins92ea77f2016-12-12 15:59:26 -0800178 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300179 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800180
181 if ((table->bst_magic_slot0 == 0 ||
182 table->bst_magic_slot0 == state_slot0.magic) &&
183 (table->bst_magic_scratch == 0 ||
184 table->bst_magic_scratch == state_scratch.magic) &&
185 (table->bst_copy_done_slot0 == 0 ||
186 table->bst_copy_done_slot0 == state_slot0.copy_done)) {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500187 source = table->bst_status_source;
188 BOOT_LOG_INF("Boot source: %s",
189 source == BOOT_STATUS_SOURCE_NONE ? "none" :
190 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
191 source == BOOT_STATUS_SOURCE_SLOT0 ? "slot 0" :
192 "BUG; can't happen");
193 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800194 }
195 }
196
Marti Bolivarfd20c762017-02-07 16:52:50 -0500197 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800198 return BOOT_STATUS_SOURCE_NONE;
199}
200
201/**
202 * Calculates the type of swap that just completed.
203 */
204static int
205boot_previous_swap_type(void)
206{
207 int post_swap_type;
208 int i;
209
210 post_swap_type = boot_swap_type();
211
212 for (i = 0; i < BOOT_SWAP_TRANS_TABLE_SIZE; i++){
213 if (boot_swap_trans_table[i][1] == post_swap_type) {
214 return boot_swap_trans_table[i][0];
215 }
216 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300217
Christopher Collins92ea77f2016-12-12 15:59:26 -0800218 /* XXX: Temporary assert. */
219 assert(0);
220
221 return BOOT_SWAP_TYPE_REVERT;
222}
223
224static int
225boot_read_image_header(int slot, struct image_header *out_hdr)
226{
227 const struct flash_area *fap;
228 int area_id;
229 int rc;
230
231 area_id = flash_area_id_from_image_slot(slot);
232 rc = flash_area_open(area_id, &fap);
233 if (rc != 0) {
234 rc = BOOT_EFLASH;
235 goto done;
236 }
237
238 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
239 if (rc != 0) {
240 rc = BOOT_EFLASH;
241 goto done;
242 }
243
244 rc = 0;
245
246done:
247 flash_area_close(fap);
248 return rc;
249}
250
251static int
252boot_read_image_headers(void)
253{
254 int rc;
255 int i;
256
257 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400258 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800259 if (rc != 0) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300260 /* If at least the first slot's header was read successfully, then
261 * the boot loader can attempt a boot. Failure to read any headers
262 * is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800263 */
264 if (i > 0) {
265 return 0;
266 } else {
267 return rc;
268 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800269 }
270 }
271
272 return 0;
273}
274
275static uint8_t
276boot_write_sz(void)
277{
278 uint8_t elem_sz;
279 uint8_t align;
280
281 /* Figure out what size to write update status update as. The size depends
282 * on what the minimum write size is for scratch area, active image slot.
283 * We need to use the bigger of those 2 values.
284 */
Marti Bolivare2587152017-06-12 15:52:05 -0400285 elem_sz = hal_flash_align(boot_img_fa_device_id(&boot_data, 0));
286 align = hal_flash_align(boot_scratch_fa_device_id(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800287 if (align > elem_sz) {
288 elem_sz = align;
289 }
290
291 return elem_sz;
292}
293
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800294static int
295boot_slots_compatible(void)
296{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400297 size_t num_sectors_0 = boot_img_num_sectors(&boot_data, 0);
298 size_t num_sectors_1 = boot_img_num_sectors(&boot_data, 1);
299 size_t size_0, size_1;
300 size_t i;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800301
302 /* Ensure both image slots have identical sector layouts. */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400303 if (num_sectors_0 != num_sectors_1) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800304 return 0;
305 }
Marti Bolivard3269fd2017-06-12 16:31:12 -0400306 for (i = 0; i < num_sectors_0; i++) {
307 size_0 = boot_img_sector_size(&boot_data, 0, i);
308 size_1 = boot_img_sector_size(&boot_data, 1, i);
309 if (size_0 != size_1) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800310 return 0;
311 }
312 }
313
314 return 1;
315}
316
Christopher Collins92ea77f2016-12-12 15:59:26 -0800317/**
318 * Determines the sector layout of both image slots and the scratch area.
319 * This information is necessary for calculating the number of bytes to erase
320 * and copy during an image swap. The information collected during this
321 * function is used to populate the boot_data global.
322 */
323static int
324boot_read_sectors(void)
325{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800326 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800327
Marti Bolivarcca28a92017-06-12 16:52:22 -0400328 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800329 if (rc != 0) {
330 return BOOT_EFLASH;
331 }
332
Marti Bolivarcca28a92017-06-12 16:52:22 -0400333 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800334 if (rc != 0) {
335 return BOOT_EFLASH;
336 }
337
Christopher Collins92ea77f2016-12-12 15:59:26 -0800338 boot_data.write_sz = boot_write_sz();
339
340 return 0;
341}
342
343static uint32_t
344boot_status_internal_off(int idx, int state, int elem_sz)
345{
346 int idx_sz;
347
348 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
349
350 return idx * idx_sz + state * elem_sz;
351}
352
353/**
354 * Reads the status of a partially-completed swap, if any. This is necessary
355 * to recover in case the boot lodaer was reset in the middle of a swap
356 * operation.
357 */
358static int
359boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
360{
361 uint32_t off;
362 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300363 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800364 int found;
365 int rc;
366 int i;
367
368 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400369 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300370
Christopher Collins92ea77f2016-12-12 15:59:26 -0800371 found = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300372 for (i = 0; i < max_entries; i++) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800373 rc = flash_area_read(fap, off + i * boot_data.write_sz, &status, 1);
374 if (rc != 0) {
375 return BOOT_EFLASH;
376 }
377
378 if (status == 0xff) {
379 if (found) {
380 break;
381 }
382 } else if (!found) {
383 found = 1;
384 }
385 }
386
387 if (found) {
388 i--;
Fabio Utzig94d998c2017-05-22 11:02:41 -0400389 bs->idx = i / BOOT_STATUS_STATE_COUNT;
390 bs->state = i % BOOT_STATUS_STATE_COUNT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800391 }
392
393 return 0;
394}
395
396/**
397 * Reads the boot status from the flash. The boot status contains
398 * the current state of an interrupted image copy operation. If the boot
399 * status is not present, or it indicates that previous copy finished,
400 * there is no operation in progress.
401 */
402static int
403boot_read_status(struct boot_status *bs)
404{
405 const struct flash_area *fap;
406 int status_loc;
407 int area_id;
408 int rc;
409
410 memset(bs, 0, sizeof *bs);
411
412 status_loc = boot_status_source();
413 switch (status_loc) {
414 case BOOT_STATUS_SOURCE_NONE:
415 return 0;
416
417 case BOOT_STATUS_SOURCE_SCRATCH:
418 area_id = FLASH_AREA_IMAGE_SCRATCH;
419 break;
420
421 case BOOT_STATUS_SOURCE_SLOT0:
422 area_id = FLASH_AREA_IMAGE_0;
423 break;
424
425 default:
426 assert(0);
427 return BOOT_EBADARGS;
428 }
429
430 rc = flash_area_open(area_id, &fap);
431 if (rc != 0) {
432 return BOOT_EFLASH;
433 }
434
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300435 return boot_read_status_bytes(fap, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800436}
437
438/**
439 * Writes the supplied boot status to the flash file system. The boot status
440 * contains the current state of an in-progress image copy operation.
441 *
442 * @param bs The boot status to write.
443 *
444 * @return 0 on success; nonzero on failure.
445 */
446int
447boot_write_status(struct boot_status *bs)
448{
449 const struct flash_area *fap;
450 uint32_t off;
451 int area_id;
452 int rc;
David Brown9d725462017-01-23 15:50:58 -0700453 uint8_t buf[8];
454 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800455
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300456 /* NOTE: The first sector copied (that is the last sector on slot) contains
457 * the trailer. Since in the last step SLOT 0 is erased, the first
458 * two status writes go to the scratch which will be copied to SLOT 0!
459 */
460
Fabio Utzig2473ac02017-05-02 12:45:02 -0300461 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800462 /* Write to scratch. */
463 area_id = FLASH_AREA_IMAGE_SCRATCH;
464 } else {
465 /* Write to slot 0. */
466 area_id = FLASH_AREA_IMAGE_0;
467 }
468
469 rc = flash_area_open(area_id, &fap);
470 if (rc != 0) {
471 rc = BOOT_EFLASH;
472 goto done;
473 }
474
475 off = boot_status_off(fap) +
476 boot_status_internal_off(bs->idx, bs->state, boot_data.write_sz);
477
David Brown9d725462017-01-23 15:50:58 -0700478 align = hal_flash_align(fap->fa_device_id);
David Brown9d725462017-01-23 15:50:58 -0700479 memset(buf, 0xFF, 8);
480 buf[0] = bs->state;
481
482 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800483 if (rc != 0) {
484 rc = BOOT_EFLASH;
485 goto done;
486 }
487
488 rc = 0;
489
490done:
491 flash_area_close(fap);
492 return rc;
493}
494
495/*
496 * Validate image hash/signature in a slot.
497 */
498static int
499boot_image_check(struct image_header *hdr, const struct flash_area *fap)
500{
David Browndb1d9d32017-01-06 11:07:54 -0700501 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800502
Christopher Collins92ea77f2016-12-12 15:59:26 -0800503 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
504 NULL, 0, NULL)) {
505 return BOOT_EBADIMAGE;
506 }
507 return 0;
508}
509
510static int
511split_image_check(struct image_header *app_hdr,
512 const struct flash_area *app_fap,
513 struct image_header *loader_hdr,
514 const struct flash_area *loader_fap)
515{
516 static void *tmpbuf;
517 uint8_t loader_hash[32];
518
519 if (!tmpbuf) {
520 tmpbuf = malloc(BOOT_TMPBUF_SZ);
521 if (!tmpbuf) {
522 return BOOT_ENOMEM;
523 }
524 }
525
526 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
527 NULL, 0, loader_hash)) {
528 return BOOT_EBADIMAGE;
529 }
530
531 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
532 loader_hash, 32, NULL)) {
533 return BOOT_EBADIMAGE;
534 }
535
536 return 0;
537}
538
539static int
David Brownd930ec62016-12-14 07:59:48 -0700540boot_validate_slot(int slot)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800541{
542 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400543 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800544 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300545
Marti Bolivarf804f622017-06-12 15:41:48 -0400546 hdr = boot_img_hdr(&boot_data, slot);
547 if (hdr->ih_magic == 0xffffffff || hdr->ih_flags & IMAGE_F_NON_BOOTABLE) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300548 /* No bootable image in slot; continue booting from slot 0. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800549 return -1;
550 }
551
David Brownd930ec62016-12-14 07:59:48 -0700552 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800553 if (rc != 0) {
554 return BOOT_EFLASH;
555 }
556
Marti Bolivarf804f622017-06-12 15:41:48 -0400557 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap) != 0)) {
David Brownb38e0442017-02-24 13:57:12 -0700558 if (slot != 0) {
559 flash_area_erase(fap, 0, fap->fa_size);
560 /* Image in slot 1 is invalid. Erase the image and
561 * continue booting from slot 0.
562 */
563 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800564 return -1;
565 }
566
567 flash_area_close(fap);
568
569 /* Image in slot 1 is valid. */
570 return 0;
571}
572
573/**
574 * Determines which swap operation to perform, if any. If it is determined
575 * that a swap operation is required, the image in the second slot is checked
576 * for validity. If the image in the second slot is invalid, it is erased, and
577 * a swap type of "none" is indicated.
578 *
579 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
580 */
581static int
582boot_validated_swap_type(void)
583{
584 int swap_type;
585 int rc;
586
587 swap_type = boot_swap_type();
588 if (swap_type == BOOT_SWAP_TYPE_NONE) {
589 /* Continue using slot 0. */
590 return BOOT_SWAP_TYPE_NONE;
591 }
592
593 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
David Brownd930ec62016-12-14 07:59:48 -0700594 rc = boot_validate_slot(1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800595 if (rc != 0) {
596 return BOOT_SWAP_TYPE_FAIL;
597 }
598
599 return swap_type;
600}
601
602/**
603 * Calculates the number of sectors the scratch area can contain. A "last"
604 * source sector is specified because images are copied backwards in flash
605 * (final index to index number 0).
606 *
607 * @param last_sector_idx The index of the last source sector
608 * (inclusive).
609 * @param out_first_sector_idx The index of the first source sector
610 * (inclusive) gets written here.
611 *
612 * @return The number of bytes comprised by the
613 * [first-sector, last-sector] range.
614 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300615#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800616static uint32_t
617boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
618{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400619 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800620 uint32_t new_sz;
621 uint32_t sz;
622 int i;
623
624 sz = 0;
625
Marti Bolivard3269fd2017-06-12 16:31:12 -0400626 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800627 for (i = last_sector_idx; i >= 0; i--) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400628 new_sz = sz + boot_img_sector_size(&boot_data, 0, i);
629 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800630 break;
631 }
632 sz = new_sz;
633 }
634
635 /* i currently refers to a sector that doesn't fit or it is -1 because all
636 * sectors have been processed. In both cases, exclude sector i.
637 */
638 *out_first_sector_idx = i + 1;
639 return sz;
640}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300641#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800642
643/**
644 * Erases a region of flash.
645 *
646 * @param flash_area_idx The ID of the flash area containing the region
647 * to erase.
648 * @param off The offset within the flash area to start the
649 * erase.
650 * @param sz The number of bytes to erase.
651 *
652 * @return 0 on success; nonzero on failure.
653 */
654static int
655boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
656{
657 const struct flash_area *fap;
658 int rc;
659
660 rc = flash_area_open(flash_area_id, &fap);
661 if (rc != 0) {
662 rc = BOOT_EFLASH;
663 goto done;
664 }
665
666 rc = flash_area_erase(fap, off, sz);
667 if (rc != 0) {
668 rc = BOOT_EFLASH;
669 goto done;
670 }
671
672 rc = 0;
673
674done:
675 flash_area_close(fap);
676 return rc;
677}
678
679/**
680 * Copies the contents of one flash region to another. You must erase the
681 * destination region prior to calling this function.
682 *
683 * @param flash_area_id_src The ID of the source flash area.
684 * @param flash_area_id_dst The ID of the destination flash area.
685 * @param off_src The offset within the source flash area to
686 * copy from.
687 * @param off_dst The offset within the destination flash area to
688 * copy to.
689 * @param sz The number of bytes to copy.
690 *
691 * @return 0 on success; nonzero on failure.
692 */
693static int
694boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
695 uint32_t off_src, uint32_t off_dst, uint32_t sz)
696{
697 const struct flash_area *fap_src;
698 const struct flash_area *fap_dst;
699 uint32_t bytes_copied;
700 int chunk_sz;
701 int rc;
702
703 static uint8_t buf[1024];
704
705 fap_src = NULL;
706 fap_dst = NULL;
707
708 rc = flash_area_open(flash_area_id_src, &fap_src);
709 if (rc != 0) {
710 rc = BOOT_EFLASH;
711 goto done;
712 }
713
714 rc = flash_area_open(flash_area_id_dst, &fap_dst);
715 if (rc != 0) {
716 rc = BOOT_EFLASH;
717 goto done;
718 }
719
720 bytes_copied = 0;
721 while (bytes_copied < sz) {
722 if (sz - bytes_copied > sizeof buf) {
723 chunk_sz = sizeof buf;
724 } else {
725 chunk_sz = sz - bytes_copied;
726 }
727
728 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
729 if (rc != 0) {
730 rc = BOOT_EFLASH;
731 goto done;
732 }
733
734 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
735 if (rc != 0) {
736 rc = BOOT_EFLASH;
737 goto done;
738 }
739
740 bytes_copied += chunk_sz;
741 }
742
743 rc = 0;
744
745done:
746 flash_area_close(fap_src);
747 flash_area_close(fap_dst);
748 return rc;
749}
750
Fabio Utzig2473ac02017-05-02 12:45:02 -0300751static inline int
752boot_status_init_by_id(int flash_area_id)
753{
754 const struct flash_area *fap;
755 struct boot_swap_state swap_state;
756 int rc;
757
758 rc = flash_area_open(flash_area_id, &fap);
759 assert(rc == 0);
760
761 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
762 assert(rc == 0);
763
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400764 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300765 rc = boot_write_image_ok(fap);
766 assert(rc == 0);
767 }
768
769 rc = boot_write_magic(fap);
770 assert(rc == 0);
771
772 flash_area_close(fap);
773
774 return 0;
775}
776
777static int
778boot_erase_last_sector_by_id(int flash_area_id)
779{
780 uint8_t slot;
781 uint32_t last_sector;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300782 int rc;
783
784 switch (flash_area_id) {
785 case FLASH_AREA_IMAGE_0:
786 slot = 0;
787 break;
788 case FLASH_AREA_IMAGE_1:
789 slot = 1;
790 break;
791 default:
792 return BOOT_EFLASH;
793 }
794
Marti Bolivard3269fd2017-06-12 16:31:12 -0400795 last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300796 rc = boot_erase_sector(flash_area_id,
Marti Bolivarea088872017-06-12 17:10:49 -0400797 boot_img_sector_off(&boot_data, slot, last_sector),
Marti Bolivard3269fd2017-06-12 16:31:12 -0400798 boot_img_sector_size(&boot_data, slot, last_sector));
Fabio Utzig2473ac02017-05-02 12:45:02 -0300799 assert(rc == 0);
800
801 return rc;
802}
803
Christopher Collins92ea77f2016-12-12 15:59:26 -0800804/**
805 * Swaps the contents of two flash regions within the two image slots.
806 *
807 * @param idx The index of the first sector in the range of
808 * sectors being swapped.
809 * @param sz The number of bytes to swap.
810 * @param bs The current boot status. This struct gets
811 * updated according to the outcome.
812 *
813 * @return 0 on success; nonzero on failure.
814 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300815#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800816static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800817boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
818{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300819 const struct flash_area *fap;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800820 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300821 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800822 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300823 uint32_t scratch_trailer_off;
824 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400825 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800826 int rc;
827
828 /* Calculate offset from start of image area. */
Marti Bolivarea088872017-06-12 17:10:49 -0400829 img_off = boot_img_sector_off(&boot_data, 0, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800830
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300831 copy_sz = sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300832 trailer_sz = boot_slots_trailer_sz(boot_data.write_sz);
Fabio Utzig9678c972017-05-23 11:28:56 -0400833
834 /* sz in this function is always is always sized on a multiple of the
Marti Bolivarea088872017-06-12 17:10:49 -0400835 * sector size. The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -0400836 * is to determine if we're swapping the last sector. The last sector
837 * needs special handling because it's where the trailer lives. If we're
838 * copying it, we need to use scratch to write the trailer temporarily.
839 *
840 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
841 * controls if special handling is needed (swapping last sector).
842 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400843 last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
Marti Bolivarea088872017-06-12 17:10:49 -0400844 if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300845 copy_sz -= trailer_sz;
846 }
847
Fabio Utzig2473ac02017-05-02 12:45:02 -0300848 bs->use_scratch = (bs->idx == 0 && copy_sz != sz);
849
Christopher Collins92ea77f2016-12-12 15:59:26 -0800850 if (bs->state == 0) {
851 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800852 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800853
854 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300855 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800856 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800857
Fabio Utzig2473ac02017-05-02 12:45:02 -0300858 if (bs->idx == 0) {
859 if (bs->use_scratch) {
860 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH);
861 } else {
862 /* Prepare the status area... here it is known that the
863 * last sector is not being used by the image data so it's
864 * safe to erase.
865 */
866 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300867 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300868
869 boot_status_init_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300870 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300871 }
872
Christopher Collins92ea77f2016-12-12 15:59:26 -0800873 bs->state = 1;
Christopher Collins4772ac42017-02-27 20:08:01 -0800874 rc = boot_write_status(bs);
875 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800876 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300877
Christopher Collins92ea77f2016-12-12 15:59:26 -0800878 if (bs->state == 1) {
879 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800880 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800881
Christopher Collins92ea77f2016-12-12 15:59:26 -0800882 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
883 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800884 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800885
Fabio Utzig2473ac02017-05-02 12:45:02 -0300886 if (bs->idx == 0 && !bs->use_scratch) {
887 /* If not all sectors of the slot are being swapped,
888 * guarantee here that only slot0 will have the state.
889 */
890 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1);
891 assert(rc == 0);
892 }
893
Christopher Collins92ea77f2016-12-12 15:59:26 -0800894 bs->state = 2;
Christopher Collins4772ac42017-02-27 20:08:01 -0800895 rc = boot_write_status(bs);
896 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800897 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300898
Christopher Collins92ea77f2016-12-12 15:59:26 -0800899 if (bs->state == 2) {
900 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800901 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800902
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300903 /* NOTE: also copy trailer from scratch (has status info) */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800904 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300905 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800906 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800907
Fabio Utzig94d998c2017-05-22 11:02:41 -0400908 if (bs->use_scratch) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300909 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
910 assert(rc == 0);
911
912 scratch_trailer_off = boot_status_off(fap);
913
914 flash_area_close(fap);
915
916 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
917 assert(rc == 0);
918
919 /* copy current status that is being maintained in scratch */
920 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
921 scratch_trailer_off,
922 img_off + copy_sz + BOOT_MAGIC_SZ,
923 BOOT_STATUS_STATE_COUNT * boot_data.write_sz);
924 assert(rc == 0);
925
Fabio Utzig2473ac02017-05-02 12:45:02 -0300926 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
927 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300928 assert(rc == 0);
929
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400930 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300931 rc = boot_write_image_ok(fap);
932 assert(rc == 0);
933 }
934
935 rc = boot_write_magic(fap);
936 assert(rc == 0);
937
938 flash_area_close(fap);
939 }
940
Christopher Collins92ea77f2016-12-12 15:59:26 -0800941 bs->idx++;
942 bs->state = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300943 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -0800944 rc = boot_write_status(bs);
945 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800946 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800947}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300948#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800949
950/**
951 * Swaps the two images in flash. If a prior copy operation was interrupted
952 * by a system reset, this function completes that operation.
953 *
954 * @param bs The current boot status. This function reads
955 * this struct to determine if it is resuming
956 * an interrupted swap operation. This
957 * function writes the updated status to this
958 * function on return.
959 *
960 * @return 0 on success; nonzero on failure.
961 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300962#ifdef MCUBOOT_OVERWRITE_ONLY
David Brown17609d82017-05-05 09:41:34 -0600963static int
964boot_copy_image(struct boot_status *bs)
965{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400966 size_t sect_count;
967 size_t sect;
David Brown17609d82017-05-05 09:41:34 -0600968 int rc;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400969 size_t size = 0;
970 size_t this_size;
David Brown17609d82017-05-05 09:41:34 -0600971
972 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
973 BOOT_LOG_INF("Erasing slot0");
974
Marti Bolivard3269fd2017-06-12 16:31:12 -0400975 sect_count = boot_img_num_sectors(&boot_data, 0);
David Brown17609d82017-05-05 09:41:34 -0600976 for (sect = 0; sect < sect_count; sect++) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400977 this_size = boot_img_sector_size(&boot_data, 0, sect);
David Brown17609d82017-05-05 09:41:34 -0600978 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
979 size,
980 this_size);
981 assert(rc == 0);
982
983 size += this_size;
984 }
985
986 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%x bytes",
987 size);
988 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
989 0, 0, size);
990
991 /* Erase slot 1 so that we don't do the upgrade on every boot.
992 * TODO: Perhaps verify slot 0's signature again? */
993 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
Marti Bolivard3269fd2017-06-12 16:31:12 -0400994 0, boot_img_sector_size(&boot_data, 1, 0));
David Brown17609d82017-05-05 09:41:34 -0600995 assert(rc == 0);
996
997 return 0;
998}
999#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001000static int
1001boot_copy_image(struct boot_status *bs)
1002{
1003 uint32_t sz;
1004 int first_sector_idx;
1005 int last_sector_idx;
1006 int swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001007 struct image_header *hdr;
1008 uint32_t size;
1009 uint32_t copy_size;
1010 struct image_header tmp_hdr;
1011 int rc;
1012
1013 /* FIXME: just do this if asked by user? */
1014
1015 size = copy_size = 0;
1016
Marti Bolivarf804f622017-06-12 15:41:48 -04001017 hdr = boot_img_hdr(&boot_data, 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001018 if (hdr->ih_magic == IMAGE_MAGIC) {
1019 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1020 }
1021
Marti Bolivarf804f622017-06-12 15:41:48 -04001022 hdr = boot_img_hdr(&boot_data, 1);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001023 if (hdr->ih_magic == IMAGE_MAGIC) {
1024 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1025 }
1026
1027 if (!size || !copy_size || size == copy_size) {
1028 rc = boot_read_image_header(2, &tmp_hdr);
1029 assert(rc == 0);
1030
1031 hdr = &tmp_hdr;
1032 if (hdr->ih_magic == IMAGE_MAGIC) {
1033 if (!size) {
1034 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1035 } else {
1036 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1037 }
1038 }
1039 }
1040
1041 if (size > copy_size) {
1042 copy_size = size;
1043 }
1044
1045 size = 0;
1046 last_sector_idx = 0;
1047 while (1) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001048 size += boot_img_sector_size(&boot_data, 0, last_sector_idx);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001049 if (size >= copy_size) {
1050 break;
1051 }
1052 last_sector_idx++;
1053 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001054
1055 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001056 while (last_sector_idx >= 0) {
1057 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
1058 if (swap_idx >= bs->idx) {
1059 boot_swap_sectors(first_sector_idx, sz, bs);
1060 }
1061
1062 last_sector_idx = first_sector_idx - 1;
1063 swap_idx++;
1064 }
1065
1066 return 0;
1067}
David Brown17609d82017-05-05 09:41:34 -06001068#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001069
1070/**
1071 * Marks a test image in slot 0 as fully copied.
1072 */
1073static int
1074boot_finalize_test_swap(void)
1075{
1076 const struct flash_area *fap;
1077 int rc;
1078
1079 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1080 if (rc != 0) {
1081 return BOOT_EFLASH;
1082 }
1083
1084 rc = boot_write_copy_done(fap);
1085 if (rc != 0) {
1086 return rc;
1087 }
1088
1089 return 0;
1090}
1091
1092/**
1093 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1094 * the status bytes from the image revert operation don't get processed on a
1095 * subsequent boot.
1096 */
1097static int
1098boot_finalize_revert_swap(void)
1099{
1100 const struct flash_area *fap;
1101 struct boot_swap_state state_slot0;
1102 int rc;
1103
1104 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1105 if (rc != 0) {
1106 return BOOT_EFLASH;
1107 }
1108
1109 rc = boot_read_swap_state(fap, &state_slot0);
1110 if (rc != 0) {
1111 return BOOT_EFLASH;
1112 }
1113
1114 if (state_slot0.magic == BOOT_MAGIC_UNSET) {
1115 rc = boot_write_magic(fap);
1116 if (rc != 0) {
1117 return rc;
1118 }
1119 }
1120
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001121 if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001122 rc = boot_write_copy_done(fap);
1123 if (rc != 0) {
1124 return rc;
1125 }
1126 }
1127
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001128 if (state_slot0.image_ok == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001129 rc = boot_write_image_ok(fap);
1130 if (rc != 0) {
1131 return rc;
1132 }
1133 }
1134
1135 return 0;
1136}
1137
1138/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001139 * Performs an image swap if one is required.
1140 *
1141 * @param out_swap_type On success, the type of swap performed gets
1142 * written here.
1143 *
1144 * @return 0 on success; nonzero on failure.
1145 */
1146static int
1147boot_swap_if_needed(int *out_swap_type)
1148{
1149 struct boot_status bs;
1150 int swap_type;
1151 int rc;
1152
1153 /* Determine if we rebooted in the middle of an image swap
1154 * operation.
1155 */
1156 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001157 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001158 if (rc != 0) {
1159 return rc;
1160 }
1161
1162 /* If a partial swap was detected, complete it. */
1163 if (bs.idx != 0 || bs.state != 0) {
1164 rc = boot_copy_image(&bs);
1165 assert(rc == 0);
1166
1167 /* Extrapolate the type of the partial swap. We need this
1168 * information to know how to mark the swap complete in flash.
1169 */
1170 swap_type = boot_previous_swap_type();
1171 } else {
1172 swap_type = boot_validated_swap_type();
1173 switch (swap_type) {
1174 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001175 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001176 case BOOT_SWAP_TYPE_REVERT:
1177 rc = boot_copy_image(&bs);
1178 assert(rc == 0);
1179 break;
1180 }
1181 }
1182
1183 *out_swap_type = swap_type;
1184 return 0;
1185}
1186
1187/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001188 * Prepares the booting process. This function moves images around in flash as
1189 * appropriate, and tells you what address to boot from.
1190 *
1191 * @param rsp On success, indicates how booting should occur.
1192 *
1193 * @return 0 on success; nonzero on failure.
1194 */
1195int
1196boot_go(struct boot_rsp *rsp)
1197{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001198 int swap_type;
1199 int slot;
1200 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001201 int fa_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001202
1203 /* The array of slot sectors are defined here (as opposed to file scope) so
1204 * that they don't get allocated for non-boot-loader apps. This is
1205 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001206 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001207 */
1208 static struct flash_area slot0_sectors[BOOT_MAX_IMG_SECTORS];
1209 static struct flash_area slot1_sectors[BOOT_MAX_IMG_SECTORS];
1210 boot_data.imgs[0].sectors = slot0_sectors;
1211 boot_data.imgs[1].sectors = slot1_sectors;
1212
Marti Bolivarc0b47912017-06-13 17:18:09 -04001213 /* Open boot_data image areas for the duration of this call. */
1214 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1215 fa_id = flash_area_id_from_image_slot(slot);
1216 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1217 assert(rc == 0);
1218 }
1219 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1220 &BOOT_SCRATCH_AREA(&boot_data));
1221 assert(rc == 0);
1222
Christopher Collins92ea77f2016-12-12 15:59:26 -08001223 /* Determine the sector layout of the image slots and scratch area. */
1224 rc = boot_read_sectors();
1225 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001226 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001227 }
1228
1229 /* Attempt to read an image header from each slot. */
1230 rc = boot_read_image_headers();
1231 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001232 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001233 }
1234
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001235 /* If the image slots aren't compatible, no swap is possible. Just boot
1236 * into slot 0.
1237 */
1238 if (boot_slots_compatible()) {
1239 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001240 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001241 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001242 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001243 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001244 } else {
1245 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001246 }
1247
1248 switch (swap_type) {
1249 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig19356bf2017-05-11 16:19:36 -03001250#ifdef MCUBOOT_VALIDATE_SLOT0
David Brownd930ec62016-12-14 07:59:48 -07001251 rc = boot_validate_slot(0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001252 assert(rc == 0);
David Brownd930ec62016-12-14 07:59:48 -07001253 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001254 rc = BOOT_EBADIMAGE;
1255 goto out;
David Brownd930ec62016-12-14 07:59:48 -07001256 }
1257#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001258 slot = 0;
1259 break;
1260
1261 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001262 case BOOT_SWAP_TYPE_PERM:
Christopher Collins92ea77f2016-12-12 15:59:26 -08001263 slot = 1;
1264 boot_finalize_test_swap();
1265 break;
1266
1267 case BOOT_SWAP_TYPE_REVERT:
1268 slot = 1;
1269 boot_finalize_revert_swap();
1270 break;
1271
1272 case BOOT_SWAP_TYPE_FAIL:
1273 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1274 * try to boot into it again on the next reboot. Do this by pretending
1275 * we just reverted back to slot 0.
1276 */
1277 slot = 0;
1278 boot_finalize_revert_swap();
1279 break;
1280
1281 default:
1282 assert(0);
1283 slot = 0;
1284 break;
1285 }
1286
1287 /* Always boot from the primary slot. */
Marti Bolivare2587152017-06-12 15:52:05 -04001288 rsp->br_flash_id = boot_img_fa_device_id(&boot_data, 0);
Marti Bolivarea088872017-06-12 17:10:49 -04001289 rsp->br_image_addr = boot_img_slot_off(&boot_data, 0);
Marti Bolivarf804f622017-06-12 15:41:48 -04001290 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001291
Marti Bolivarc0b47912017-06-13 17:18:09 -04001292 out:
1293 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1294 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1295 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1296 }
1297 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001298}
1299
1300int
1301split_go(int loader_slot, int split_slot, void **entry)
1302{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001303 struct flash_area *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001304 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001305 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001306 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001307 int rc;
1308
Christopher Collins92ea77f2016-12-12 15:59:26 -08001309 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1310 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001311 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001312 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001313 boot_data.imgs[loader_slot].sectors = sectors + 0;
1314 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1315
1316 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1317 rc = flash_area_open(loader_flash_id,
1318 &BOOT_IMG_AREA(&boot_data, split_slot));
1319 assert(rc == 0);
1320 split_flash_id = flash_area_id_from_image_slot(split_slot);
1321 rc = flash_area_open(split_flash_id,
1322 &BOOT_IMG_AREA(&boot_data, split_slot));
1323 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001324
1325 /* Determine the sector layout of the image slots and scratch area. */
1326 rc = boot_read_sectors();
1327 if (rc != 0) {
1328 rc = SPLIT_GO_ERR;
1329 goto done;
1330 }
1331
1332 rc = boot_read_image_headers();
1333 if (rc != 0) {
1334 goto done;
1335 }
1336
Christopher Collins92ea77f2016-12-12 15:59:26 -08001337 /* Don't check the bootable image flag because we could really call a
1338 * bootable or non-bootable image. Just validate that the image check
1339 * passes which is distinct from the normal check.
1340 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001341 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001342 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001343 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001344 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001345 if (rc != 0) {
1346 rc = SPLIT_GO_NON_MATCHING;
1347 goto done;
1348 }
1349
Marti Bolivarea088872017-06-12 17:10:49 -04001350 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001351 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001352 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001353 rc = SPLIT_GO_OK;
1354
1355done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001356 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1357 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001358 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001359 return rc;
1360}