blob: 75494f683498295a2ab8580f3b1df87127fb8495 [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 Utziga0e1cce2017-11-23 20:04:01 -020046#ifdef MCUBOOT_VALIDATE_SLOT0
47static int boot_status_fails = 0;
48#define BOOT_STATUS_ASSERT(x) \
49 do { \
50 if (x) { \
51 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
288boot_read_image_headers(void)
289{
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 Utzig7ebb7c22017-04-26 10:59:31 -0300296 /* If at least the first slot's header was read successfully, then
297 * the boot loader can attempt a boot. Failure to read any headers
298 * is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800299 */
300 if (i > 0) {
301 return 0;
302 } else {
303 return rc;
304 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800305 }
306 }
307
308 return 0;
309}
310
311static uint8_t
312boot_write_sz(void)
313{
314 uint8_t elem_sz;
315 uint8_t align;
316
317 /* Figure out what size to write update status update as. The size depends
318 * on what the minimum write size is for scratch area, active image slot.
319 * We need to use the bigger of those 2 values.
320 */
Marti Bolivare2587152017-06-12 15:52:05 -0400321 elem_sz = hal_flash_align(boot_img_fa_device_id(&boot_data, 0));
322 align = hal_flash_align(boot_scratch_fa_device_id(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800323 if (align > elem_sz) {
324 elem_sz = align;
325 }
326
327 return elem_sz;
328}
329
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800330static int
331boot_slots_compatible(void)
332{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400333 size_t num_sectors_0 = boot_img_num_sectors(&boot_data, 0);
334 size_t num_sectors_1 = boot_img_num_sectors(&boot_data, 1);
335 size_t size_0, size_1;
336 size_t i;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800337
338 /* Ensure both image slots have identical sector layouts. */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400339 if (num_sectors_0 != num_sectors_1) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800340 return 0;
341 }
Marti Bolivard3269fd2017-06-12 16:31:12 -0400342 for (i = 0; i < num_sectors_0; i++) {
343 size_0 = boot_img_sector_size(&boot_data, 0, i);
344 size_1 = boot_img_sector_size(&boot_data, 1, i);
345 if (size_0 != size_1) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800346 return 0;
347 }
348 }
349
350 return 1;
351}
352
Christopher Collins92ea77f2016-12-12 15:59:26 -0800353/**
354 * Determines the sector layout of both image slots and the scratch area.
355 * This information is necessary for calculating the number of bytes to erase
356 * and copy during an image swap. The information collected during this
357 * function is used to populate the boot_data global.
358 */
359static int
360boot_read_sectors(void)
361{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800362 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800363
Marti Bolivarcca28a92017-06-12 16:52:22 -0400364 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800365 if (rc != 0) {
366 return BOOT_EFLASH;
367 }
368
Marti Bolivarcca28a92017-06-12 16:52:22 -0400369 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800370 if (rc != 0) {
371 return BOOT_EFLASH;
372 }
373
Marti Bolivare10a7392017-06-14 16:20:07 -0400374 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800375
376 return 0;
377}
378
379static uint32_t
380boot_status_internal_off(int idx, int state, int elem_sz)
381{
382 int idx_sz;
383
384 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
385
386 return idx * idx_sz + state * elem_sz;
387}
388
389/**
390 * Reads the status of a partially-completed swap, if any. This is necessary
391 * to recover in case the boot lodaer was reset in the middle of a swap
392 * operation.
393 */
394static int
395boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
396{
397 uint32_t off;
398 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300399 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800400 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200401 int found_idx;
402 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800403 int rc;
404 int i;
405
406 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400407 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300408
Christopher Collins92ea77f2016-12-12 15:59:26 -0800409 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200410 found_idx = 0;
411 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300412 for (i = 0; i < max_entries; i++) {
Marti Bolivare10a7392017-06-14 16:20:07 -0400413 rc = flash_area_read(fap, off + i * BOOT_WRITE_SZ(&boot_data),
414 &status, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800415 if (rc != 0) {
416 return BOOT_EFLASH;
417 }
418
419 if (status == 0xff) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200420 if (found && !found_idx) {
421 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800422 }
423 } else if (!found) {
424 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200425 } else if (found_idx) {
426 invalid = 1;
427 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800428 }
429 }
430
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200431 if (invalid) {
432 /* This means there was an error writing status on the last
433 * swap. Tell user and move on to validation!
434 */
435 BOOT_LOG_ERR("Detected inconsistent status!");
436
437#if !defined(MCUBOOT_VALIDATE_SLOT0)
438 /* With validation of slot0 disabled, there is no way to be sure the
439 * swapped slot0 is OK, so abort!
440 */
441 assert(0);
442#endif
443 }
444
Christopher Collins92ea77f2016-12-12 15:59:26 -0800445 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200446 if (!found_idx) {
447 found_idx = i;
448 }
449 found_idx--;
450 bs->idx = found_idx / BOOT_STATUS_STATE_COUNT;
451 bs->state = found_idx % BOOT_STATUS_STATE_COUNT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800452 }
453
454 return 0;
455}
456
457/**
458 * Reads the boot status from the flash. The boot status contains
459 * the current state of an interrupted image copy operation. If the boot
460 * status is not present, or it indicates that previous copy finished,
461 * there is no operation in progress.
462 */
463static int
464boot_read_status(struct boot_status *bs)
465{
466 const struct flash_area *fap;
467 int status_loc;
468 int area_id;
469 int rc;
470
471 memset(bs, 0, sizeof *bs);
472
473 status_loc = boot_status_source();
474 switch (status_loc) {
475 case BOOT_STATUS_SOURCE_NONE:
476 return 0;
477
478 case BOOT_STATUS_SOURCE_SCRATCH:
479 area_id = FLASH_AREA_IMAGE_SCRATCH;
480 break;
481
482 case BOOT_STATUS_SOURCE_SLOT0:
483 area_id = FLASH_AREA_IMAGE_0;
484 break;
485
486 default:
487 assert(0);
488 return BOOT_EBADARGS;
489 }
490
491 rc = flash_area_open(area_id, &fap);
492 if (rc != 0) {
493 return BOOT_EFLASH;
494 }
495
Fabio Utzig46490722017-09-04 15:34:32 -0300496 rc = boot_read_status_bytes(fap, bs);
497
498 flash_area_close(fap);
499 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800500}
501
502/**
503 * Writes the supplied boot status to the flash file system. The boot status
504 * contains the current state of an in-progress image copy operation.
505 *
506 * @param bs The boot status to write.
507 *
508 * @return 0 on success; nonzero on failure.
509 */
510int
511boot_write_status(struct boot_status *bs)
512{
513 const struct flash_area *fap;
514 uint32_t off;
515 int area_id;
516 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300517 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700518 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800519
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300520 /* NOTE: The first sector copied (that is the last sector on slot) contains
521 * the trailer. Since in the last step SLOT 0 is erased, the first
522 * two status writes go to the scratch which will be copied to SLOT 0!
523 */
524
Fabio Utzig2473ac02017-05-02 12:45:02 -0300525 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800526 /* Write to scratch. */
527 area_id = FLASH_AREA_IMAGE_SCRATCH;
528 } else {
529 /* Write to slot 0. */
530 area_id = FLASH_AREA_IMAGE_0;
531 }
532
533 rc = flash_area_open(area_id, &fap);
534 if (rc != 0) {
535 rc = BOOT_EFLASH;
536 goto done;
537 }
538
539 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400540 boot_status_internal_off(bs->idx, bs->state,
541 BOOT_WRITE_SZ(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800542
David Brown9d725462017-01-23 15:50:58 -0700543 align = hal_flash_align(fap->fa_device_id);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300544 memset(buf, 0xFF, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700545 buf[0] = bs->state;
546
547 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800548 if (rc != 0) {
549 rc = BOOT_EFLASH;
550 goto done;
551 }
552
553 rc = 0;
554
555done:
556 flash_area_close(fap);
557 return rc;
558}
559
560/*
561 * Validate image hash/signature in a slot.
562 */
563static int
564boot_image_check(struct image_header *hdr, const struct flash_area *fap)
565{
David Browndb1d9d32017-01-06 11:07:54 -0700566 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800567
Christopher Collins92ea77f2016-12-12 15:59:26 -0800568 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
569 NULL, 0, NULL)) {
570 return BOOT_EBADIMAGE;
571 }
572 return 0;
573}
574
575static int
576split_image_check(struct image_header *app_hdr,
577 const struct flash_area *app_fap,
578 struct image_header *loader_hdr,
579 const struct flash_area *loader_fap)
580{
581 static void *tmpbuf;
582 uint8_t loader_hash[32];
583
584 if (!tmpbuf) {
585 tmpbuf = malloc(BOOT_TMPBUF_SZ);
586 if (!tmpbuf) {
587 return BOOT_ENOMEM;
588 }
589 }
590
591 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
592 NULL, 0, loader_hash)) {
593 return BOOT_EBADIMAGE;
594 }
595
596 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
597 loader_hash, 32, NULL)) {
598 return BOOT_EBADIMAGE;
599 }
600
601 return 0;
602}
603
604static int
David Brownd930ec62016-12-14 07:59:48 -0700605boot_validate_slot(int slot)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800606{
607 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400608 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800609 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300610
Marti Bolivarf804f622017-06-12 15:41:48 -0400611 hdr = boot_img_hdr(&boot_data, slot);
612 if (hdr->ih_magic == 0xffffffff || hdr->ih_flags & IMAGE_F_NON_BOOTABLE) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300613 /* No bootable image in slot; continue booting from slot 0. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800614 return -1;
615 }
616
David Brownd930ec62016-12-14 07:59:48 -0700617 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800618 if (rc != 0) {
619 return BOOT_EFLASH;
620 }
621
Marti Bolivarf804f622017-06-12 15:41:48 -0400622 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap) != 0)) {
David Brownb38e0442017-02-24 13:57:12 -0700623 if (slot != 0) {
624 flash_area_erase(fap, 0, fap->fa_size);
625 /* Image in slot 1 is invalid. Erase the image and
626 * continue booting from slot 0.
627 */
628 }
Fabio Utzigb6297af2017-10-05 18:26:36 -0300629 BOOT_LOG_ERR("Image in slot %d is not valid!", slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800630 return -1;
631 }
632
633 flash_area_close(fap);
634
635 /* Image in slot 1 is valid. */
636 return 0;
637}
638
639/**
640 * Determines which swap operation to perform, if any. If it is determined
641 * that a swap operation is required, the image in the second slot is checked
642 * for validity. If the image in the second slot is invalid, it is erased, and
643 * a swap type of "none" is indicated.
644 *
645 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
646 */
647static int
648boot_validated_swap_type(void)
649{
650 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800651
652 swap_type = boot_swap_type();
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300653 switch (swap_type) {
654 case BOOT_SWAP_TYPE_TEST:
655 case BOOT_SWAP_TYPE_PERM:
656 case BOOT_SWAP_TYPE_REVERT:
657 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
658 if (boot_validate_slot(1) != 0) {
659 swap_type = BOOT_SWAP_TYPE_FAIL;
660 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800661 }
662
663 return swap_type;
664}
665
666/**
667 * Calculates the number of sectors the scratch area can contain. A "last"
668 * source sector is specified because images are copied backwards in flash
669 * (final index to index number 0).
670 *
671 * @param last_sector_idx The index of the last source sector
672 * (inclusive).
673 * @param out_first_sector_idx The index of the first source sector
674 * (inclusive) gets written here.
675 *
676 * @return The number of bytes comprised by the
677 * [first-sector, last-sector] range.
678 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300679#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800680static uint32_t
681boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
682{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400683 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800684 uint32_t new_sz;
685 uint32_t sz;
686 int i;
687
688 sz = 0;
689
Marti Bolivard3269fd2017-06-12 16:31:12 -0400690 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800691 for (i = last_sector_idx; i >= 0; i--) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400692 new_sz = sz + boot_img_sector_size(&boot_data, 0, i);
693 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800694 break;
695 }
696 sz = new_sz;
697 }
698
699 /* i currently refers to a sector that doesn't fit or it is -1 because all
700 * sectors have been processed. In both cases, exclude sector i.
701 */
702 *out_first_sector_idx = i + 1;
703 return sz;
704}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300705#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800706
707/**
708 * Erases a region of flash.
709 *
710 * @param flash_area_idx The ID of the flash area containing the region
711 * to erase.
712 * @param off The offset within the flash area to start the
713 * erase.
714 * @param sz The number of bytes to erase.
715 *
716 * @return 0 on success; nonzero on failure.
717 */
718static int
719boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
720{
721 const struct flash_area *fap;
722 int rc;
723
724 rc = flash_area_open(flash_area_id, &fap);
725 if (rc != 0) {
726 rc = BOOT_EFLASH;
727 goto done;
728 }
729
730 rc = flash_area_erase(fap, off, sz);
731 if (rc != 0) {
732 rc = BOOT_EFLASH;
733 goto done;
734 }
735
736 rc = 0;
737
738done:
739 flash_area_close(fap);
740 return rc;
741}
742
743/**
744 * Copies the contents of one flash region to another. You must erase the
745 * destination region prior to calling this function.
746 *
747 * @param flash_area_id_src The ID of the source flash area.
748 * @param flash_area_id_dst The ID of the destination flash area.
749 * @param off_src The offset within the source flash area to
750 * copy from.
751 * @param off_dst The offset within the destination flash area to
752 * copy to.
753 * @param sz The number of bytes to copy.
754 *
755 * @return 0 on success; nonzero on failure.
756 */
757static int
758boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
759 uint32_t off_src, uint32_t off_dst, uint32_t sz)
760{
761 const struct flash_area *fap_src;
762 const struct flash_area *fap_dst;
763 uint32_t bytes_copied;
764 int chunk_sz;
765 int rc;
766
767 static uint8_t buf[1024];
768
769 fap_src = NULL;
770 fap_dst = NULL;
771
772 rc = flash_area_open(flash_area_id_src, &fap_src);
773 if (rc != 0) {
774 rc = BOOT_EFLASH;
775 goto done;
776 }
777
778 rc = flash_area_open(flash_area_id_dst, &fap_dst);
779 if (rc != 0) {
780 rc = BOOT_EFLASH;
781 goto done;
782 }
783
784 bytes_copied = 0;
785 while (bytes_copied < sz) {
786 if (sz - bytes_copied > sizeof buf) {
787 chunk_sz = sizeof buf;
788 } else {
789 chunk_sz = sz - bytes_copied;
790 }
791
792 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
793 if (rc != 0) {
794 rc = BOOT_EFLASH;
795 goto done;
796 }
797
798 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
799 if (rc != 0) {
800 rc = BOOT_EFLASH;
801 goto done;
802 }
803
804 bytes_copied += chunk_sz;
805 }
806
807 rc = 0;
808
809done:
Fabio Utzige7686262017-06-28 09:26:54 -0300810 if (fap_src) {
811 flash_area_close(fap_src);
812 }
813 if (fap_dst) {
814 flash_area_close(fap_dst);
815 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800816 return rc;
817}
818
David Brown6b1b3b92017-09-19 08:59:10 -0600819#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300820static inline int
Fabio Utzig46490722017-09-04 15:34:32 -0300821boot_status_init_by_id(int flash_area_id, const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300822{
823 const struct flash_area *fap;
824 struct boot_swap_state swap_state;
825 int rc;
826
827 rc = flash_area_open(flash_area_id, &fap);
828 assert(rc == 0);
829
830 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
831 assert(rc == 0);
832
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400833 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300834 rc = boot_write_image_ok(fap);
835 assert(rc == 0);
836 }
837
Fabio Utzig46490722017-09-04 15:34:32 -0300838 rc = boot_write_swap_size(fap, bs->swap_size);
839 assert(rc == 0);
840
Fabio Utzig2473ac02017-05-02 12:45:02 -0300841 rc = boot_write_magic(fap);
842 assert(rc == 0);
843
844 flash_area_close(fap);
845
846 return 0;
847}
David Brown6b1b3b92017-09-19 08:59:10 -0600848#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -0300849
Fabio Utzig358c9352017-07-25 22:10:45 -0300850#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300851static int
852boot_erase_last_sector_by_id(int flash_area_id)
853{
854 uint8_t slot;
855 uint32_t last_sector;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300856 int rc;
857
858 switch (flash_area_id) {
859 case FLASH_AREA_IMAGE_0:
860 slot = 0;
861 break;
862 case FLASH_AREA_IMAGE_1:
863 slot = 1;
864 break;
865 default:
866 return BOOT_EFLASH;
867 }
868
Marti Bolivard3269fd2017-06-12 16:31:12 -0400869 last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300870 rc = boot_erase_sector(flash_area_id,
Marti Bolivarea088872017-06-12 17:10:49 -0400871 boot_img_sector_off(&boot_data, slot, last_sector),
Marti Bolivard3269fd2017-06-12 16:31:12 -0400872 boot_img_sector_size(&boot_data, slot, last_sector));
Fabio Utzig2473ac02017-05-02 12:45:02 -0300873 assert(rc == 0);
874
875 return rc;
876}
Fabio Utzig358c9352017-07-25 22:10:45 -0300877#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -0300878
Christopher Collins92ea77f2016-12-12 15:59:26 -0800879/**
880 * Swaps the contents of two flash regions within the two image slots.
881 *
882 * @param idx The index of the first sector in the range of
883 * sectors being swapped.
884 * @param sz The number of bytes to swap.
885 * @param bs The current boot status. This struct gets
886 * updated according to the outcome.
887 *
888 * @return 0 on success; nonzero on failure.
889 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300890#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800891static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800892boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
893{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300894 const struct flash_area *fap;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800895 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300896 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800897 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300898 uint32_t scratch_trailer_off;
899 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400900 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800901 int rc;
902
903 /* Calculate offset from start of image area. */
Marti Bolivarea088872017-06-12 17:10:49 -0400904 img_off = boot_img_sector_off(&boot_data, 0, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800905
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300906 copy_sz = sz;
Marti Bolivare10a7392017-06-14 16:20:07 -0400907 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -0400908
909 /* sz in this function is always is always sized on a multiple of the
Marti Bolivarea088872017-06-12 17:10:49 -0400910 * sector size. The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -0400911 * is to determine if we're swapping the last sector. The last sector
912 * needs special handling because it's where the trailer lives. If we're
913 * copying it, we need to use scratch to write the trailer temporarily.
914 *
915 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
916 * controls if special handling is needed (swapping last sector).
917 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400918 last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
Marti Bolivarea088872017-06-12 17:10:49 -0400919 if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300920 copy_sz -= trailer_sz;
921 }
922
Fabio Utzig2473ac02017-05-02 12:45:02 -0300923 bs->use_scratch = (bs->idx == 0 && copy_sz != sz);
924
Christopher Collins92ea77f2016-12-12 15:59:26 -0800925 if (bs->state == 0) {
926 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800927 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800928
929 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300930 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800931 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800932
Fabio Utzig2473ac02017-05-02 12:45:02 -0300933 if (bs->idx == 0) {
934 if (bs->use_scratch) {
Fabio Utzig46490722017-09-04 15:34:32 -0300935 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH, bs);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300936 } else {
937 /* Prepare the status area... here it is known that the
938 * last sector is not being used by the image data so it's
939 * safe to erase.
940 */
941 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300942 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300943
Fabio Utzig46490722017-09-04 15:34:32 -0300944 boot_status_init_by_id(FLASH_AREA_IMAGE_0, bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300945 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300946 }
947
Christopher Collins92ea77f2016-12-12 15:59:26 -0800948 bs->state = 1;
Christopher Collins4772ac42017-02-27 20:08:01 -0800949 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200950 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800951 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300952
Christopher Collins92ea77f2016-12-12 15:59:26 -0800953 if (bs->state == 1) {
954 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800955 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800956
Christopher Collins92ea77f2016-12-12 15:59:26 -0800957 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
958 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800959 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800960
Fabio Utzig2473ac02017-05-02 12:45:02 -0300961 if (bs->idx == 0 && !bs->use_scratch) {
962 /* If not all sectors of the slot are being swapped,
963 * guarantee here that only slot0 will have the state.
964 */
965 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1);
966 assert(rc == 0);
967 }
968
Christopher Collins92ea77f2016-12-12 15:59:26 -0800969 bs->state = 2;
Christopher Collins4772ac42017-02-27 20:08:01 -0800970 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200971 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800972 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300973
Christopher Collins92ea77f2016-12-12 15:59:26 -0800974 if (bs->state == 2) {
975 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800976 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800977
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300978 /* NOTE: also copy trailer from scratch (has status info) */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800979 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300980 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800981 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800982
Fabio Utzig94d998c2017-05-22 11:02:41 -0400983 if (bs->use_scratch) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300984 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
985 assert(rc == 0);
986
987 scratch_trailer_off = boot_status_off(fap);
988
989 flash_area_close(fap);
990
991 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
992 assert(rc == 0);
993
994 /* copy current status that is being maintained in scratch */
995 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Marti Bolivare10a7392017-06-14 16:20:07 -0400996 scratch_trailer_off,
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300997 img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -0400998 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200999 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001000
Fabio Utzig2473ac02017-05-02 12:45:02 -03001001 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1002 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001003 assert(rc == 0);
1004
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001005 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001006 rc = boot_write_image_ok(fap);
1007 assert(rc == 0);
1008 }
1009
Fabio Utzig46490722017-09-04 15:34:32 -03001010 rc = boot_write_swap_size(fap, bs->swap_size);
1011 assert(rc == 0);
1012
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001013 rc = boot_write_magic(fap);
1014 assert(rc == 0);
1015
1016 flash_area_close(fap);
1017 }
1018
Christopher Collins92ea77f2016-12-12 15:59:26 -08001019 bs->idx++;
1020 bs->state = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001021 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -08001022 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001023 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001024 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001025}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001026#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001027
1028/**
1029 * Swaps the two images in flash. If a prior copy operation was interrupted
1030 * by a system reset, this function completes that operation.
1031 *
1032 * @param bs The current boot status. This function reads
1033 * this struct to determine if it is resuming
1034 * an interrupted swap operation. This
1035 * function writes the updated status to this
1036 * function on return.
1037 *
1038 * @return 0 on success; nonzero on failure.
1039 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001040#ifdef MCUBOOT_OVERWRITE_ONLY
David Brown17609d82017-05-05 09:41:34 -06001041static int
1042boot_copy_image(struct boot_status *bs)
1043{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001044 size_t sect_count;
1045 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001046 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001047 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001048 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001049 size_t last_sector;
1050
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001051 (void)bs;
1052
Fabio Utzig13d9e352017-10-05 20:32:31 -03001053#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1054 uint32_t src_size = 0;
1055 rc = boot_read_image_size(1, boot_img_hdr(&boot_data, 1), &src_size);
1056 assert(rc == 0);
1057#endif
David Brown17609d82017-05-05 09:41:34 -06001058
1059 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
1060 BOOT_LOG_INF("Erasing slot0");
1061
Marti Bolivard3269fd2017-06-12 16:31:12 -04001062 sect_count = boot_img_num_sectors(&boot_data, 0);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001063 for (sect = 0, size = 0; sect < sect_count; sect++) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001064 this_size = boot_img_sector_size(&boot_data, 0, sect);
David Brown17609d82017-05-05 09:41:34 -06001065 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
1066 size,
1067 this_size);
1068 assert(rc == 0);
1069
1070 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001071
1072#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1073 if (size >= src_size) {
1074 break;
1075 }
1076#endif
David Brown17609d82017-05-05 09:41:34 -06001077 }
1078
Fabio Utzig6a537ee2017-09-13 17:31:44 -03001079 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%lx bytes", size);
David Brown17609d82017-05-05 09:41:34 -06001080 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
1081 0, 0, size);
1082
Fabio Utzig13d9e352017-10-05 20:32:31 -03001083 /*
1084 * Erases header and trailer. The trailer is erased because when a new
1085 * image is written without a trailer as is the case when using newt, the
1086 * trailer that was left might trigger a new upgrade.
1087 */
David Brown17609d82017-05-05 09:41:34 -06001088 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
Fabio Utzig13d9e352017-10-05 20:32:31 -03001089 boot_img_sector_off(&boot_data, 1, 0),
1090 boot_img_sector_size(&boot_data, 1, 0));
David Brown17609d82017-05-05 09:41:34 -06001091 assert(rc == 0);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001092 last_sector = boot_img_num_sectors(&boot_data, 1) - 1;
1093 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
1094 boot_img_sector_off(&boot_data, 1, last_sector),
1095 boot_img_sector_size(&boot_data, 1, last_sector));
1096 assert(rc == 0);
1097
1098 /* TODO: Perhaps verify slot 0's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001099
1100 return 0;
1101}
1102#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001103static int
1104boot_copy_image(struct boot_status *bs)
1105{
1106 uint32_t sz;
1107 int first_sector_idx;
1108 int last_sector_idx;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001109 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001110 struct image_header *hdr;
1111 uint32_t size;
1112 uint32_t copy_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001113 int rc;
1114
1115 /* FIXME: just do this if asked by user? */
1116
1117 size = copy_size = 0;
1118
Fabio Utzig46490722017-09-04 15:34:32 -03001119 if (bs->idx == 0 && bs->state == 0) {
1120 /*
1121 * No swap ever happened, so need to find the largest image which
1122 * will be used to determine the amount of sectors to swap.
1123 */
1124 hdr = boot_img_hdr(&boot_data, 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001125 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzig46490722017-09-04 15:34:32 -03001126 rc = boot_read_image_size(0, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001127 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001128 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001129
Fabio Utzig46490722017-09-04 15:34:32 -03001130 hdr = boot_img_hdr(&boot_data, 1);
1131 if (hdr->ih_magic == IMAGE_MAGIC) {
1132 rc = boot_read_image_size(1, hdr, &size);
1133 assert(rc == 0);
1134 }
1135
1136 if (size > copy_size) {
1137 copy_size = size;
1138 }
1139
1140 bs->swap_size = copy_size;
1141 } else {
1142 /*
1143 * If a swap was under way, the swap_size should already be present
1144 * in the trailer...
1145 */
1146 rc = boot_read_swap_size(&bs->swap_size);
1147 assert(rc == 0);
1148
1149 copy_size = bs->swap_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001150 }
1151
1152 size = 0;
1153 last_sector_idx = 0;
1154 while (1) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001155 size += boot_img_sector_size(&boot_data, 0, last_sector_idx);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001156 if (size >= copy_size) {
1157 break;
1158 }
1159 last_sector_idx++;
1160 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001161
1162 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001163 while (last_sector_idx >= 0) {
1164 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
1165 if (swap_idx >= bs->idx) {
1166 boot_swap_sectors(first_sector_idx, sz, bs);
1167 }
1168
1169 last_sector_idx = first_sector_idx - 1;
1170 swap_idx++;
1171 }
1172
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001173#ifdef MCUBOOT_VALIDATE_SLOT0
1174 if (boot_status_fails > 0) {
1175 BOOT_LOG_WRN("%d status write fails performing the swap", boot_status_fails);
1176 }
1177#endif
1178
Christopher Collins92ea77f2016-12-12 15:59:26 -08001179 return 0;
1180}
David Brown17609d82017-05-05 09:41:34 -06001181#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001182
1183/**
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001184 * Marks the image in slot 0 as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001185 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001186#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001187static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001188boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001189{
1190 const struct flash_area *fap;
1191 int rc;
1192
1193 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1194 if (rc != 0) {
1195 return BOOT_EFLASH;
1196 }
1197
1198 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001199 flash_area_close(fap);
1200 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001201}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001202#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001203
1204/**
1205 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1206 * the status bytes from the image revert operation don't get processed on a
1207 * subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001208 *
1209 * NOTE: image_ok is tested before writing because if there's a valid permanent
1210 * image installed on slot0 and the new image to be upgrade to has a bad sig,
1211 * image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001212 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001213#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001214static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001215boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001216{
1217 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001218 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001219 int rc;
1220
1221 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1222 if (rc != 0) {
1223 return BOOT_EFLASH;
1224 }
1225
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001226 rc = boot_read_swap_state(fap, &state);
1227 if (rc != 0) {
1228 rc = BOOT_EFLASH;
1229 goto out;
1230 }
1231
1232 if (state.image_ok == BOOT_FLAG_UNSET) {
1233 rc = boot_write_image_ok(fap);
1234 }
1235
1236out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001237 flash_area_close(fap);
1238 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001239}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001240#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001241
1242/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001243 * Performs an image swap if one is required.
1244 *
1245 * @param out_swap_type On success, the type of swap performed gets
1246 * written here.
1247 *
1248 * @return 0 on success; nonzero on failure.
1249 */
1250static int
1251boot_swap_if_needed(int *out_swap_type)
1252{
1253 struct boot_status bs;
1254 int swap_type;
1255 int rc;
1256
1257 /* Determine if we rebooted in the middle of an image swap
1258 * operation.
1259 */
1260 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001261 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001262 if (rc != 0) {
1263 return rc;
1264 }
1265
1266 /* If a partial swap was detected, complete it. */
1267 if (bs.idx != 0 || bs.state != 0) {
1268 rc = boot_copy_image(&bs);
1269 assert(rc == 0);
1270
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001271 /* NOTE: here we have finished a swap resume. The initial request
1272 * was either a TEST or PERM swap, which now after the completed
1273 * swap will be determined to be respectively REVERT (was TEST)
1274 * or NONE (was PERM).
1275 */
1276
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001277 /* Extrapolate the type of the partial swap. We need this
1278 * information to know how to mark the swap complete in flash.
1279 */
1280 swap_type = boot_previous_swap_type();
1281 } else {
1282 swap_type = boot_validated_swap_type();
1283 switch (swap_type) {
1284 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001285 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001286 case BOOT_SWAP_TYPE_REVERT:
1287 rc = boot_copy_image(&bs);
1288 assert(rc == 0);
1289 break;
1290 }
1291 }
1292
1293 *out_swap_type = swap_type;
1294 return 0;
1295}
1296
1297/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001298 * Prepares the booting process. This function moves images around in flash as
1299 * appropriate, and tells you what address to boot from.
1300 *
1301 * @param rsp On success, indicates how booting should occur.
1302 *
1303 * @return 0 on success; nonzero on failure.
1304 */
1305int
1306boot_go(struct boot_rsp *rsp)
1307{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001308 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001309 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001310 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001311 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001312 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001313
1314 /* The array of slot sectors are defined here (as opposed to file scope) so
1315 * that they don't get allocated for non-boot-loader apps. This is
1316 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001317 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001318 */
Marti Bolivarc50926f2017-06-14 09:35:40 -04001319 static boot_sector_t slot0_sectors[BOOT_MAX_IMG_SECTORS];
1320 static boot_sector_t slot1_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001321 boot_data.imgs[0].sectors = slot0_sectors;
1322 boot_data.imgs[1].sectors = slot1_sectors;
1323
Marti Bolivarc0b47912017-06-13 17:18:09 -04001324 /* Open boot_data image areas for the duration of this call. */
1325 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1326 fa_id = flash_area_id_from_image_slot(slot);
1327 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1328 assert(rc == 0);
1329 }
1330 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1331 &BOOT_SCRATCH_AREA(&boot_data));
1332 assert(rc == 0);
1333
Christopher Collins92ea77f2016-12-12 15:59:26 -08001334 /* Determine the sector layout of the image slots and scratch area. */
1335 rc = boot_read_sectors();
1336 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001337 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001338 }
1339
1340 /* Attempt to read an image header from each slot. */
1341 rc = boot_read_image_headers();
1342 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001343 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001344 }
1345
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001346 /* If the image slots aren't compatible, no swap is possible. Just boot
1347 * into slot 0.
1348 */
1349 if (boot_slots_compatible()) {
1350 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001351 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001352 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001353 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001354 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001355
1356 /*
1357 * The following states need image_ok be explicitly set after the
1358 * swap was finished to avoid a new revert.
1359 */
1360 if (swap_type == BOOT_SWAP_TYPE_REVERT || swap_type == BOOT_SWAP_TYPE_FAIL) {
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001361#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001362 rc = boot_set_image_ok();
1363 if (rc != 0) {
1364 swap_type = BOOT_SWAP_TYPE_PANIC;
1365 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001366#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001367 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001368 } else {
1369 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001370 }
1371
1372 switch (swap_type) {
1373 case BOOT_SWAP_TYPE_NONE:
1374 slot = 0;
1375 break;
1376
Fabio Utzig695d5642017-07-20 09:47:16 -03001377 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1378 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001379 case BOOT_SWAP_TYPE_REVERT:
1380 slot = 1;
David Brown52eee562017-07-05 11:25:09 -06001381 reload_headers = true;
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001382#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001383 rc = boot_set_copy_done();
1384 if (rc != 0) {
1385 swap_type = BOOT_SWAP_TYPE_PANIC;
1386 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001387#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001388 break;
1389
1390 case BOOT_SWAP_TYPE_FAIL:
1391 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1392 * try to boot into it again on the next reboot. Do this by pretending
1393 * we just reverted back to slot 0.
1394 */
1395 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001396 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001397 break;
1398
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001399 default:
Fabio Utzig695d5642017-07-20 09:47:16 -03001400 swap_type = BOOT_SWAP_TYPE_PANIC;
1401 }
1402
1403 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1404 BOOT_LOG_ERR("panic!");
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001405 assert(0);
Fabio Utzig695d5642017-07-20 09:47:16 -03001406
1407 /* Loop forever... */
1408 while (1) {}
Christopher Collins92ea77f2016-12-12 15:59:26 -08001409 }
1410
David Brown52eee562017-07-05 11:25:09 -06001411 if (reload_headers) {
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001412 rc = boot_read_image_headers();
1413 if (rc != 0) {
1414 goto out;
1415 }
1416 /* Since headers were reloaded, it can be assumed we just performed a
1417 * swap or overwrite. Now the header info that should be used to
1418 * provide the data for the bootstrap, which previously was at Slot 1,
1419 * was updated to Slot 0.
1420 */
1421 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001422 }
1423
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001424#ifdef MCUBOOT_VALIDATE_SLOT0
David Brown554c52e2017-06-30 16:01:07 -06001425 rc = boot_validate_slot(0);
Fabio Utzig57c40f72017-12-12 21:48:30 -02001426 ASSERT(rc == 0);
David Brown554c52e2017-06-30 16:01:07 -06001427 if (rc != 0) {
1428 rc = BOOT_EBADIMAGE;
1429 goto out;
1430 }
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001431#else
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001432 /* Even if we're not re-validating slot 0, we could be booting
1433 * onto an empty flash chip. At least do a basic sanity check that
1434 * the magic number on the image is OK.
1435 */
1436 if (boot_data.imgs[0].hdr.ih_magic != IMAGE_MAGIC) {
1437 BOOT_LOG_ERR("bad image magic 0x%x", boot_data.imgs[0].hdr.ih_magic);
1438 rc = BOOT_EBADIMAGE;
1439 goto out;
1440 }
David Brown554c52e2017-06-30 16:01:07 -06001441#endif
1442
Christopher Collins92ea77f2016-12-12 15:59:26 -08001443 /* Always boot from the primary slot. */
Marti Bolivar428cdbf2017-05-01 22:32:42 -04001444 rsp->br_flash_dev_id = boot_img_fa_device_id(&boot_data, 0);
Marti Bolivar88f48d92017-05-01 22:30:02 -04001445 rsp->br_image_off = boot_img_slot_off(&boot_data, 0);
Marti Bolivarf804f622017-06-12 15:41:48 -04001446 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001447
Marti Bolivarc0b47912017-06-13 17:18:09 -04001448 out:
1449 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1450 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1451 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1452 }
1453 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001454}
1455
1456int
1457split_go(int loader_slot, int split_slot, void **entry)
1458{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001459 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001460 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001461 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001462 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001463 int rc;
1464
Christopher Collins92ea77f2016-12-12 15:59:26 -08001465 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1466 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001467 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001468 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001469 boot_data.imgs[loader_slot].sectors = sectors + 0;
1470 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1471
1472 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1473 rc = flash_area_open(loader_flash_id,
1474 &BOOT_IMG_AREA(&boot_data, split_slot));
1475 assert(rc == 0);
1476 split_flash_id = flash_area_id_from_image_slot(split_slot);
1477 rc = flash_area_open(split_flash_id,
1478 &BOOT_IMG_AREA(&boot_data, split_slot));
1479 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001480
1481 /* Determine the sector layout of the image slots and scratch area. */
1482 rc = boot_read_sectors();
1483 if (rc != 0) {
1484 rc = SPLIT_GO_ERR;
1485 goto done;
1486 }
1487
1488 rc = boot_read_image_headers();
1489 if (rc != 0) {
1490 goto done;
1491 }
1492
Christopher Collins92ea77f2016-12-12 15:59:26 -08001493 /* Don't check the bootable image flag because we could really call a
1494 * bootable or non-bootable image. Just validate that the image check
1495 * passes which is distinct from the normal check.
1496 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001497 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001498 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001499 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001500 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001501 if (rc != 0) {
1502 rc = SPLIT_GO_NON_MATCHING;
1503 goto done;
1504 }
1505
Marti Bolivarea088872017-06-12 17:10:49 -04001506 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001507 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001508 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001509 rc = SPLIT_GO_OK;
1510
1511done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001512 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1513 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001514 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001515 return rc;
1516}