blob: e2762bd2eff54f0a888324a1125d7ab6bdfb77c6 [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>
David Brown52eee562017-07-05 11:25:09 -060027#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080028#include <inttypes.h>
29#include <stdlib.h>
30#include <string.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 Utzigba1fbe62017-07-21 14:01:20 -030040#ifdef MCUBOOT_MYNEWT
41#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030042#endif
43
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040044static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080045
Fabio Utzigf70e3022018-01-10 15:00:42 -020046#if defined(MCUBOOT_VALIDATE_SLOT0) && !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020047static int boot_status_fails = 0;
48#define BOOT_STATUS_ASSERT(x) \
49 do { \
Johann Fischered8461b2018-02-15 16:50:31 +010050 if (!(x)) { \
Fabio Utziga0e1cce2017-11-23 20:04:01 -020051 boot_status_fails++; \
52 } \
53 } while (0)
54#else
Fabio Utzig57c40f72017-12-12 21:48:30 -020055#define BOOT_STATUS_ASSERT(x) ASSERT(x)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020056#endif
57
Christopher Collins92ea77f2016-12-12 15:59:26 -080058struct boot_status_table {
59 /**
60 * For each field, a value of 0 means "any".
61 */
62 uint8_t bst_magic_slot0;
63 uint8_t bst_magic_scratch;
64 uint8_t bst_copy_done_slot0;
65 uint8_t bst_status_source;
66};
67
68/**
69 * This set of tables maps swap state contents to boot status location.
70 * When searching for a match, these tables must be iterated in order.
71 */
72static const struct boot_status_table boot_status_tables[] = {
73 {
74 /* | slot-0 | scratch |
75 * ----------+------------+------------|
76 * magic | Good | Any |
77 * copy-done | 0x01 | N/A |
78 * ----------+------------+------------'
79 * source: none |
80 * ------------------------------------'
81 */
82 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
83 .bst_magic_scratch = 0,
84 .bst_copy_done_slot0 = 0x01,
85 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
86 },
87
88 {
89 /* | slot-0 | scratch |
90 * ----------+------------+------------|
91 * magic | Good | Any |
92 * copy-done | 0xff | N/A |
93 * ----------+------------+------------'
94 * source: slot 0 |
95 * ------------------------------------'
96 */
97 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
98 .bst_magic_scratch = 0,
99 .bst_copy_done_slot0 = 0xff,
100 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
101 },
102
103 {
104 /* | slot-0 | scratch |
105 * ----------+------------+------------|
106 * magic | Any | Good |
107 * copy-done | Any | N/A |
108 * ----------+------------+------------'
109 * source: scratch |
110 * ------------------------------------'
111 */
112 .bst_magic_slot0 = 0,
113 .bst_magic_scratch = BOOT_MAGIC_GOOD,
114 .bst_copy_done_slot0 = 0,
115 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
116 },
117
118 {
119 /* | slot-0 | scratch |
120 * ----------+------------+------------|
121 * magic | Unset | Any |
122 * copy-done | 0xff | N/A |
123 * ----------+------------+------------|
124 * source: varies |
125 * ------------------------------------+------------------------------+
126 * This represents one of two cases: |
127 * o No swaps ever (no status to read, so no harm in checking). |
128 * o Mid-revert; status in slot 0. |
129 * -------------------------------------------------------------------'
130 */
131 .bst_magic_slot0 = BOOT_MAGIC_UNSET,
132 .bst_magic_scratch = 0,
133 .bst_copy_done_slot0 = 0xff,
134 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
135 },
136};
137
138#define BOOT_STATUS_TABLES_COUNT \
139 (sizeof boot_status_tables / sizeof boot_status_tables[0])
140
Marti Bolivarfd20c762017-02-07 16:52:50 -0500141#define BOOT_LOG_SWAP_STATE(area, state) \
142 BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \
143 (area), \
144 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
145 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
146 "bad"), \
147 (state)->copy_done, \
148 (state)->image_ok)
149
Christopher Collins92ea77f2016-12-12 15:59:26 -0800150/**
151 * Determines where in flash the most recent boot status is stored. The boot
152 * status is necessary for completing a swap that was interrupted by a boot
153 * loader reset.
154 *
155 * @return A BOOT_STATUS_SOURCE_[...] code indicating where * status should be read from.
156 */
157static int
158boot_status_source(void)
159{
160 const struct boot_status_table *table;
161 struct boot_swap_state state_scratch;
162 struct boot_swap_state state_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800163 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200164 size_t i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500165 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800166
Fabio Utzig2473ac02017-05-02 12:45:02 -0300167 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800168 assert(rc == 0);
169
Fabio Utzig2473ac02017-05-02 12:45:02 -0300170 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800171 assert(rc == 0);
172
Marti Bolivarfd20c762017-02-07 16:52:50 -0500173 BOOT_LOG_SWAP_STATE("Image 0", &state_slot0);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500174 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
175
Christopher Collins92ea77f2016-12-12 15:59:26 -0800176 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300177 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800178
179 if ((table->bst_magic_slot0 == 0 ||
180 table->bst_magic_slot0 == state_slot0.magic) &&
181 (table->bst_magic_scratch == 0 ||
182 table->bst_magic_scratch == state_scratch.magic) &&
183 (table->bst_copy_done_slot0 == 0 ||
184 table->bst_copy_done_slot0 == state_slot0.copy_done)) {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500185 source = table->bst_status_source;
186 BOOT_LOG_INF("Boot source: %s",
187 source == BOOT_STATUS_SOURCE_NONE ? "none" :
188 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
189 source == BOOT_STATUS_SOURCE_SLOT0 ? "slot 0" :
190 "BUG; can't happen");
191 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800192 }
193 }
194
Marti Bolivarfd20c762017-02-07 16:52:50 -0500195 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800196 return BOOT_STATUS_SOURCE_NONE;
197}
198
199/**
200 * Calculates the type of swap that just completed.
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300201 *
202 * This is used when a swap is interrupted by an external event. After
203 * finishing the swap operation determines what the initial request was.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800204 */
205static int
206boot_previous_swap_type(void)
207{
208 int post_swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800209
210 post_swap_type = boot_swap_type();
211
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300212 switch (post_swap_type) {
213 case BOOT_SWAP_TYPE_NONE : return BOOT_SWAP_TYPE_PERM;
214 case BOOT_SWAP_TYPE_REVERT : return BOOT_SWAP_TYPE_TEST;
215 case BOOT_SWAP_TYPE_PANIC : return BOOT_SWAP_TYPE_PANIC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800216 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300217
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300218 return BOOT_SWAP_TYPE_FAIL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800219}
220
David Brownf5b33d82017-09-01 10:58:27 -0600221/*
222 * Compute the total size of the given image. Includes the size of
223 * the TLVs.
224 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300225#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600226static int
227boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
228{
229 const struct flash_area *fap;
230 struct image_tlv_info info;
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, hdr->ih_hdr_size + hdr->ih_img_size,
242 &info, sizeof(info));
243 if (rc != 0) {
244 rc = BOOT_EFLASH;
245 goto done;
246 }
247 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
248 rc = BOOT_EBADIMAGE;
249 goto done;
250 }
251 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
252 rc = 0;
253
254done:
255 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300256 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600257}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300258#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600259
Fabio Utzigc08ed212017-06-20 19:28:36 -0300260static int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800261boot_read_image_header(int slot, struct image_header *out_hdr)
262{
263 const struct flash_area *fap;
264 int area_id;
265 int rc;
266
267 area_id = flash_area_id_from_image_slot(slot);
268 rc = flash_area_open(area_id, &fap);
269 if (rc != 0) {
270 rc = BOOT_EFLASH;
271 goto done;
272 }
273
274 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
275 if (rc != 0) {
276 rc = BOOT_EFLASH;
277 goto done;
278 }
279
280 rc = 0;
281
282done:
283 flash_area_close(fap);
284 return rc;
285}
286
287static int
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200288boot_read_image_headers(bool require_all)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800289{
290 int rc;
291 int i;
292
293 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400294 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800295 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200296 /* If `require_all` is set, fail on any single fail, otherwise
297 * if at least the first slot's header was read successfully,
298 * then the boot loader can attempt a boot.
299 *
300 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800301 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200302 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800303 return 0;
304 } else {
305 return rc;
306 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800307 }
308 }
309
310 return 0;
311}
312
313static uint8_t
314boot_write_sz(void)
315{
316 uint8_t elem_sz;
317 uint8_t align;
318
319 /* Figure out what size to write update status update as. The size depends
320 * on what the minimum write size is for scratch area, active image slot.
321 * We need to use the bigger of those 2 values.
322 */
Marti Bolivare2587152017-06-12 15:52:05 -0400323 elem_sz = hal_flash_align(boot_img_fa_device_id(&boot_data, 0));
324 align = hal_flash_align(boot_scratch_fa_device_id(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800325 if (align > elem_sz) {
326 elem_sz = align;
327 }
328
329 return elem_sz;
330}
331
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800332static int
333boot_slots_compatible(void)
334{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400335 size_t num_sectors_0 = boot_img_num_sectors(&boot_data, 0);
336 size_t num_sectors_1 = boot_img_num_sectors(&boot_data, 1);
337 size_t size_0, size_1;
338 size_t i;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800339
Fabio Utziga1fae672018-03-30 10:52:38 -0300340 if (num_sectors_0 > BOOT_MAX_IMG_SECTORS || num_sectors_1 > BOOT_MAX_IMG_SECTORS) {
341 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800342 return 0;
343 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300344
345 /* Ensure both image slots have identical sector layouts. */
346 if (num_sectors_0 != num_sectors_1) {
347 BOOT_LOG_WRN("Cannot upgrade: number of sectors differ between slots");
348 return 0;
349 }
350
Marti Bolivard3269fd2017-06-12 16:31:12 -0400351 for (i = 0; i < num_sectors_0; i++) {
352 size_0 = boot_img_sector_size(&boot_data, 0, i);
353 size_1 = boot_img_sector_size(&boot_data, 1, i);
354 if (size_0 != size_1) {
Fabio Utziga1fae672018-03-30 10:52:38 -0300355 BOOT_LOG_WRN("Cannot upgrade: an incompatible sector was found");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800356 return 0;
357 }
358 }
359
360 return 1;
361}
362
Christopher Collins92ea77f2016-12-12 15:59:26 -0800363/**
364 * Determines the sector layout of both image slots and the scratch area.
365 * This information is necessary for calculating the number of bytes to erase
366 * and copy during an image swap. The information collected during this
367 * function is used to populate the boot_data global.
368 */
369static int
370boot_read_sectors(void)
371{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800372 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800373
Marti Bolivarcca28a92017-06-12 16:52:22 -0400374 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800375 if (rc != 0) {
376 return BOOT_EFLASH;
377 }
378
Marti Bolivarcca28a92017-06-12 16:52:22 -0400379 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800380 if (rc != 0) {
381 return BOOT_EFLASH;
382 }
383
Marti Bolivare10a7392017-06-14 16:20:07 -0400384 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800385
386 return 0;
387}
388
389static uint32_t
390boot_status_internal_off(int idx, int state, int elem_sz)
391{
392 int idx_sz;
393
394 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
395
396 return idx * idx_sz + state * elem_sz;
397}
398
399/**
400 * Reads the status of a partially-completed swap, if any. This is necessary
401 * to recover in case the boot lodaer was reset in the middle of a swap
402 * operation.
403 */
404static int
405boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
406{
407 uint32_t off;
408 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300409 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800410 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200411 int found_idx;
412 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800413 int rc;
414 int i;
415
416 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400417 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300418
Christopher Collins92ea77f2016-12-12 15:59:26 -0800419 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200420 found_idx = 0;
421 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300422 for (i = 0; i < max_entries; i++) {
Marti Bolivare10a7392017-06-14 16:20:07 -0400423 rc = flash_area_read(fap, off + i * BOOT_WRITE_SZ(&boot_data),
424 &status, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800425 if (rc != 0) {
426 return BOOT_EFLASH;
427 }
428
429 if (status == 0xff) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200430 if (found && !found_idx) {
431 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800432 }
433 } else if (!found) {
434 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200435 } else if (found_idx) {
436 invalid = 1;
437 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800438 }
439 }
440
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200441 if (invalid) {
442 /* This means there was an error writing status on the last
443 * swap. Tell user and move on to validation!
444 */
445 BOOT_LOG_ERR("Detected inconsistent status!");
446
447#if !defined(MCUBOOT_VALIDATE_SLOT0)
448 /* With validation of slot0 disabled, there is no way to be sure the
449 * swapped slot0 is OK, so abort!
450 */
451 assert(0);
452#endif
453 }
454
Christopher Collins92ea77f2016-12-12 15:59:26 -0800455 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200456 if (!found_idx) {
457 found_idx = i;
458 }
459 found_idx--;
460 bs->idx = found_idx / BOOT_STATUS_STATE_COUNT;
461 bs->state = found_idx % BOOT_STATUS_STATE_COUNT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800462 }
463
464 return 0;
465}
466
467/**
468 * Reads the boot status from the flash. The boot status contains
469 * the current state of an interrupted image copy operation. If the boot
470 * status is not present, or it indicates that previous copy finished,
471 * there is no operation in progress.
472 */
473static int
474boot_read_status(struct boot_status *bs)
475{
476 const struct flash_area *fap;
477 int status_loc;
478 int area_id;
479 int rc;
480
481 memset(bs, 0, sizeof *bs);
482
483 status_loc = boot_status_source();
484 switch (status_loc) {
485 case BOOT_STATUS_SOURCE_NONE:
486 return 0;
487
488 case BOOT_STATUS_SOURCE_SCRATCH:
489 area_id = FLASH_AREA_IMAGE_SCRATCH;
490 break;
491
492 case BOOT_STATUS_SOURCE_SLOT0:
493 area_id = FLASH_AREA_IMAGE_0;
494 break;
495
496 default:
497 assert(0);
498 return BOOT_EBADARGS;
499 }
500
501 rc = flash_area_open(area_id, &fap);
502 if (rc != 0) {
503 return BOOT_EFLASH;
504 }
505
Fabio Utzig46490722017-09-04 15:34:32 -0300506 rc = boot_read_status_bytes(fap, bs);
507
508 flash_area_close(fap);
509 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800510}
511
512/**
513 * Writes the supplied boot status to the flash file system. The boot status
514 * contains the current state of an in-progress image copy operation.
515 *
516 * @param bs The boot status to write.
517 *
518 * @return 0 on success; nonzero on failure.
519 */
520int
521boot_write_status(struct boot_status *bs)
522{
523 const struct flash_area *fap;
524 uint32_t off;
525 int area_id;
526 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300527 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700528 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800529
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300530 /* NOTE: The first sector copied (that is the last sector on slot) contains
531 * the trailer. Since in the last step SLOT 0 is erased, the first
532 * two status writes go to the scratch which will be copied to SLOT 0!
533 */
534
Fabio Utzig2473ac02017-05-02 12:45:02 -0300535 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800536 /* Write to scratch. */
537 area_id = FLASH_AREA_IMAGE_SCRATCH;
538 } else {
539 /* Write to slot 0. */
540 area_id = FLASH_AREA_IMAGE_0;
541 }
542
543 rc = flash_area_open(area_id, &fap);
544 if (rc != 0) {
545 rc = BOOT_EFLASH;
546 goto done;
547 }
548
549 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400550 boot_status_internal_off(bs->idx, bs->state,
551 BOOT_WRITE_SZ(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800552
David Brown9d725462017-01-23 15:50:58 -0700553 align = hal_flash_align(fap->fa_device_id);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300554 memset(buf, 0xFF, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700555 buf[0] = bs->state;
556
557 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800558 if (rc != 0) {
559 rc = BOOT_EFLASH;
560 goto done;
561 }
562
563 rc = 0;
564
565done:
566 flash_area_close(fap);
567 return rc;
568}
569
570/*
571 * Validate image hash/signature in a slot.
572 */
573static int
574boot_image_check(struct image_header *hdr, const struct flash_area *fap)
575{
David Browndb1d9d32017-01-06 11:07:54 -0700576 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800577
Christopher Collins92ea77f2016-12-12 15:59:26 -0800578 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
579 NULL, 0, NULL)) {
580 return BOOT_EBADIMAGE;
581 }
582 return 0;
583}
584
585static int
586split_image_check(struct image_header *app_hdr,
587 const struct flash_area *app_fap,
588 struct image_header *loader_hdr,
589 const struct flash_area *loader_fap)
590{
591 static void *tmpbuf;
592 uint8_t loader_hash[32];
593
594 if (!tmpbuf) {
595 tmpbuf = malloc(BOOT_TMPBUF_SZ);
596 if (!tmpbuf) {
597 return BOOT_ENOMEM;
598 }
599 }
600
601 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
602 NULL, 0, loader_hash)) {
603 return BOOT_EBADIMAGE;
604 }
605
606 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
607 loader_hash, 32, NULL)) {
608 return BOOT_EBADIMAGE;
609 }
610
611 return 0;
612}
613
614static int
David Brownd930ec62016-12-14 07:59:48 -0700615boot_validate_slot(int slot)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800616{
617 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400618 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800619 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300620
Marti Bolivarf804f622017-06-12 15:41:48 -0400621 hdr = boot_img_hdr(&boot_data, slot);
622 if (hdr->ih_magic == 0xffffffff || hdr->ih_flags & IMAGE_F_NON_BOOTABLE) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300623 /* No bootable image in slot; continue booting from slot 0. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800624 return -1;
625 }
626
David Brownd930ec62016-12-14 07:59:48 -0700627 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800628 if (rc != 0) {
629 return BOOT_EFLASH;
630 }
631
Marti Bolivarf804f622017-06-12 15:41:48 -0400632 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap) != 0)) {
David Brownb38e0442017-02-24 13:57:12 -0700633 if (slot != 0) {
634 flash_area_erase(fap, 0, fap->fa_size);
635 /* Image in slot 1 is invalid. Erase the image and
636 * continue booting from slot 0.
637 */
638 }
Fabio Utzigb6297af2017-10-05 18:26:36 -0300639 BOOT_LOG_ERR("Image in slot %d is not valid!", slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800640 return -1;
641 }
642
643 flash_area_close(fap);
644
645 /* Image in slot 1 is valid. */
646 return 0;
647}
648
649/**
650 * Determines which swap operation to perform, if any. If it is determined
651 * that a swap operation is required, the image in the second slot is checked
652 * for validity. If the image in the second slot is invalid, it is erased, and
653 * a swap type of "none" is indicated.
654 *
655 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
656 */
657static int
658boot_validated_swap_type(void)
659{
660 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800661
662 swap_type = boot_swap_type();
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300663 switch (swap_type) {
664 case BOOT_SWAP_TYPE_TEST:
665 case BOOT_SWAP_TYPE_PERM:
666 case BOOT_SWAP_TYPE_REVERT:
667 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
668 if (boot_validate_slot(1) != 0) {
669 swap_type = BOOT_SWAP_TYPE_FAIL;
670 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800671 }
672
673 return swap_type;
674}
675
676/**
677 * Calculates the number of sectors the scratch area can contain. A "last"
678 * source sector is specified because images are copied backwards in flash
679 * (final index to index number 0).
680 *
681 * @param last_sector_idx The index of the last source sector
682 * (inclusive).
683 * @param out_first_sector_idx The index of the first source sector
684 * (inclusive) gets written here.
685 *
686 * @return The number of bytes comprised by the
687 * [first-sector, last-sector] range.
688 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300689#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800690static uint32_t
691boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
692{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400693 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800694 uint32_t new_sz;
695 uint32_t sz;
696 int i;
697
698 sz = 0;
699
Marti Bolivard3269fd2017-06-12 16:31:12 -0400700 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800701 for (i = last_sector_idx; i >= 0; i--) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400702 new_sz = sz + boot_img_sector_size(&boot_data, 0, i);
703 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800704 break;
705 }
706 sz = new_sz;
707 }
708
709 /* i currently refers to a sector that doesn't fit or it is -1 because all
710 * sectors have been processed. In both cases, exclude sector i.
711 */
712 *out_first_sector_idx = i + 1;
713 return sz;
714}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300715#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800716
717/**
718 * Erases a region of flash.
719 *
720 * @param flash_area_idx The ID of the flash area containing the region
721 * to erase.
722 * @param off The offset within the flash area to start the
723 * erase.
724 * @param sz The number of bytes to erase.
725 *
726 * @return 0 on success; nonzero on failure.
727 */
728static int
729boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
730{
731 const struct flash_area *fap;
732 int rc;
733
734 rc = flash_area_open(flash_area_id, &fap);
735 if (rc != 0) {
736 rc = BOOT_EFLASH;
737 goto done;
738 }
739
740 rc = flash_area_erase(fap, off, sz);
741 if (rc != 0) {
742 rc = BOOT_EFLASH;
743 goto done;
744 }
745
746 rc = 0;
747
748done:
749 flash_area_close(fap);
750 return rc;
751}
752
753/**
754 * Copies the contents of one flash region to another. You must erase the
755 * destination region prior to calling this function.
756 *
757 * @param flash_area_id_src The ID of the source flash area.
758 * @param flash_area_id_dst The ID of the destination flash area.
759 * @param off_src The offset within the source flash area to
760 * copy from.
761 * @param off_dst The offset within the destination flash area to
762 * copy to.
763 * @param sz The number of bytes to copy.
764 *
765 * @return 0 on success; nonzero on failure.
766 */
767static int
768boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
769 uint32_t off_src, uint32_t off_dst, uint32_t sz)
770{
771 const struct flash_area *fap_src;
772 const struct flash_area *fap_dst;
773 uint32_t bytes_copied;
774 int chunk_sz;
775 int rc;
776
777 static uint8_t buf[1024];
778
779 fap_src = NULL;
780 fap_dst = NULL;
781
782 rc = flash_area_open(flash_area_id_src, &fap_src);
783 if (rc != 0) {
784 rc = BOOT_EFLASH;
785 goto done;
786 }
787
788 rc = flash_area_open(flash_area_id_dst, &fap_dst);
789 if (rc != 0) {
790 rc = BOOT_EFLASH;
791 goto done;
792 }
793
794 bytes_copied = 0;
795 while (bytes_copied < sz) {
796 if (sz - bytes_copied > sizeof buf) {
797 chunk_sz = sizeof buf;
798 } else {
799 chunk_sz = sz - bytes_copied;
800 }
801
802 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
803 if (rc != 0) {
804 rc = BOOT_EFLASH;
805 goto done;
806 }
807
808 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
809 if (rc != 0) {
810 rc = BOOT_EFLASH;
811 goto done;
812 }
813
814 bytes_copied += chunk_sz;
815 }
816
817 rc = 0;
818
819done:
Fabio Utzige7686262017-06-28 09:26:54 -0300820 if (fap_src) {
821 flash_area_close(fap_src);
822 }
823 if (fap_dst) {
824 flash_area_close(fap_dst);
825 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800826 return rc;
827}
828
David Brown6b1b3b92017-09-19 08:59:10 -0600829#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300830static inline int
Fabio Utzig46490722017-09-04 15:34:32 -0300831boot_status_init_by_id(int flash_area_id, const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300832{
833 const struct flash_area *fap;
834 struct boot_swap_state swap_state;
835 int rc;
836
837 rc = flash_area_open(flash_area_id, &fap);
838 assert(rc == 0);
839
840 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
841 assert(rc == 0);
842
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400843 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300844 rc = boot_write_image_ok(fap);
845 assert(rc == 0);
846 }
847
Fabio Utzig46490722017-09-04 15:34:32 -0300848 rc = boot_write_swap_size(fap, bs->swap_size);
849 assert(rc == 0);
850
Fabio Utzig2473ac02017-05-02 12:45:02 -0300851 rc = boot_write_magic(fap);
852 assert(rc == 0);
853
854 flash_area_close(fap);
855
856 return 0;
857}
David Brown6b1b3b92017-09-19 08:59:10 -0600858#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -0300859
Fabio Utzig358c9352017-07-25 22:10:45 -0300860#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300861static int
862boot_erase_last_sector_by_id(int flash_area_id)
863{
864 uint8_t slot;
865 uint32_t last_sector;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300866 int rc;
867
868 switch (flash_area_id) {
869 case FLASH_AREA_IMAGE_0:
870 slot = 0;
871 break;
872 case FLASH_AREA_IMAGE_1:
873 slot = 1;
874 break;
875 default:
876 return BOOT_EFLASH;
877 }
878
Marti Bolivard3269fd2017-06-12 16:31:12 -0400879 last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300880 rc = boot_erase_sector(flash_area_id,
Marti Bolivarea088872017-06-12 17:10:49 -0400881 boot_img_sector_off(&boot_data, slot, last_sector),
Marti Bolivard3269fd2017-06-12 16:31:12 -0400882 boot_img_sector_size(&boot_data, slot, last_sector));
Fabio Utzig2473ac02017-05-02 12:45:02 -0300883 assert(rc == 0);
884
885 return rc;
886}
Fabio Utzig358c9352017-07-25 22:10:45 -0300887#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -0300888
Christopher Collins92ea77f2016-12-12 15:59:26 -0800889/**
890 * Swaps the contents of two flash regions within the two image slots.
891 *
892 * @param idx The index of the first sector in the range of
893 * sectors being swapped.
894 * @param sz The number of bytes to swap.
895 * @param bs The current boot status. This struct gets
896 * updated according to the outcome.
897 *
898 * @return 0 on success; nonzero on failure.
899 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300900#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800901static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800902boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
903{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300904 const struct flash_area *fap;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800905 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300906 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800907 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300908 uint32_t scratch_trailer_off;
909 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400910 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800911 int rc;
912
913 /* Calculate offset from start of image area. */
Marti Bolivarea088872017-06-12 17:10:49 -0400914 img_off = boot_img_sector_off(&boot_data, 0, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800915
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300916 copy_sz = sz;
Marti Bolivare10a7392017-06-14 16:20:07 -0400917 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -0400918
919 /* sz in this function is always is always sized on a multiple of the
Marti Bolivarea088872017-06-12 17:10:49 -0400920 * sector size. The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -0400921 * is to determine if we're swapping the last sector. The last sector
922 * needs special handling because it's where the trailer lives. If we're
923 * copying it, we need to use scratch to write the trailer temporarily.
924 *
925 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
926 * controls if special handling is needed (swapping last sector).
927 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400928 last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
Marti Bolivarea088872017-06-12 17:10:49 -0400929 if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300930 copy_sz -= trailer_sz;
931 }
932
Fabio Utzig2473ac02017-05-02 12:45:02 -0300933 bs->use_scratch = (bs->idx == 0 && copy_sz != sz);
934
Christopher Collins92ea77f2016-12-12 15:59:26 -0800935 if (bs->state == 0) {
936 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800937 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800938
939 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300940 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800941 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800942
Fabio Utzig2473ac02017-05-02 12:45:02 -0300943 if (bs->idx == 0) {
944 if (bs->use_scratch) {
Fabio Utzig46490722017-09-04 15:34:32 -0300945 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH, bs);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300946 } else {
947 /* Prepare the status area... here it is known that the
948 * last sector is not being used by the image data so it's
949 * safe to erase.
950 */
951 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300952 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300953
Fabio Utzig46490722017-09-04 15:34:32 -0300954 boot_status_init_by_id(FLASH_AREA_IMAGE_0, bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300955 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300956 }
957
Christopher Collins92ea77f2016-12-12 15:59:26 -0800958 bs->state = 1;
Christopher Collins4772ac42017-02-27 20:08:01 -0800959 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200960 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800961 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300962
Christopher Collins92ea77f2016-12-12 15:59:26 -0800963 if (bs->state == 1) {
964 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800965 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800966
Christopher Collins92ea77f2016-12-12 15:59:26 -0800967 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
968 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800969 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800970
Fabio Utzig2473ac02017-05-02 12:45:02 -0300971 if (bs->idx == 0 && !bs->use_scratch) {
972 /* If not all sectors of the slot are being swapped,
973 * guarantee here that only slot0 will have the state.
974 */
975 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1);
976 assert(rc == 0);
977 }
978
Christopher Collins92ea77f2016-12-12 15:59:26 -0800979 bs->state = 2;
Christopher Collins4772ac42017-02-27 20:08:01 -0800980 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200981 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800982 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300983
Christopher Collins92ea77f2016-12-12 15:59:26 -0800984 if (bs->state == 2) {
985 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800986 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800987
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300988 /* NOTE: also copy trailer from scratch (has status info) */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800989 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300990 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800991 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800992
Fabio Utzig94d998c2017-05-22 11:02:41 -0400993 if (bs->use_scratch) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300994 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
995 assert(rc == 0);
996
997 scratch_trailer_off = boot_status_off(fap);
998
999 flash_area_close(fap);
1000
1001 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1002 assert(rc == 0);
1003
1004 /* copy current status that is being maintained in scratch */
1005 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Marti Bolivare10a7392017-06-14 16:20:07 -04001006 scratch_trailer_off,
Fabio Utziga0bc9b52017-06-28 09:19:55 -03001007 img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -04001008 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001009 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001010
Fabio Utzig2473ac02017-05-02 12:45:02 -03001011 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1012 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001013 assert(rc == 0);
1014
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001015 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001016 rc = boot_write_image_ok(fap);
1017 assert(rc == 0);
1018 }
1019
Fabio Utzig46490722017-09-04 15:34:32 -03001020 rc = boot_write_swap_size(fap, bs->swap_size);
1021 assert(rc == 0);
1022
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001023 rc = boot_write_magic(fap);
1024 assert(rc == 0);
1025
1026 flash_area_close(fap);
1027 }
1028
Christopher Collins92ea77f2016-12-12 15:59:26 -08001029 bs->idx++;
1030 bs->state = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001031 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -08001032 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001033 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001034 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001035}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001036#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001037
1038/**
1039 * Swaps the two images in flash. If a prior copy operation was interrupted
1040 * by a system reset, this function completes that operation.
1041 *
1042 * @param bs The current boot status. This function reads
1043 * this struct to determine if it is resuming
1044 * an interrupted swap operation. This
1045 * function writes the updated status to this
1046 * function on return.
1047 *
1048 * @return 0 on success; nonzero on failure.
1049 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001050#ifdef MCUBOOT_OVERWRITE_ONLY
David Brown17609d82017-05-05 09:41:34 -06001051static int
1052boot_copy_image(struct boot_status *bs)
1053{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001054 size_t sect_count;
1055 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001056 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001057 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001058 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001059 size_t last_sector;
1060
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001061 (void)bs;
1062
Fabio Utzig13d9e352017-10-05 20:32:31 -03001063#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1064 uint32_t src_size = 0;
1065 rc = boot_read_image_size(1, boot_img_hdr(&boot_data, 1), &src_size);
1066 assert(rc == 0);
1067#endif
David Brown17609d82017-05-05 09:41:34 -06001068
1069 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
1070 BOOT_LOG_INF("Erasing slot0");
1071
Marti Bolivard3269fd2017-06-12 16:31:12 -04001072 sect_count = boot_img_num_sectors(&boot_data, 0);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001073 for (sect = 0, size = 0; sect < sect_count; sect++) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001074 this_size = boot_img_sector_size(&boot_data, 0, sect);
David Brown17609d82017-05-05 09:41:34 -06001075 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
1076 size,
1077 this_size);
1078 assert(rc == 0);
1079
1080 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001081
1082#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1083 if (size >= src_size) {
1084 break;
1085 }
1086#endif
David Brown17609d82017-05-05 09:41:34 -06001087 }
1088
Fabio Utzig6a537ee2017-09-13 17:31:44 -03001089 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%lx bytes", size);
David Brown17609d82017-05-05 09:41:34 -06001090 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
1091 0, 0, size);
1092
Fabio Utzig13d9e352017-10-05 20:32:31 -03001093 /*
1094 * Erases header and trailer. The trailer is erased because when a new
1095 * image is written without a trailer as is the case when using newt, the
1096 * trailer that was left might trigger a new upgrade.
1097 */
David Brown17609d82017-05-05 09:41:34 -06001098 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
Fabio Utzig13d9e352017-10-05 20:32:31 -03001099 boot_img_sector_off(&boot_data, 1, 0),
1100 boot_img_sector_size(&boot_data, 1, 0));
David Brown17609d82017-05-05 09:41:34 -06001101 assert(rc == 0);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001102 last_sector = boot_img_num_sectors(&boot_data, 1) - 1;
1103 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
1104 boot_img_sector_off(&boot_data, 1, last_sector),
1105 boot_img_sector_size(&boot_data, 1, last_sector));
1106 assert(rc == 0);
1107
1108 /* TODO: Perhaps verify slot 0's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001109
1110 return 0;
1111}
1112#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001113static int
1114boot_copy_image(struct boot_status *bs)
1115{
1116 uint32_t sz;
1117 int first_sector_idx;
1118 int last_sector_idx;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001119 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001120 struct image_header *hdr;
1121 uint32_t size;
1122 uint32_t copy_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001123 int rc;
1124
1125 /* FIXME: just do this if asked by user? */
1126
1127 size = copy_size = 0;
1128
Fabio Utzig46490722017-09-04 15:34:32 -03001129 if (bs->idx == 0 && bs->state == 0) {
1130 /*
1131 * No swap ever happened, so need to find the largest image which
1132 * will be used to determine the amount of sectors to swap.
1133 */
1134 hdr = boot_img_hdr(&boot_data, 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001135 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzig46490722017-09-04 15:34:32 -03001136 rc = boot_read_image_size(0, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001137 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001138 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001139
Fabio Utzig46490722017-09-04 15:34:32 -03001140 hdr = boot_img_hdr(&boot_data, 1);
1141 if (hdr->ih_magic == IMAGE_MAGIC) {
1142 rc = boot_read_image_size(1, hdr, &size);
1143 assert(rc == 0);
1144 }
1145
1146 if (size > copy_size) {
1147 copy_size = size;
1148 }
1149
1150 bs->swap_size = copy_size;
1151 } else {
1152 /*
1153 * If a swap was under way, the swap_size should already be present
1154 * in the trailer...
1155 */
1156 rc = boot_read_swap_size(&bs->swap_size);
1157 assert(rc == 0);
1158
1159 copy_size = bs->swap_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001160 }
1161
1162 size = 0;
1163 last_sector_idx = 0;
1164 while (1) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001165 size += boot_img_sector_size(&boot_data, 0, last_sector_idx);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001166 if (size >= copy_size) {
1167 break;
1168 }
1169 last_sector_idx++;
1170 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001171
1172 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001173 while (last_sector_idx >= 0) {
1174 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
1175 if (swap_idx >= bs->idx) {
1176 boot_swap_sectors(first_sector_idx, sz, bs);
1177 }
1178
1179 last_sector_idx = first_sector_idx - 1;
1180 swap_idx++;
1181 }
1182
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001183#ifdef MCUBOOT_VALIDATE_SLOT0
1184 if (boot_status_fails > 0) {
1185 BOOT_LOG_WRN("%d status write fails performing the swap", boot_status_fails);
1186 }
1187#endif
1188
Christopher Collins92ea77f2016-12-12 15:59:26 -08001189 return 0;
1190}
David Brown17609d82017-05-05 09:41:34 -06001191#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001192
1193/**
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001194 * Marks the image in slot 0 as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001195 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001196#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001197static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001198boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001199{
1200 const struct flash_area *fap;
1201 int rc;
1202
1203 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1204 if (rc != 0) {
1205 return BOOT_EFLASH;
1206 }
1207
1208 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001209 flash_area_close(fap);
1210 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001211}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001212#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001213
1214/**
1215 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1216 * the status bytes from the image revert operation don't get processed on a
1217 * subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001218 *
1219 * NOTE: image_ok is tested before writing because if there's a valid permanent
1220 * image installed on slot0 and the new image to be upgrade to has a bad sig,
1221 * image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001222 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001223#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001224static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001225boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001226{
1227 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001228 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001229 int rc;
1230
1231 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1232 if (rc != 0) {
1233 return BOOT_EFLASH;
1234 }
1235
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001236 rc = boot_read_swap_state(fap, &state);
1237 if (rc != 0) {
1238 rc = BOOT_EFLASH;
1239 goto out;
1240 }
1241
1242 if (state.image_ok == BOOT_FLAG_UNSET) {
1243 rc = boot_write_image_ok(fap);
1244 }
1245
1246out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001247 flash_area_close(fap);
1248 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001249}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001250#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001251
1252/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001253 * Performs an image swap if one is required.
1254 *
1255 * @param out_swap_type On success, the type of swap performed gets
1256 * written here.
1257 *
1258 * @return 0 on success; nonzero on failure.
1259 */
1260static int
1261boot_swap_if_needed(int *out_swap_type)
1262{
1263 struct boot_status bs;
1264 int swap_type;
1265 int rc;
1266
1267 /* Determine if we rebooted in the middle of an image swap
1268 * operation.
1269 */
1270 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001271 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001272 if (rc != 0) {
1273 return rc;
1274 }
1275
1276 /* If a partial swap was detected, complete it. */
1277 if (bs.idx != 0 || bs.state != 0) {
1278 rc = boot_copy_image(&bs);
1279 assert(rc == 0);
1280
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001281 /* NOTE: here we have finished a swap resume. The initial request
1282 * was either a TEST or PERM swap, which now after the completed
1283 * swap will be determined to be respectively REVERT (was TEST)
1284 * or NONE (was PERM).
1285 */
1286
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001287 /* Extrapolate the type of the partial swap. We need this
1288 * information to know how to mark the swap complete in flash.
1289 */
1290 swap_type = boot_previous_swap_type();
1291 } else {
1292 swap_type = boot_validated_swap_type();
1293 switch (swap_type) {
1294 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001295 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001296 case BOOT_SWAP_TYPE_REVERT:
1297 rc = boot_copy_image(&bs);
1298 assert(rc == 0);
1299 break;
1300 }
1301 }
1302
1303 *out_swap_type = swap_type;
1304 return 0;
1305}
1306
1307/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001308 * Prepares the booting process. This function moves images around in flash as
1309 * appropriate, and tells you what address to boot from.
1310 *
1311 * @param rsp On success, indicates how booting should occur.
1312 *
1313 * @return 0 on success; nonzero on failure.
1314 */
1315int
1316boot_go(struct boot_rsp *rsp)
1317{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001318 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001319 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001320 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001321 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001322 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001323
1324 /* The array of slot sectors are defined here (as opposed to file scope) so
1325 * that they don't get allocated for non-boot-loader apps. This is
1326 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001327 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001328 */
Marti Bolivarc50926f2017-06-14 09:35:40 -04001329 static boot_sector_t slot0_sectors[BOOT_MAX_IMG_SECTORS];
1330 static boot_sector_t slot1_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001331 boot_data.imgs[0].sectors = slot0_sectors;
1332 boot_data.imgs[1].sectors = slot1_sectors;
1333
Marti Bolivarc0b47912017-06-13 17:18:09 -04001334 /* Open boot_data image areas for the duration of this call. */
1335 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1336 fa_id = flash_area_id_from_image_slot(slot);
1337 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1338 assert(rc == 0);
1339 }
1340 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1341 &BOOT_SCRATCH_AREA(&boot_data));
1342 assert(rc == 0);
1343
Christopher Collins92ea77f2016-12-12 15:59:26 -08001344 /* Determine the sector layout of the image slots and scratch area. */
1345 rc = boot_read_sectors();
1346 if (rc != 0) {
Fabio Utziga1fae672018-03-30 10:52:38 -03001347 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1348 BOOT_MAX_IMG_SECTORS);
Marti Bolivarc0b47912017-06-13 17:18:09 -04001349 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001350 }
1351
1352 /* Attempt to read an image header from each slot. */
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001353 rc = boot_read_image_headers(false);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001354 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001355 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001356 }
1357
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001358 /* If the image slots aren't compatible, no swap is possible. Just boot
1359 * into slot 0.
1360 */
1361 if (boot_slots_compatible()) {
1362 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001363 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001364 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001365 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001366 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001367
1368 /*
1369 * The following states need image_ok be explicitly set after the
1370 * swap was finished to avoid a new revert.
1371 */
1372 if (swap_type == BOOT_SWAP_TYPE_REVERT || swap_type == BOOT_SWAP_TYPE_FAIL) {
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001373#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001374 rc = boot_set_image_ok();
1375 if (rc != 0) {
1376 swap_type = BOOT_SWAP_TYPE_PANIC;
1377 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001378#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001379 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001380 } else {
1381 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001382 }
1383
1384 switch (swap_type) {
1385 case BOOT_SWAP_TYPE_NONE:
1386 slot = 0;
1387 break;
1388
Fabio Utzig695d5642017-07-20 09:47:16 -03001389 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1390 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001391 case BOOT_SWAP_TYPE_REVERT:
1392 slot = 1;
David Brown52eee562017-07-05 11:25:09 -06001393 reload_headers = true;
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001394#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001395 rc = boot_set_copy_done();
1396 if (rc != 0) {
1397 swap_type = BOOT_SWAP_TYPE_PANIC;
1398 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001399#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001400 break;
1401
1402 case BOOT_SWAP_TYPE_FAIL:
1403 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1404 * try to boot into it again on the next reboot. Do this by pretending
1405 * we just reverted back to slot 0.
1406 */
1407 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001408 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001409 break;
1410
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001411 default:
Fabio Utzig695d5642017-07-20 09:47:16 -03001412 swap_type = BOOT_SWAP_TYPE_PANIC;
1413 }
1414
1415 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1416 BOOT_LOG_ERR("panic!");
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001417 assert(0);
Fabio Utzig695d5642017-07-20 09:47:16 -03001418
1419 /* Loop forever... */
1420 while (1) {}
Christopher Collins92ea77f2016-12-12 15:59:26 -08001421 }
1422
David Brown52eee562017-07-05 11:25:09 -06001423 if (reload_headers) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001424 rc = boot_read_image_headers(false);
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001425 if (rc != 0) {
1426 goto out;
1427 }
1428 /* Since headers were reloaded, it can be assumed we just performed a
1429 * swap or overwrite. Now the header info that should be used to
1430 * provide the data for the bootstrap, which previously was at Slot 1,
1431 * was updated to Slot 0.
1432 */
1433 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001434 }
1435
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001436#ifdef MCUBOOT_VALIDATE_SLOT0
David Brown554c52e2017-06-30 16:01:07 -06001437 rc = boot_validate_slot(0);
Fabio Utzig57c40f72017-12-12 21:48:30 -02001438 ASSERT(rc == 0);
David Brown554c52e2017-06-30 16:01:07 -06001439 if (rc != 0) {
1440 rc = BOOT_EBADIMAGE;
1441 goto out;
1442 }
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001443#else
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001444 /* Even if we're not re-validating slot 0, we could be booting
1445 * onto an empty flash chip. At least do a basic sanity check that
1446 * the magic number on the image is OK.
1447 */
1448 if (boot_data.imgs[0].hdr.ih_magic != IMAGE_MAGIC) {
Fabio Utzig67716012018-02-26 10:36:15 -03001449 BOOT_LOG_ERR("bad image magic 0x%lx", (unsigned long)boot_data.imgs[0].hdr.ih_magic);
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001450 rc = BOOT_EBADIMAGE;
1451 goto out;
1452 }
David Brown554c52e2017-06-30 16:01:07 -06001453#endif
1454
Christopher Collins92ea77f2016-12-12 15:59:26 -08001455 /* Always boot from the primary slot. */
Marti Bolivar428cdbf2017-05-01 22:32:42 -04001456 rsp->br_flash_dev_id = boot_img_fa_device_id(&boot_data, 0);
Marti Bolivar88f48d92017-05-01 22:30:02 -04001457 rsp->br_image_off = boot_img_slot_off(&boot_data, 0);
Marti Bolivarf804f622017-06-12 15:41:48 -04001458 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001459
Marti Bolivarc0b47912017-06-13 17:18:09 -04001460 out:
1461 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1462 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1463 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1464 }
1465 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001466}
1467
1468int
1469split_go(int loader_slot, int split_slot, void **entry)
1470{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001471 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001472 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001473 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001474 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001475 int rc;
1476
Christopher Collins92ea77f2016-12-12 15:59:26 -08001477 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1478 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001479 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001480 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001481 boot_data.imgs[loader_slot].sectors = sectors + 0;
1482 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1483
1484 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1485 rc = flash_area_open(loader_flash_id,
1486 &BOOT_IMG_AREA(&boot_data, split_slot));
1487 assert(rc == 0);
1488 split_flash_id = flash_area_id_from_image_slot(split_slot);
1489 rc = flash_area_open(split_flash_id,
1490 &BOOT_IMG_AREA(&boot_data, split_slot));
1491 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001492
1493 /* Determine the sector layout of the image slots and scratch area. */
1494 rc = boot_read_sectors();
1495 if (rc != 0) {
1496 rc = SPLIT_GO_ERR;
1497 goto done;
1498 }
1499
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001500 rc = boot_read_image_headers(true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001501 if (rc != 0) {
1502 goto done;
1503 }
1504
Christopher Collins92ea77f2016-12-12 15:59:26 -08001505 /* Don't check the bootable image flag because we could really call a
1506 * bootable or non-bootable image. Just validate that the image check
1507 * passes which is distinct from the normal check.
1508 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001509 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001510 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001511 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001512 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001513 if (rc != 0) {
1514 rc = SPLIT_GO_NON_MATCHING;
1515 goto done;
1516 }
1517
Marti Bolivarea088872017-06-12 17:10:49 -04001518 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001519 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001520 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001521 rc = SPLIT_GO_OK;
1522
1523done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001524 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1525 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001526 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001527 return rc;
1528}