blob: fbbc8055aafd56a13322d18e336bfb7d9d594e51 [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
Fabio Utzig9b0ee902017-11-23 19:49:00 -020044#ifdef __BOOTSIM__
45#undef assert
46void sim_assert(int, const char *test, const char *, unsigned int, const char *);
47#define assert(x) sim_assert((x), #x, __FILE__, __LINE__, __func__)
48#endif
49
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040050static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080051
Fabio Utziga0e1cce2017-11-23 20:04:01 -020052#ifdef MCUBOOT_VALIDATE_SLOT0
53static int boot_status_fails = 0;
54#define BOOT_STATUS_ASSERT(x) \
55 do { \
56 if (x) { \
57 boot_status_fails++; \
58 } \
59 } while (0)
60#else
61#define BOOT_STATUS_ASSERT(x) assert(x)
62#endif
63
Christopher Collins92ea77f2016-12-12 15:59:26 -080064struct boot_status_table {
65 /**
66 * For each field, a value of 0 means "any".
67 */
68 uint8_t bst_magic_slot0;
69 uint8_t bst_magic_scratch;
70 uint8_t bst_copy_done_slot0;
71 uint8_t bst_status_source;
72};
73
74/**
75 * This set of tables maps swap state contents to boot status location.
76 * When searching for a match, these tables must be iterated in order.
77 */
78static const struct boot_status_table boot_status_tables[] = {
79 {
80 /* | slot-0 | scratch |
81 * ----------+------------+------------|
82 * magic | Good | Any |
83 * copy-done | 0x01 | N/A |
84 * ----------+------------+------------'
85 * source: none |
86 * ------------------------------------'
87 */
88 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
89 .bst_magic_scratch = 0,
90 .bst_copy_done_slot0 = 0x01,
91 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
92 },
93
94 {
95 /* | slot-0 | scratch |
96 * ----------+------------+------------|
97 * magic | Good | Any |
98 * copy-done | 0xff | N/A |
99 * ----------+------------+------------'
100 * source: slot 0 |
101 * ------------------------------------'
102 */
103 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
104 .bst_magic_scratch = 0,
105 .bst_copy_done_slot0 = 0xff,
106 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
107 },
108
109 {
110 /* | slot-0 | scratch |
111 * ----------+------------+------------|
112 * magic | Any | Good |
113 * copy-done | Any | N/A |
114 * ----------+------------+------------'
115 * source: scratch |
116 * ------------------------------------'
117 */
118 .bst_magic_slot0 = 0,
119 .bst_magic_scratch = BOOT_MAGIC_GOOD,
120 .bst_copy_done_slot0 = 0,
121 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
122 },
123
124 {
125 /* | slot-0 | scratch |
126 * ----------+------------+------------|
127 * magic | Unset | Any |
128 * copy-done | 0xff | N/A |
129 * ----------+------------+------------|
130 * source: varies |
131 * ------------------------------------+------------------------------+
132 * This represents one of two cases: |
133 * o No swaps ever (no status to read, so no harm in checking). |
134 * o Mid-revert; status in slot 0. |
135 * -------------------------------------------------------------------'
136 */
137 .bst_magic_slot0 = BOOT_MAGIC_UNSET,
138 .bst_magic_scratch = 0,
139 .bst_copy_done_slot0 = 0xff,
140 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
141 },
142};
143
144#define BOOT_STATUS_TABLES_COUNT \
145 (sizeof boot_status_tables / sizeof boot_status_tables[0])
146
Marti Bolivarfd20c762017-02-07 16:52:50 -0500147#define BOOT_LOG_SWAP_STATE(area, state) \
148 BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \
149 (area), \
150 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
151 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
152 "bad"), \
153 (state)->copy_done, \
154 (state)->image_ok)
155
Christopher Collins92ea77f2016-12-12 15:59:26 -0800156/**
157 * Determines where in flash the most recent boot status is stored. The boot
158 * status is necessary for completing a swap that was interrupted by a boot
159 * loader reset.
160 *
161 * @return A BOOT_STATUS_SOURCE_[...] code indicating where * status should be read from.
162 */
163static int
164boot_status_source(void)
165{
166 const struct boot_status_table *table;
167 struct boot_swap_state state_scratch;
168 struct boot_swap_state state_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800169 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200170 size_t i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500171 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800172
Fabio Utzig2473ac02017-05-02 12:45:02 -0300173 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800174 assert(rc == 0);
175
Fabio Utzig2473ac02017-05-02 12:45:02 -0300176 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177 assert(rc == 0);
178
Marti Bolivarfd20c762017-02-07 16:52:50 -0500179 BOOT_LOG_SWAP_STATE("Image 0", &state_slot0);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500180 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
181
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300183 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800184
185 if ((table->bst_magic_slot0 == 0 ||
186 table->bst_magic_slot0 == state_slot0.magic) &&
187 (table->bst_magic_scratch == 0 ||
188 table->bst_magic_scratch == state_scratch.magic) &&
189 (table->bst_copy_done_slot0 == 0 ||
190 table->bst_copy_done_slot0 == state_slot0.copy_done)) {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500191 source = table->bst_status_source;
192 BOOT_LOG_INF("Boot source: %s",
193 source == BOOT_STATUS_SOURCE_NONE ? "none" :
194 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
195 source == BOOT_STATUS_SOURCE_SLOT0 ? "slot 0" :
196 "BUG; can't happen");
197 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800198 }
199 }
200
Marti Bolivarfd20c762017-02-07 16:52:50 -0500201 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800202 return BOOT_STATUS_SOURCE_NONE;
203}
204
205/**
206 * Calculates the type of swap that just completed.
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300207 *
208 * This is used when a swap is interrupted by an external event. After
209 * finishing the swap operation determines what the initial request was.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800210 */
211static int
212boot_previous_swap_type(void)
213{
214 int post_swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800215
216 post_swap_type = boot_swap_type();
217
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300218 switch (post_swap_type) {
219 case BOOT_SWAP_TYPE_NONE : return BOOT_SWAP_TYPE_PERM;
220 case BOOT_SWAP_TYPE_REVERT : return BOOT_SWAP_TYPE_TEST;
221 case BOOT_SWAP_TYPE_PANIC : return BOOT_SWAP_TYPE_PANIC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800222 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300223
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300224 return BOOT_SWAP_TYPE_FAIL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800225}
226
David Brownf5b33d82017-09-01 10:58:27 -0600227/*
228 * Compute the total size of the given image. Includes the size of
229 * the TLVs.
230 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300231#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600232static int
233boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
234{
235 const struct flash_area *fap;
236 struct image_tlv_info info;
237 int area_id;
238 int rc;
239
240 area_id = flash_area_id_from_image_slot(slot);
241 rc = flash_area_open(area_id, &fap);
242 if (rc != 0) {
243 rc = BOOT_EFLASH;
244 goto done;
245 }
246
247 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
248 &info, sizeof(info));
249 if (rc != 0) {
250 rc = BOOT_EFLASH;
251 goto done;
252 }
253 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
254 rc = BOOT_EBADIMAGE;
255 goto done;
256 }
257 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
258 rc = 0;
259
260done:
261 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300262 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600263}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300264#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600265
Fabio Utzigc08ed212017-06-20 19:28:36 -0300266static int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800267boot_read_image_header(int slot, struct image_header *out_hdr)
268{
269 const struct flash_area *fap;
270 int area_id;
271 int rc;
272
273 area_id = flash_area_id_from_image_slot(slot);
274 rc = flash_area_open(area_id, &fap);
275 if (rc != 0) {
276 rc = BOOT_EFLASH;
277 goto done;
278 }
279
280 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
281 if (rc != 0) {
282 rc = BOOT_EFLASH;
283 goto done;
284 }
285
286 rc = 0;
287
288done:
289 flash_area_close(fap);
290 return rc;
291}
292
293static int
294boot_read_image_headers(void)
295{
296 int rc;
297 int i;
298
299 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400300 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800301 if (rc != 0) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300302 /* If at least the first slot's header was read successfully, then
303 * the boot loader can attempt a boot. Failure to read any headers
304 * is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800305 */
306 if (i > 0) {
307 return 0;
308 } else {
309 return rc;
310 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800311 }
312 }
313
314 return 0;
315}
316
317static uint8_t
318boot_write_sz(void)
319{
320 uint8_t elem_sz;
321 uint8_t align;
322
323 /* Figure out what size to write update status update as. The size depends
324 * on what the minimum write size is for scratch area, active image slot.
325 * We need to use the bigger of those 2 values.
326 */
Marti Bolivare2587152017-06-12 15:52:05 -0400327 elem_sz = hal_flash_align(boot_img_fa_device_id(&boot_data, 0));
328 align = hal_flash_align(boot_scratch_fa_device_id(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800329 if (align > elem_sz) {
330 elem_sz = align;
331 }
332
333 return elem_sz;
334}
335
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800336static int
337boot_slots_compatible(void)
338{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400339 size_t num_sectors_0 = boot_img_num_sectors(&boot_data, 0);
340 size_t num_sectors_1 = boot_img_num_sectors(&boot_data, 1);
341 size_t size_0, size_1;
342 size_t i;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800343
344 /* Ensure both image slots have identical sector layouts. */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400345 if (num_sectors_0 != num_sectors_1) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800346 return 0;
347 }
Marti Bolivard3269fd2017-06-12 16:31:12 -0400348 for (i = 0; i < num_sectors_0; i++) {
349 size_0 = boot_img_sector_size(&boot_data, 0, i);
350 size_1 = boot_img_sector_size(&boot_data, 1, i);
351 if (size_0 != size_1) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800352 return 0;
353 }
354 }
355
356 return 1;
357}
358
Christopher Collins92ea77f2016-12-12 15:59:26 -0800359/**
360 * Determines the sector layout of both image slots and the scratch area.
361 * This information is necessary for calculating the number of bytes to erase
362 * and copy during an image swap. The information collected during this
363 * function is used to populate the boot_data global.
364 */
365static int
366boot_read_sectors(void)
367{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800368 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800369
Marti Bolivarcca28a92017-06-12 16:52:22 -0400370 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800371 if (rc != 0) {
372 return BOOT_EFLASH;
373 }
374
Marti Bolivarcca28a92017-06-12 16:52:22 -0400375 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800376 if (rc != 0) {
377 return BOOT_EFLASH;
378 }
379
Marti Bolivare10a7392017-06-14 16:20:07 -0400380 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800381
382 return 0;
383}
384
385static uint32_t
386boot_status_internal_off(int idx, int state, int elem_sz)
387{
388 int idx_sz;
389
390 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
391
392 return idx * idx_sz + state * elem_sz;
393}
394
395/**
396 * Reads the status of a partially-completed swap, if any. This is necessary
397 * to recover in case the boot lodaer was reset in the middle of a swap
398 * operation.
399 */
400static int
401boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
402{
403 uint32_t off;
404 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300405 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800406 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200407 int found_idx;
408 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800409 int rc;
410 int i;
411
412 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400413 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300414
Christopher Collins92ea77f2016-12-12 15:59:26 -0800415 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200416 found_idx = 0;
417 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300418 for (i = 0; i < max_entries; i++) {
Marti Bolivare10a7392017-06-14 16:20:07 -0400419 rc = flash_area_read(fap, off + i * BOOT_WRITE_SZ(&boot_data),
420 &status, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800421 if (rc != 0) {
422 return BOOT_EFLASH;
423 }
424
425 if (status == 0xff) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200426 if (found && !found_idx) {
427 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800428 }
429 } else if (!found) {
430 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200431 } else if (found_idx) {
432 invalid = 1;
433 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800434 }
435 }
436
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200437 if (invalid) {
438 /* This means there was an error writing status on the last
439 * swap. Tell user and move on to validation!
440 */
441 BOOT_LOG_ERR("Detected inconsistent status!");
442
443#if !defined(MCUBOOT_VALIDATE_SLOT0)
444 /* With validation of slot0 disabled, there is no way to be sure the
445 * swapped slot0 is OK, so abort!
446 */
447 assert(0);
448#endif
449 }
450
Christopher Collins92ea77f2016-12-12 15:59:26 -0800451 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200452 if (!found_idx) {
453 found_idx = i;
454 }
455 found_idx--;
456 bs->idx = found_idx / BOOT_STATUS_STATE_COUNT;
457 bs->state = found_idx % BOOT_STATUS_STATE_COUNT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800458 }
459
460 return 0;
461}
462
463/**
464 * Reads the boot status from the flash. The boot status contains
465 * the current state of an interrupted image copy operation. If the boot
466 * status is not present, or it indicates that previous copy finished,
467 * there is no operation in progress.
468 */
469static int
470boot_read_status(struct boot_status *bs)
471{
472 const struct flash_area *fap;
473 int status_loc;
474 int area_id;
475 int rc;
476
477 memset(bs, 0, sizeof *bs);
478
479 status_loc = boot_status_source();
480 switch (status_loc) {
481 case BOOT_STATUS_SOURCE_NONE:
482 return 0;
483
484 case BOOT_STATUS_SOURCE_SCRATCH:
485 area_id = FLASH_AREA_IMAGE_SCRATCH;
486 break;
487
488 case BOOT_STATUS_SOURCE_SLOT0:
489 area_id = FLASH_AREA_IMAGE_0;
490 break;
491
492 default:
493 assert(0);
494 return BOOT_EBADARGS;
495 }
496
497 rc = flash_area_open(area_id, &fap);
498 if (rc != 0) {
499 return BOOT_EFLASH;
500 }
501
Fabio Utzig46490722017-09-04 15:34:32 -0300502 rc = boot_read_status_bytes(fap, bs);
503
504 flash_area_close(fap);
505 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800506}
507
508/**
509 * Writes the supplied boot status to the flash file system. The boot status
510 * contains the current state of an in-progress image copy operation.
511 *
512 * @param bs The boot status to write.
513 *
514 * @return 0 on success; nonzero on failure.
515 */
516int
517boot_write_status(struct boot_status *bs)
518{
519 const struct flash_area *fap;
520 uint32_t off;
521 int area_id;
522 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300523 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700524 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800525
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300526 /* NOTE: The first sector copied (that is the last sector on slot) contains
527 * the trailer. Since in the last step SLOT 0 is erased, the first
528 * two status writes go to the scratch which will be copied to SLOT 0!
529 */
530
Fabio Utzig2473ac02017-05-02 12:45:02 -0300531 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800532 /* Write to scratch. */
533 area_id = FLASH_AREA_IMAGE_SCRATCH;
534 } else {
535 /* Write to slot 0. */
536 area_id = FLASH_AREA_IMAGE_0;
537 }
538
539 rc = flash_area_open(area_id, &fap);
540 if (rc != 0) {
541 rc = BOOT_EFLASH;
542 goto done;
543 }
544
545 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400546 boot_status_internal_off(bs->idx, bs->state,
547 BOOT_WRITE_SZ(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800548
David Brown9d725462017-01-23 15:50:58 -0700549 align = hal_flash_align(fap->fa_device_id);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300550 memset(buf, 0xFF, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700551 buf[0] = bs->state;
552
553 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800554 if (rc != 0) {
555 rc = BOOT_EFLASH;
556 goto done;
557 }
558
559 rc = 0;
560
561done:
562 flash_area_close(fap);
563 return rc;
564}
565
566/*
567 * Validate image hash/signature in a slot.
568 */
569static int
570boot_image_check(struct image_header *hdr, const struct flash_area *fap)
571{
David Browndb1d9d32017-01-06 11:07:54 -0700572 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800573
Christopher Collins92ea77f2016-12-12 15:59:26 -0800574 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
575 NULL, 0, NULL)) {
576 return BOOT_EBADIMAGE;
577 }
578 return 0;
579}
580
581static int
582split_image_check(struct image_header *app_hdr,
583 const struct flash_area *app_fap,
584 struct image_header *loader_hdr,
585 const struct flash_area *loader_fap)
586{
587 static void *tmpbuf;
588 uint8_t loader_hash[32];
589
590 if (!tmpbuf) {
591 tmpbuf = malloc(BOOT_TMPBUF_SZ);
592 if (!tmpbuf) {
593 return BOOT_ENOMEM;
594 }
595 }
596
597 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
598 NULL, 0, loader_hash)) {
599 return BOOT_EBADIMAGE;
600 }
601
602 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
603 loader_hash, 32, NULL)) {
604 return BOOT_EBADIMAGE;
605 }
606
607 return 0;
608}
609
610static int
David Brownd930ec62016-12-14 07:59:48 -0700611boot_validate_slot(int slot)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800612{
613 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400614 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800615 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300616
Marti Bolivarf804f622017-06-12 15:41:48 -0400617 hdr = boot_img_hdr(&boot_data, slot);
618 if (hdr->ih_magic == 0xffffffff || hdr->ih_flags & IMAGE_F_NON_BOOTABLE) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300619 /* No bootable image in slot; continue booting from slot 0. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800620 return -1;
621 }
622
David Brownd930ec62016-12-14 07:59:48 -0700623 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800624 if (rc != 0) {
625 return BOOT_EFLASH;
626 }
627
Marti Bolivarf804f622017-06-12 15:41:48 -0400628 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap) != 0)) {
David Brownb38e0442017-02-24 13:57:12 -0700629 if (slot != 0) {
630 flash_area_erase(fap, 0, fap->fa_size);
631 /* Image in slot 1 is invalid. Erase the image and
632 * continue booting from slot 0.
633 */
634 }
Fabio Utzigb6297af2017-10-05 18:26:36 -0300635 BOOT_LOG_ERR("Image in slot %d is not valid!", slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800636 return -1;
637 }
638
639 flash_area_close(fap);
640
641 /* Image in slot 1 is valid. */
642 return 0;
643}
644
645/**
646 * Determines which swap operation to perform, if any. If it is determined
647 * that a swap operation is required, the image in the second slot is checked
648 * for validity. If the image in the second slot is invalid, it is erased, and
649 * a swap type of "none" is indicated.
650 *
651 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
652 */
653static int
654boot_validated_swap_type(void)
655{
656 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800657
658 swap_type = boot_swap_type();
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300659 switch (swap_type) {
660 case BOOT_SWAP_TYPE_TEST:
661 case BOOT_SWAP_TYPE_PERM:
662 case BOOT_SWAP_TYPE_REVERT:
663 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
664 if (boot_validate_slot(1) != 0) {
665 swap_type = BOOT_SWAP_TYPE_FAIL;
666 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800667 }
668
669 return swap_type;
670}
671
672/**
673 * Calculates the number of sectors the scratch area can contain. A "last"
674 * source sector is specified because images are copied backwards in flash
675 * (final index to index number 0).
676 *
677 * @param last_sector_idx The index of the last source sector
678 * (inclusive).
679 * @param out_first_sector_idx The index of the first source sector
680 * (inclusive) gets written here.
681 *
682 * @return The number of bytes comprised by the
683 * [first-sector, last-sector] range.
684 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300685#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800686static uint32_t
687boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
688{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400689 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800690 uint32_t new_sz;
691 uint32_t sz;
692 int i;
693
694 sz = 0;
695
Marti Bolivard3269fd2017-06-12 16:31:12 -0400696 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800697 for (i = last_sector_idx; i >= 0; i--) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400698 new_sz = sz + boot_img_sector_size(&boot_data, 0, i);
699 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800700 break;
701 }
702 sz = new_sz;
703 }
704
705 /* i currently refers to a sector that doesn't fit or it is -1 because all
706 * sectors have been processed. In both cases, exclude sector i.
707 */
708 *out_first_sector_idx = i + 1;
709 return sz;
710}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300711#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800712
713/**
714 * Erases a region of flash.
715 *
716 * @param flash_area_idx The ID of the flash area containing the region
717 * to erase.
718 * @param off The offset within the flash area to start the
719 * erase.
720 * @param sz The number of bytes to erase.
721 *
722 * @return 0 on success; nonzero on failure.
723 */
724static int
725boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
726{
727 const struct flash_area *fap;
728 int rc;
729
730 rc = flash_area_open(flash_area_id, &fap);
731 if (rc != 0) {
732 rc = BOOT_EFLASH;
733 goto done;
734 }
735
736 rc = flash_area_erase(fap, off, sz);
737 if (rc != 0) {
738 rc = BOOT_EFLASH;
739 goto done;
740 }
741
742 rc = 0;
743
744done:
745 flash_area_close(fap);
746 return rc;
747}
748
749/**
750 * Copies the contents of one flash region to another. You must erase the
751 * destination region prior to calling this function.
752 *
753 * @param flash_area_id_src The ID of the source flash area.
754 * @param flash_area_id_dst The ID of the destination flash area.
755 * @param off_src The offset within the source flash area to
756 * copy from.
757 * @param off_dst The offset within the destination flash area to
758 * copy to.
759 * @param sz The number of bytes to copy.
760 *
761 * @return 0 on success; nonzero on failure.
762 */
763static int
764boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
765 uint32_t off_src, uint32_t off_dst, uint32_t sz)
766{
767 const struct flash_area *fap_src;
768 const struct flash_area *fap_dst;
769 uint32_t bytes_copied;
770 int chunk_sz;
771 int rc;
772
773 static uint8_t buf[1024];
774
775 fap_src = NULL;
776 fap_dst = NULL;
777
778 rc = flash_area_open(flash_area_id_src, &fap_src);
779 if (rc != 0) {
780 rc = BOOT_EFLASH;
781 goto done;
782 }
783
784 rc = flash_area_open(flash_area_id_dst, &fap_dst);
785 if (rc != 0) {
786 rc = BOOT_EFLASH;
787 goto done;
788 }
789
790 bytes_copied = 0;
791 while (bytes_copied < sz) {
792 if (sz - bytes_copied > sizeof buf) {
793 chunk_sz = sizeof buf;
794 } else {
795 chunk_sz = sz - bytes_copied;
796 }
797
798 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
799 if (rc != 0) {
800 rc = BOOT_EFLASH;
801 goto done;
802 }
803
804 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
805 if (rc != 0) {
806 rc = BOOT_EFLASH;
807 goto done;
808 }
809
810 bytes_copied += chunk_sz;
811 }
812
813 rc = 0;
814
815done:
Fabio Utzige7686262017-06-28 09:26:54 -0300816 if (fap_src) {
817 flash_area_close(fap_src);
818 }
819 if (fap_dst) {
820 flash_area_close(fap_dst);
821 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800822 return rc;
823}
824
David Brown6b1b3b92017-09-19 08:59:10 -0600825#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300826static inline int
Fabio Utzig46490722017-09-04 15:34:32 -0300827boot_status_init_by_id(int flash_area_id, const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300828{
829 const struct flash_area *fap;
830 struct boot_swap_state swap_state;
831 int rc;
832
833 rc = flash_area_open(flash_area_id, &fap);
834 assert(rc == 0);
835
836 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
837 assert(rc == 0);
838
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400839 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300840 rc = boot_write_image_ok(fap);
841 assert(rc == 0);
842 }
843
Fabio Utzig46490722017-09-04 15:34:32 -0300844 rc = boot_write_swap_size(fap, bs->swap_size);
845 assert(rc == 0);
846
Fabio Utzig2473ac02017-05-02 12:45:02 -0300847 rc = boot_write_magic(fap);
848 assert(rc == 0);
849
850 flash_area_close(fap);
851
852 return 0;
853}
David Brown6b1b3b92017-09-19 08:59:10 -0600854#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -0300855
Fabio Utzig358c9352017-07-25 22:10:45 -0300856#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300857static int
858boot_erase_last_sector_by_id(int flash_area_id)
859{
860 uint8_t slot;
861 uint32_t last_sector;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300862 int rc;
863
864 switch (flash_area_id) {
865 case FLASH_AREA_IMAGE_0:
866 slot = 0;
867 break;
868 case FLASH_AREA_IMAGE_1:
869 slot = 1;
870 break;
871 default:
872 return BOOT_EFLASH;
873 }
874
Marti Bolivard3269fd2017-06-12 16:31:12 -0400875 last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300876 rc = boot_erase_sector(flash_area_id,
Marti Bolivarea088872017-06-12 17:10:49 -0400877 boot_img_sector_off(&boot_data, slot, last_sector),
Marti Bolivard3269fd2017-06-12 16:31:12 -0400878 boot_img_sector_size(&boot_data, slot, last_sector));
Fabio Utzig2473ac02017-05-02 12:45:02 -0300879 assert(rc == 0);
880
881 return rc;
882}
Fabio Utzig358c9352017-07-25 22:10:45 -0300883#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -0300884
Christopher Collins92ea77f2016-12-12 15:59:26 -0800885/**
886 * Swaps the contents of two flash regions within the two image slots.
887 *
888 * @param idx The index of the first sector in the range of
889 * sectors being swapped.
890 * @param sz The number of bytes to swap.
891 * @param bs The current boot status. This struct gets
892 * updated according to the outcome.
893 *
894 * @return 0 on success; nonzero on failure.
895 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300896#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800897static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800898boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
899{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300900 const struct flash_area *fap;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800901 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300902 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800903 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300904 uint32_t scratch_trailer_off;
905 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400906 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800907 int rc;
908
909 /* Calculate offset from start of image area. */
Marti Bolivarea088872017-06-12 17:10:49 -0400910 img_off = boot_img_sector_off(&boot_data, 0, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800911
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300912 copy_sz = sz;
Marti Bolivare10a7392017-06-14 16:20:07 -0400913 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -0400914
915 /* sz in this function is always is always sized on a multiple of the
Marti Bolivarea088872017-06-12 17:10:49 -0400916 * sector size. The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -0400917 * is to determine if we're swapping the last sector. The last sector
918 * needs special handling because it's where the trailer lives. If we're
919 * copying it, we need to use scratch to write the trailer temporarily.
920 *
921 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
922 * controls if special handling is needed (swapping last sector).
923 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400924 last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
Marti Bolivarea088872017-06-12 17:10:49 -0400925 if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300926 copy_sz -= trailer_sz;
927 }
928
Fabio Utzig2473ac02017-05-02 12:45:02 -0300929 bs->use_scratch = (bs->idx == 0 && copy_sz != sz);
930
Christopher Collins92ea77f2016-12-12 15:59:26 -0800931 if (bs->state == 0) {
932 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800933 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800934
935 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300936 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800937 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800938
Fabio Utzig2473ac02017-05-02 12:45:02 -0300939 if (bs->idx == 0) {
940 if (bs->use_scratch) {
Fabio Utzig46490722017-09-04 15:34:32 -0300941 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH, bs);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300942 } else {
943 /* Prepare the status area... here it is known that the
944 * last sector is not being used by the image data so it's
945 * safe to erase.
946 */
947 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300948 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300949
Fabio Utzig46490722017-09-04 15:34:32 -0300950 boot_status_init_by_id(FLASH_AREA_IMAGE_0, bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300951 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300952 }
953
Christopher Collins92ea77f2016-12-12 15:59:26 -0800954 bs->state = 1;
Christopher Collins4772ac42017-02-27 20:08:01 -0800955 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200956 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800957 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300958
Christopher Collins92ea77f2016-12-12 15:59:26 -0800959 if (bs->state == 1) {
960 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800961 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800962
Christopher Collins92ea77f2016-12-12 15:59:26 -0800963 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
964 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800965 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800966
Fabio Utzig2473ac02017-05-02 12:45:02 -0300967 if (bs->idx == 0 && !bs->use_scratch) {
968 /* If not all sectors of the slot are being swapped,
969 * guarantee here that only slot0 will have the state.
970 */
971 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1);
972 assert(rc == 0);
973 }
974
Christopher Collins92ea77f2016-12-12 15:59:26 -0800975 bs->state = 2;
Christopher Collins4772ac42017-02-27 20:08:01 -0800976 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200977 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800978 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300979
Christopher Collins92ea77f2016-12-12 15:59:26 -0800980 if (bs->state == 2) {
981 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800982 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800983
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300984 /* NOTE: also copy trailer from scratch (has status info) */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800985 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300986 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800987 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800988
Fabio Utzig94d998c2017-05-22 11:02:41 -0400989 if (bs->use_scratch) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300990 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
991 assert(rc == 0);
992
993 scratch_trailer_off = boot_status_off(fap);
994
995 flash_area_close(fap);
996
997 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
998 assert(rc == 0);
999
1000 /* copy current status that is being maintained in scratch */
1001 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Marti Bolivare10a7392017-06-14 16:20:07 -04001002 scratch_trailer_off,
Fabio Utziga0bc9b52017-06-28 09:19:55 -03001003 img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -04001004 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001005 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001006
Fabio Utzig2473ac02017-05-02 12:45:02 -03001007 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1008 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001009 assert(rc == 0);
1010
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001011 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001012 rc = boot_write_image_ok(fap);
1013 assert(rc == 0);
1014 }
1015
Fabio Utzig46490722017-09-04 15:34:32 -03001016 rc = boot_write_swap_size(fap, bs->swap_size);
1017 assert(rc == 0);
1018
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001019 rc = boot_write_magic(fap);
1020 assert(rc == 0);
1021
1022 flash_area_close(fap);
1023 }
1024
Christopher Collins92ea77f2016-12-12 15:59:26 -08001025 bs->idx++;
1026 bs->state = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001027 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -08001028 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001029 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001030 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001031}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001032#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001033
1034/**
1035 * Swaps the two images in flash. If a prior copy operation was interrupted
1036 * by a system reset, this function completes that operation.
1037 *
1038 * @param bs The current boot status. This function reads
1039 * this struct to determine if it is resuming
1040 * an interrupted swap operation. This
1041 * function writes the updated status to this
1042 * function on return.
1043 *
1044 * @return 0 on success; nonzero on failure.
1045 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001046#ifdef MCUBOOT_OVERWRITE_ONLY
David Brown17609d82017-05-05 09:41:34 -06001047static int
1048boot_copy_image(struct boot_status *bs)
1049{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001050 size_t sect_count;
1051 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001052 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001053 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001054 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001055 size_t last_sector;
1056
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001057 (void)bs;
1058
Fabio Utzig13d9e352017-10-05 20:32:31 -03001059#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1060 uint32_t src_size = 0;
1061 rc = boot_read_image_size(1, boot_img_hdr(&boot_data, 1), &src_size);
1062 assert(rc == 0);
1063#endif
David Brown17609d82017-05-05 09:41:34 -06001064
1065 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
1066 BOOT_LOG_INF("Erasing slot0");
1067
Marti Bolivard3269fd2017-06-12 16:31:12 -04001068 sect_count = boot_img_num_sectors(&boot_data, 0);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001069 for (sect = 0, size = 0; sect < sect_count; sect++) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001070 this_size = boot_img_sector_size(&boot_data, 0, sect);
David Brown17609d82017-05-05 09:41:34 -06001071 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
1072 size,
1073 this_size);
1074 assert(rc == 0);
1075
1076 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001077
1078#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1079 if (size >= src_size) {
1080 break;
1081 }
1082#endif
David Brown17609d82017-05-05 09:41:34 -06001083 }
1084
Fabio Utzig6a537ee2017-09-13 17:31:44 -03001085 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%lx bytes", size);
David Brown17609d82017-05-05 09:41:34 -06001086 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
1087 0, 0, size);
1088
Fabio Utzig13d9e352017-10-05 20:32:31 -03001089 /*
1090 * Erases header and trailer. The trailer is erased because when a new
1091 * image is written without a trailer as is the case when using newt, the
1092 * trailer that was left might trigger a new upgrade.
1093 */
David Brown17609d82017-05-05 09:41:34 -06001094 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
Fabio Utzig13d9e352017-10-05 20:32:31 -03001095 boot_img_sector_off(&boot_data, 1, 0),
1096 boot_img_sector_size(&boot_data, 1, 0));
David Brown17609d82017-05-05 09:41:34 -06001097 assert(rc == 0);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001098 last_sector = boot_img_num_sectors(&boot_data, 1) - 1;
1099 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
1100 boot_img_sector_off(&boot_data, 1, last_sector),
1101 boot_img_sector_size(&boot_data, 1, last_sector));
1102 assert(rc == 0);
1103
1104 /* TODO: Perhaps verify slot 0's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001105
1106 return 0;
1107}
1108#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001109static int
1110boot_copy_image(struct boot_status *bs)
1111{
1112 uint32_t sz;
1113 int first_sector_idx;
1114 int last_sector_idx;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001115 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001116 struct image_header *hdr;
1117 uint32_t size;
1118 uint32_t copy_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001119 int rc;
1120
1121 /* FIXME: just do this if asked by user? */
1122
1123 size = copy_size = 0;
1124
Fabio Utzig46490722017-09-04 15:34:32 -03001125 if (bs->idx == 0 && bs->state == 0) {
1126 /*
1127 * No swap ever happened, so need to find the largest image which
1128 * will be used to determine the amount of sectors to swap.
1129 */
1130 hdr = boot_img_hdr(&boot_data, 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001131 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzig46490722017-09-04 15:34:32 -03001132 rc = boot_read_image_size(0, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001133 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001134 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001135
Fabio Utzig46490722017-09-04 15:34:32 -03001136 hdr = boot_img_hdr(&boot_data, 1);
1137 if (hdr->ih_magic == IMAGE_MAGIC) {
1138 rc = boot_read_image_size(1, hdr, &size);
1139 assert(rc == 0);
1140 }
1141
1142 if (size > copy_size) {
1143 copy_size = size;
1144 }
1145
1146 bs->swap_size = copy_size;
1147 } else {
1148 /*
1149 * If a swap was under way, the swap_size should already be present
1150 * in the trailer...
1151 */
1152 rc = boot_read_swap_size(&bs->swap_size);
1153 assert(rc == 0);
1154
1155 copy_size = bs->swap_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001156 }
1157
1158 size = 0;
1159 last_sector_idx = 0;
1160 while (1) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001161 size += boot_img_sector_size(&boot_data, 0, last_sector_idx);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001162 if (size >= copy_size) {
1163 break;
1164 }
1165 last_sector_idx++;
1166 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001167
1168 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001169 while (last_sector_idx >= 0) {
1170 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
1171 if (swap_idx >= bs->idx) {
1172 boot_swap_sectors(first_sector_idx, sz, bs);
1173 }
1174
1175 last_sector_idx = first_sector_idx - 1;
1176 swap_idx++;
1177 }
1178
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001179#ifdef MCUBOOT_VALIDATE_SLOT0
1180 if (boot_status_fails > 0) {
1181 BOOT_LOG_WRN("%d status write fails performing the swap", boot_status_fails);
1182 }
1183#endif
1184
Christopher Collins92ea77f2016-12-12 15:59:26 -08001185 return 0;
1186}
David Brown17609d82017-05-05 09:41:34 -06001187#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001188
1189/**
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001190 * Marks the image in slot 0 as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001191 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001192#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001193static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001194boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001195{
1196 const struct flash_area *fap;
1197 int rc;
1198
1199 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1200 if (rc != 0) {
1201 return BOOT_EFLASH;
1202 }
1203
1204 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001205 flash_area_close(fap);
1206 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001207}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001208#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001209
1210/**
1211 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1212 * the status bytes from the image revert operation don't get processed on a
1213 * subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001214 *
1215 * NOTE: image_ok is tested before writing because if there's a valid permanent
1216 * image installed on slot0 and the new image to be upgrade to has a bad sig,
1217 * image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001218 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001219#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001220static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001221boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001222{
1223 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001224 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001225 int rc;
1226
1227 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1228 if (rc != 0) {
1229 return BOOT_EFLASH;
1230 }
1231
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001232 rc = boot_read_swap_state(fap, &state);
1233 if (rc != 0) {
1234 rc = BOOT_EFLASH;
1235 goto out;
1236 }
1237
1238 if (state.image_ok == BOOT_FLAG_UNSET) {
1239 rc = boot_write_image_ok(fap);
1240 }
1241
1242out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001243 flash_area_close(fap);
1244 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001245}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001246#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001247
1248/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001249 * Performs an image swap if one is required.
1250 *
1251 * @param out_swap_type On success, the type of swap performed gets
1252 * written here.
1253 *
1254 * @return 0 on success; nonzero on failure.
1255 */
1256static int
1257boot_swap_if_needed(int *out_swap_type)
1258{
1259 struct boot_status bs;
1260 int swap_type;
1261 int rc;
1262
1263 /* Determine if we rebooted in the middle of an image swap
1264 * operation.
1265 */
1266 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001267 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001268 if (rc != 0) {
1269 return rc;
1270 }
1271
1272 /* If a partial swap was detected, complete it. */
1273 if (bs.idx != 0 || bs.state != 0) {
1274 rc = boot_copy_image(&bs);
1275 assert(rc == 0);
1276
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001277 /* NOTE: here we have finished a swap resume. The initial request
1278 * was either a TEST or PERM swap, which now after the completed
1279 * swap will be determined to be respectively REVERT (was TEST)
1280 * or NONE (was PERM).
1281 */
1282
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001283 /* Extrapolate the type of the partial swap. We need this
1284 * information to know how to mark the swap complete in flash.
1285 */
1286 swap_type = boot_previous_swap_type();
1287 } else {
1288 swap_type = boot_validated_swap_type();
1289 switch (swap_type) {
1290 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001291 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001292 case BOOT_SWAP_TYPE_REVERT:
1293 rc = boot_copy_image(&bs);
1294 assert(rc == 0);
1295 break;
1296 }
1297 }
1298
1299 *out_swap_type = swap_type;
1300 return 0;
1301}
1302
1303/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001304 * Prepares the booting process. This function moves images around in flash as
1305 * appropriate, and tells you what address to boot from.
1306 *
1307 * @param rsp On success, indicates how booting should occur.
1308 *
1309 * @return 0 on success; nonzero on failure.
1310 */
1311int
1312boot_go(struct boot_rsp *rsp)
1313{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001314 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001315 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001316 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001317 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001318 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001319
1320 /* The array of slot sectors are defined here (as opposed to file scope) so
1321 * that they don't get allocated for non-boot-loader apps. This is
1322 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001323 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001324 */
Marti Bolivarc50926f2017-06-14 09:35:40 -04001325 static boot_sector_t slot0_sectors[BOOT_MAX_IMG_SECTORS];
1326 static boot_sector_t slot1_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001327 boot_data.imgs[0].sectors = slot0_sectors;
1328 boot_data.imgs[1].sectors = slot1_sectors;
1329
Marti Bolivarc0b47912017-06-13 17:18:09 -04001330 /* Open boot_data image areas for the duration of this call. */
1331 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1332 fa_id = flash_area_id_from_image_slot(slot);
1333 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1334 assert(rc == 0);
1335 }
1336 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1337 &BOOT_SCRATCH_AREA(&boot_data));
1338 assert(rc == 0);
1339
Christopher Collins92ea77f2016-12-12 15:59:26 -08001340 /* Determine the sector layout of the image slots and scratch area. */
1341 rc = boot_read_sectors();
1342 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001343 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001344 }
1345
1346 /* Attempt to read an image header from each slot. */
1347 rc = boot_read_image_headers();
1348 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001349 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001350 }
1351
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001352 /* If the image slots aren't compatible, no swap is possible. Just boot
1353 * into slot 0.
1354 */
1355 if (boot_slots_compatible()) {
1356 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001357 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001358 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001359 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001360 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001361
1362 /*
1363 * The following states need image_ok be explicitly set after the
1364 * swap was finished to avoid a new revert.
1365 */
1366 if (swap_type == BOOT_SWAP_TYPE_REVERT || swap_type == BOOT_SWAP_TYPE_FAIL) {
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001367#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001368 rc = boot_set_image_ok();
1369 if (rc != 0) {
1370 swap_type = BOOT_SWAP_TYPE_PANIC;
1371 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001372#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001373 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001374 } else {
1375 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001376 }
1377
1378 switch (swap_type) {
1379 case BOOT_SWAP_TYPE_NONE:
1380 slot = 0;
1381 break;
1382
Fabio Utzig695d5642017-07-20 09:47:16 -03001383 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1384 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001385 case BOOT_SWAP_TYPE_REVERT:
1386 slot = 1;
David Brown52eee562017-07-05 11:25:09 -06001387 reload_headers = true;
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001388#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001389 rc = boot_set_copy_done();
1390 if (rc != 0) {
1391 swap_type = BOOT_SWAP_TYPE_PANIC;
1392 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001393#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001394 break;
1395
1396 case BOOT_SWAP_TYPE_FAIL:
1397 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1398 * try to boot into it again on the next reboot. Do this by pretending
1399 * we just reverted back to slot 0.
1400 */
1401 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001402 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001403 break;
1404
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001405 default:
Fabio Utzig695d5642017-07-20 09:47:16 -03001406 swap_type = BOOT_SWAP_TYPE_PANIC;
1407 }
1408
1409 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1410 BOOT_LOG_ERR("panic!");
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001411 assert(0);
Fabio Utzig695d5642017-07-20 09:47:16 -03001412
1413 /* Loop forever... */
1414 while (1) {}
Christopher Collins92ea77f2016-12-12 15:59:26 -08001415 }
1416
David Brown52eee562017-07-05 11:25:09 -06001417 if (reload_headers) {
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001418 rc = boot_read_image_headers();
1419 if (rc != 0) {
1420 goto out;
1421 }
1422 /* Since headers were reloaded, it can be assumed we just performed a
1423 * swap or overwrite. Now the header info that should be used to
1424 * provide the data for the bootstrap, which previously was at Slot 1,
1425 * was updated to Slot 0.
1426 */
1427 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001428 }
1429
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001430#ifdef MCUBOOT_VALIDATE_SLOT0
David Brown554c52e2017-06-30 16:01:07 -06001431 rc = boot_validate_slot(0);
1432 assert(rc == 0);
1433 if (rc != 0) {
1434 rc = BOOT_EBADIMAGE;
1435 goto out;
1436 }
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001437#else
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001438 /* Even if we're not re-validating slot 0, we could be booting
1439 * onto an empty flash chip. At least do a basic sanity check that
1440 * the magic number on the image is OK.
1441 */
1442 if (boot_data.imgs[0].hdr.ih_magic != IMAGE_MAGIC) {
1443 BOOT_LOG_ERR("bad image magic 0x%x", boot_data.imgs[0].hdr.ih_magic);
1444 rc = BOOT_EBADIMAGE;
1445 goto out;
1446 }
David Brown554c52e2017-06-30 16:01:07 -06001447#endif
1448
Christopher Collins92ea77f2016-12-12 15:59:26 -08001449 /* Always boot from the primary slot. */
Marti Bolivar428cdbf2017-05-01 22:32:42 -04001450 rsp->br_flash_dev_id = boot_img_fa_device_id(&boot_data, 0);
Marti Bolivar88f48d92017-05-01 22:30:02 -04001451 rsp->br_image_off = boot_img_slot_off(&boot_data, 0);
Marti Bolivarf804f622017-06-12 15:41:48 -04001452 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001453
Marti Bolivarc0b47912017-06-13 17:18:09 -04001454 out:
1455 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1456 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1457 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1458 }
1459 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001460}
1461
1462int
1463split_go(int loader_slot, int split_slot, void **entry)
1464{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001465 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001466 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001467 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001468 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001469 int rc;
1470
Christopher Collins92ea77f2016-12-12 15:59:26 -08001471 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1472 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001473 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001474 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001475 boot_data.imgs[loader_slot].sectors = sectors + 0;
1476 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1477
1478 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1479 rc = flash_area_open(loader_flash_id,
1480 &BOOT_IMG_AREA(&boot_data, split_slot));
1481 assert(rc == 0);
1482 split_flash_id = flash_area_id_from_image_slot(split_slot);
1483 rc = flash_area_open(split_flash_id,
1484 &BOOT_IMG_AREA(&boot_data, split_slot));
1485 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001486
1487 /* Determine the sector layout of the image slots and scratch area. */
1488 rc = boot_read_sectors();
1489 if (rc != 0) {
1490 rc = SPLIT_GO_ERR;
1491 goto done;
1492 }
1493
1494 rc = boot_read_image_headers();
1495 if (rc != 0) {
1496 goto done;
1497 }
1498
Christopher Collins92ea77f2016-12-12 15:59:26 -08001499 /* Don't check the bootable image flag because we could really call a
1500 * bootable or non-bootable image. Just validate that the image check
1501 * passes which is distinct from the normal check.
1502 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001503 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001504 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001505 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001506 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001507 if (rc != 0) {
1508 rc = SPLIT_GO_NON_MATCHING;
1509 goto done;
1510 }
1511
Marti Bolivarea088872017-06-12 17:10:49 -04001512 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001513 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001514 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001515 rc = SPLIT_GO_OK;
1516
1517done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001518 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1519 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001520 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001521 return rc;
1522}