blob: a76d056bcc448918bd5f1ee723f093a24b606fd7 [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#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030041
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040042static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080043
Fabio Utzigf70e3022018-01-10 15:00:42 -020044#if defined(MCUBOOT_VALIDATE_SLOT0) && !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020045static int boot_status_fails = 0;
46#define BOOT_STATUS_ASSERT(x) \
47 do { \
Johann Fischered8461b2018-02-15 16:50:31 +010048 if (!(x)) { \
Fabio Utziga0e1cce2017-11-23 20:04:01 -020049 boot_status_fails++; \
50 } \
51 } while (0)
52#else
Fabio Utzig57c40f72017-12-12 21:48:30 -020053#define BOOT_STATUS_ASSERT(x) ASSERT(x)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020054#endif
55
Christopher Collins92ea77f2016-12-12 15:59:26 -080056struct boot_status_table {
57 /**
58 * For each field, a value of 0 means "any".
59 */
60 uint8_t bst_magic_slot0;
61 uint8_t bst_magic_scratch;
62 uint8_t bst_copy_done_slot0;
63 uint8_t bst_status_source;
64};
65
66/**
67 * This set of tables maps swap state contents to boot status location.
68 * When searching for a match, these tables must be iterated in order.
69 */
70static const struct boot_status_table boot_status_tables[] = {
71 {
72 /* | slot-0 | scratch |
73 * ----------+------------+------------|
74 * magic | Good | Any |
75 * copy-done | 0x01 | N/A |
76 * ----------+------------+------------'
77 * source: none |
78 * ------------------------------------'
79 */
80 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
81 .bst_magic_scratch = 0,
82 .bst_copy_done_slot0 = 0x01,
83 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
84 },
85
86 {
87 /* | slot-0 | scratch |
88 * ----------+------------+------------|
89 * magic | Good | Any |
90 * copy-done | 0xff | N/A |
91 * ----------+------------+------------'
92 * source: slot 0 |
93 * ------------------------------------'
94 */
95 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
96 .bst_magic_scratch = 0,
97 .bst_copy_done_slot0 = 0xff,
98 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
99 },
100
101 {
102 /* | slot-0 | scratch |
103 * ----------+------------+------------|
104 * magic | Any | Good |
105 * copy-done | Any | N/A |
106 * ----------+------------+------------'
107 * source: scratch |
108 * ------------------------------------'
109 */
110 .bst_magic_slot0 = 0,
111 .bst_magic_scratch = BOOT_MAGIC_GOOD,
112 .bst_copy_done_slot0 = 0,
113 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
114 },
115
116 {
117 /* | slot-0 | scratch |
118 * ----------+------------+------------|
119 * magic | Unset | Any |
120 * copy-done | 0xff | N/A |
121 * ----------+------------+------------|
122 * source: varies |
123 * ------------------------------------+------------------------------+
124 * This represents one of two cases: |
125 * o No swaps ever (no status to read, so no harm in checking). |
126 * o Mid-revert; status in slot 0. |
127 * -------------------------------------------------------------------'
128 */
129 .bst_magic_slot0 = BOOT_MAGIC_UNSET,
130 .bst_magic_scratch = 0,
131 .bst_copy_done_slot0 = 0xff,
132 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
133 },
134};
135
136#define BOOT_STATUS_TABLES_COUNT \
137 (sizeof boot_status_tables / sizeof boot_status_tables[0])
138
Marti Bolivarfd20c762017-02-07 16:52:50 -0500139#define BOOT_LOG_SWAP_STATE(area, state) \
140 BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \
141 (area), \
142 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
143 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
144 "bad"), \
145 (state)->copy_done, \
146 (state)->image_ok)
147
Christopher Collins92ea77f2016-12-12 15:59:26 -0800148/**
149 * Determines where in flash the most recent boot status is stored. The boot
150 * status is necessary for completing a swap that was interrupted by a boot
151 * loader reset.
152 *
153 * @return A BOOT_STATUS_SOURCE_[...] code indicating where * status should be read from.
154 */
155static int
156boot_status_source(void)
157{
158 const struct boot_status_table *table;
159 struct boot_swap_state state_scratch;
160 struct boot_swap_state state_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800161 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200162 size_t i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500163 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800164
Fabio Utzig2473ac02017-05-02 12:45:02 -0300165 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800166 assert(rc == 0);
167
Fabio Utzig2473ac02017-05-02 12:45:02 -0300168 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800169 assert(rc == 0);
170
Marti Bolivarfd20c762017-02-07 16:52:50 -0500171 BOOT_LOG_SWAP_STATE("Image 0", &state_slot0);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500172 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
173
Christopher Collins92ea77f2016-12-12 15:59:26 -0800174 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300175 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800176
177 if ((table->bst_magic_slot0 == 0 ||
178 table->bst_magic_slot0 == state_slot0.magic) &&
179 (table->bst_magic_scratch == 0 ||
180 table->bst_magic_scratch == state_scratch.magic) &&
181 (table->bst_copy_done_slot0 == 0 ||
182 table->bst_copy_done_slot0 == state_slot0.copy_done)) {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500183 source = table->bst_status_source;
184 BOOT_LOG_INF("Boot source: %s",
185 source == BOOT_STATUS_SOURCE_NONE ? "none" :
186 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
187 source == BOOT_STATUS_SOURCE_SLOT0 ? "slot 0" :
188 "BUG; can't happen");
189 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800190 }
191 }
192
Marti Bolivarfd20c762017-02-07 16:52:50 -0500193 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800194 return BOOT_STATUS_SOURCE_NONE;
195}
196
197/**
198 * Calculates the type of swap that just completed.
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300199 *
200 * This is used when a swap is interrupted by an external event. After
201 * finishing the swap operation determines what the initial request was.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800202 */
203static int
204boot_previous_swap_type(void)
205{
206 int post_swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800207
208 post_swap_type = boot_swap_type();
209
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300210 switch (post_swap_type) {
211 case BOOT_SWAP_TYPE_NONE : return BOOT_SWAP_TYPE_PERM;
212 case BOOT_SWAP_TYPE_REVERT : return BOOT_SWAP_TYPE_TEST;
213 case BOOT_SWAP_TYPE_PANIC : return BOOT_SWAP_TYPE_PANIC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800214 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300215
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300216 return BOOT_SWAP_TYPE_FAIL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800217}
218
David Brownf5b33d82017-09-01 10:58:27 -0600219/*
220 * Compute the total size of the given image. Includes the size of
221 * the TLVs.
222 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300223#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600224static int
225boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
226{
227 const struct flash_area *fap;
228 struct image_tlv_info info;
229 int area_id;
230 int rc;
231
232 area_id = flash_area_id_from_image_slot(slot);
233 rc = flash_area_open(area_id, &fap);
234 if (rc != 0) {
235 rc = BOOT_EFLASH;
236 goto done;
237 }
238
239 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
240 &info, sizeof(info));
241 if (rc != 0) {
242 rc = BOOT_EFLASH;
243 goto done;
244 }
245 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
246 rc = BOOT_EBADIMAGE;
247 goto done;
248 }
249 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
250 rc = 0;
251
252done:
253 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300254 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600255}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300256#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600257
Fabio Utzigc08ed212017-06-20 19:28:36 -0300258static int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800259boot_read_image_header(int slot, struct image_header *out_hdr)
260{
261 const struct flash_area *fap;
262 int area_id;
263 int rc;
264
265 area_id = flash_area_id_from_image_slot(slot);
266 rc = flash_area_open(area_id, &fap);
267 if (rc != 0) {
268 rc = BOOT_EFLASH;
269 goto done;
270 }
271
272 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
273 if (rc != 0) {
274 rc = BOOT_EFLASH;
275 goto done;
276 }
277
278 rc = 0;
279
280done:
281 flash_area_close(fap);
282 return rc;
283}
284
285static int
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200286boot_read_image_headers(bool require_all)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800287{
288 int rc;
289 int i;
290
291 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400292 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800293 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200294 /* If `require_all` is set, fail on any single fail, otherwise
295 * if at least the first slot's header was read successfully,
296 * then the boot loader can attempt a boot.
297 *
298 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800299 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200300 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800301 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
Fabio Utziga1fae672018-03-30 10:52:38 -0300338 if (num_sectors_0 > BOOT_MAX_IMG_SECTORS || num_sectors_1 > BOOT_MAX_IMG_SECTORS) {
339 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800340 return 0;
341 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300342
343 /* Ensure both image slots have identical sector layouts. */
344 if (num_sectors_0 != num_sectors_1) {
345 BOOT_LOG_WRN("Cannot upgrade: number of sectors differ between slots");
346 return 0;
347 }
348
Marti Bolivard3269fd2017-06-12 16:31:12 -0400349 for (i = 0; i < num_sectors_0; i++) {
350 size_0 = boot_img_sector_size(&boot_data, 0, i);
351 size_1 = boot_img_sector_size(&boot_data, 1, i);
352 if (size_0 != size_1) {
Fabio Utziga1fae672018-03-30 10:52:38 -0300353 BOOT_LOG_WRN("Cannot upgrade: an incompatible sector was found");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800354 return 0;
355 }
356 }
357
358 return 1;
359}
360
Christopher Collins92ea77f2016-12-12 15:59:26 -0800361/**
362 * Determines the sector layout of both image slots and the scratch area.
363 * This information is necessary for calculating the number of bytes to erase
364 * and copy during an image swap. The information collected during this
365 * function is used to populate the boot_data global.
366 */
367static int
368boot_read_sectors(void)
369{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800370 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800371
Marti Bolivarcca28a92017-06-12 16:52:22 -0400372 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800373 if (rc != 0) {
374 return BOOT_EFLASH;
375 }
376
Marti Bolivarcca28a92017-06-12 16:52:22 -0400377 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800378 if (rc != 0) {
379 return BOOT_EFLASH;
380 }
381
Marti Bolivare10a7392017-06-14 16:20:07 -0400382 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800383
384 return 0;
385}
386
387static uint32_t
388boot_status_internal_off(int idx, int state, int elem_sz)
389{
390 int idx_sz;
391
392 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
393
394 return idx * idx_sz + state * elem_sz;
395}
396
397/**
398 * Reads the status of a partially-completed swap, if any. This is necessary
399 * to recover in case the boot lodaer was reset in the middle of a swap
400 * operation.
401 */
402static int
403boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
404{
405 uint32_t off;
406 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300407 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800408 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200409 int found_idx;
410 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800411 int rc;
412 int i;
413
414 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400415 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300416
Christopher Collins92ea77f2016-12-12 15:59:26 -0800417 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200418 found_idx = 0;
419 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300420 for (i = 0; i < max_entries; i++) {
Marti Bolivare10a7392017-06-14 16:20:07 -0400421 rc = flash_area_read(fap, off + i * BOOT_WRITE_SZ(&boot_data),
422 &status, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800423 if (rc != 0) {
424 return BOOT_EFLASH;
425 }
426
427 if (status == 0xff) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200428 if (found && !found_idx) {
429 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800430 }
431 } else if (!found) {
432 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200433 } else if (found_idx) {
434 invalid = 1;
435 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800436 }
437 }
438
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200439 if (invalid) {
440 /* This means there was an error writing status on the last
441 * swap. Tell user and move on to validation!
442 */
443 BOOT_LOG_ERR("Detected inconsistent status!");
444
445#if !defined(MCUBOOT_VALIDATE_SLOT0)
446 /* With validation of slot0 disabled, there is no way to be sure the
447 * swapped slot0 is OK, so abort!
448 */
449 assert(0);
450#endif
451 }
452
Christopher Collins92ea77f2016-12-12 15:59:26 -0800453 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200454 if (!found_idx) {
455 found_idx = i;
456 }
457 found_idx--;
458 bs->idx = found_idx / BOOT_STATUS_STATE_COUNT;
459 bs->state = found_idx % BOOT_STATUS_STATE_COUNT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800460 }
461
462 return 0;
463}
464
465/**
466 * Reads the boot status from the flash. The boot status contains
467 * the current state of an interrupted image copy operation. If the boot
468 * status is not present, or it indicates that previous copy finished,
469 * there is no operation in progress.
470 */
471static int
472boot_read_status(struct boot_status *bs)
473{
474 const struct flash_area *fap;
475 int status_loc;
476 int area_id;
477 int rc;
478
479 memset(bs, 0, sizeof *bs);
480
481 status_loc = boot_status_source();
482 switch (status_loc) {
483 case BOOT_STATUS_SOURCE_NONE:
484 return 0;
485
486 case BOOT_STATUS_SOURCE_SCRATCH:
487 area_id = FLASH_AREA_IMAGE_SCRATCH;
488 break;
489
490 case BOOT_STATUS_SOURCE_SLOT0:
491 area_id = FLASH_AREA_IMAGE_0;
492 break;
493
494 default:
495 assert(0);
496 return BOOT_EBADARGS;
497 }
498
499 rc = flash_area_open(area_id, &fap);
500 if (rc != 0) {
501 return BOOT_EFLASH;
502 }
503
Fabio Utzig46490722017-09-04 15:34:32 -0300504 rc = boot_read_status_bytes(fap, bs);
505
506 flash_area_close(fap);
507 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800508}
509
510/**
511 * Writes the supplied boot status to the flash file system. The boot status
512 * contains the current state of an in-progress image copy operation.
513 *
514 * @param bs The boot status to write.
515 *
516 * @return 0 on success; nonzero on failure.
517 */
518int
519boot_write_status(struct boot_status *bs)
520{
521 const struct flash_area *fap;
522 uint32_t off;
523 int area_id;
524 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300525 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700526 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800527
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300528 /* NOTE: The first sector copied (that is the last sector on slot) contains
529 * the trailer. Since in the last step SLOT 0 is erased, the first
530 * two status writes go to the scratch which will be copied to SLOT 0!
531 */
532
Fabio Utzig2473ac02017-05-02 12:45:02 -0300533 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800534 /* Write to scratch. */
535 area_id = FLASH_AREA_IMAGE_SCRATCH;
536 } else {
537 /* Write to slot 0. */
538 area_id = FLASH_AREA_IMAGE_0;
539 }
540
541 rc = flash_area_open(area_id, &fap);
542 if (rc != 0) {
543 rc = BOOT_EFLASH;
544 goto done;
545 }
546
547 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400548 boot_status_internal_off(bs->idx, bs->state,
549 BOOT_WRITE_SZ(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800550
David Brown9d725462017-01-23 15:50:58 -0700551 align = hal_flash_align(fap->fa_device_id);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300552 memset(buf, 0xFF, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700553 buf[0] = bs->state;
554
555 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800556 if (rc != 0) {
557 rc = BOOT_EFLASH;
558 goto done;
559 }
560
561 rc = 0;
562
563done:
564 flash_area_close(fap);
565 return rc;
566}
567
568/*
569 * Validate image hash/signature in a slot.
570 */
571static int
572boot_image_check(struct image_header *hdr, const struct flash_area *fap)
573{
David Browndb1d9d32017-01-06 11:07:54 -0700574 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800575
Christopher Collins92ea77f2016-12-12 15:59:26 -0800576 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
577 NULL, 0, NULL)) {
578 return BOOT_EBADIMAGE;
579 }
580 return 0;
581}
582
583static int
584split_image_check(struct image_header *app_hdr,
585 const struct flash_area *app_fap,
586 struct image_header *loader_hdr,
587 const struct flash_area *loader_fap)
588{
589 static void *tmpbuf;
590 uint8_t loader_hash[32];
591
592 if (!tmpbuf) {
593 tmpbuf = malloc(BOOT_TMPBUF_SZ);
594 if (!tmpbuf) {
595 return BOOT_ENOMEM;
596 }
597 }
598
599 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
600 NULL, 0, loader_hash)) {
601 return BOOT_EBADIMAGE;
602 }
603
604 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
605 loader_hash, 32, NULL)) {
606 return BOOT_EBADIMAGE;
607 }
608
609 return 0;
610}
611
612static int
David Brownd930ec62016-12-14 07:59:48 -0700613boot_validate_slot(int slot)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800614{
615 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400616 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800617 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300618
Marti Bolivarf804f622017-06-12 15:41:48 -0400619 hdr = boot_img_hdr(&boot_data, slot);
620 if (hdr->ih_magic == 0xffffffff || hdr->ih_flags & IMAGE_F_NON_BOOTABLE) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300621 /* No bootable image in slot; continue booting from slot 0. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800622 return -1;
623 }
624
David Brownd930ec62016-12-14 07:59:48 -0700625 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800626 if (rc != 0) {
627 return BOOT_EFLASH;
628 }
629
Marti Bolivarf804f622017-06-12 15:41:48 -0400630 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap) != 0)) {
David Brownb38e0442017-02-24 13:57:12 -0700631 if (slot != 0) {
632 flash_area_erase(fap, 0, fap->fa_size);
633 /* Image in slot 1 is invalid. Erase the image and
634 * continue booting from slot 0.
635 */
636 }
Fabio Utzigb6297af2017-10-05 18:26:36 -0300637 BOOT_LOG_ERR("Image in slot %d is not valid!", slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800638 return -1;
639 }
640
641 flash_area_close(fap);
642
643 /* Image in slot 1 is valid. */
644 return 0;
645}
646
647/**
648 * Determines which swap operation to perform, if any. If it is determined
649 * that a swap operation is required, the image in the second slot is checked
650 * for validity. If the image in the second slot is invalid, it is erased, and
651 * a swap type of "none" is indicated.
652 *
653 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
654 */
655static int
656boot_validated_swap_type(void)
657{
658 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800659
660 swap_type = boot_swap_type();
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300661 switch (swap_type) {
662 case BOOT_SWAP_TYPE_TEST:
663 case BOOT_SWAP_TYPE_PERM:
664 case BOOT_SWAP_TYPE_REVERT:
665 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
666 if (boot_validate_slot(1) != 0) {
667 swap_type = BOOT_SWAP_TYPE_FAIL;
668 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800669 }
670
671 return swap_type;
672}
673
674/**
675 * Calculates the number of sectors the scratch area can contain. A "last"
676 * source sector is specified because images are copied backwards in flash
677 * (final index to index number 0).
678 *
679 * @param last_sector_idx The index of the last source sector
680 * (inclusive).
681 * @param out_first_sector_idx The index of the first source sector
682 * (inclusive) gets written here.
683 *
684 * @return The number of bytes comprised by the
685 * [first-sector, last-sector] range.
686 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300687#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800688static uint32_t
689boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
690{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400691 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800692 uint32_t new_sz;
693 uint32_t sz;
694 int i;
695
696 sz = 0;
697
Marti Bolivard3269fd2017-06-12 16:31:12 -0400698 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800699 for (i = last_sector_idx; i >= 0; i--) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400700 new_sz = sz + boot_img_sector_size(&boot_data, 0, i);
701 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800702 break;
703 }
704 sz = new_sz;
705 }
706
707 /* i currently refers to a sector that doesn't fit or it is -1 because all
708 * sectors have been processed. In both cases, exclude sector i.
709 */
710 *out_first_sector_idx = i + 1;
711 return sz;
712}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300713#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800714
715/**
716 * Erases a region of flash.
717 *
718 * @param flash_area_idx The ID of the flash area containing the region
719 * to erase.
720 * @param off The offset within the flash area to start the
721 * erase.
722 * @param sz The number of bytes to erase.
723 *
724 * @return 0 on success; nonzero on failure.
725 */
726static int
727boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
728{
729 const struct flash_area *fap;
730 int rc;
731
732 rc = flash_area_open(flash_area_id, &fap);
733 if (rc != 0) {
734 rc = BOOT_EFLASH;
735 goto done;
736 }
737
738 rc = flash_area_erase(fap, off, sz);
739 if (rc != 0) {
740 rc = BOOT_EFLASH;
741 goto done;
742 }
743
744 rc = 0;
745
746done:
747 flash_area_close(fap);
748 return rc;
749}
750
751/**
752 * Copies the contents of one flash region to another. You must erase the
753 * destination region prior to calling this function.
754 *
755 * @param flash_area_id_src The ID of the source flash area.
756 * @param flash_area_id_dst The ID of the destination flash area.
757 * @param off_src The offset within the source flash area to
758 * copy from.
759 * @param off_dst The offset within the destination flash area to
760 * copy to.
761 * @param sz The number of bytes to copy.
762 *
763 * @return 0 on success; nonzero on failure.
764 */
765static int
766boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
767 uint32_t off_src, uint32_t off_dst, uint32_t sz)
768{
769 const struct flash_area *fap_src;
770 const struct flash_area *fap_dst;
771 uint32_t bytes_copied;
772 int chunk_sz;
773 int rc;
774
775 static uint8_t buf[1024];
776
777 fap_src = NULL;
778 fap_dst = NULL;
779
780 rc = flash_area_open(flash_area_id_src, &fap_src);
781 if (rc != 0) {
782 rc = BOOT_EFLASH;
783 goto done;
784 }
785
786 rc = flash_area_open(flash_area_id_dst, &fap_dst);
787 if (rc != 0) {
788 rc = BOOT_EFLASH;
789 goto done;
790 }
791
792 bytes_copied = 0;
793 while (bytes_copied < sz) {
794 if (sz - bytes_copied > sizeof buf) {
795 chunk_sz = sizeof buf;
796 } else {
797 chunk_sz = sz - bytes_copied;
798 }
799
800 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
801 if (rc != 0) {
802 rc = BOOT_EFLASH;
803 goto done;
804 }
805
806 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
807 if (rc != 0) {
808 rc = BOOT_EFLASH;
809 goto done;
810 }
811
812 bytes_copied += chunk_sz;
813 }
814
815 rc = 0;
816
817done:
Fabio Utzige7686262017-06-28 09:26:54 -0300818 if (fap_src) {
819 flash_area_close(fap_src);
820 }
821 if (fap_dst) {
822 flash_area_close(fap_dst);
823 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800824 return rc;
825}
826
David Brown6b1b3b92017-09-19 08:59:10 -0600827#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300828static inline int
Fabio Utzig46490722017-09-04 15:34:32 -0300829boot_status_init_by_id(int flash_area_id, const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300830{
831 const struct flash_area *fap;
832 struct boot_swap_state swap_state;
833 int rc;
834
835 rc = flash_area_open(flash_area_id, &fap);
836 assert(rc == 0);
837
838 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
839 assert(rc == 0);
840
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400841 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300842 rc = boot_write_image_ok(fap);
843 assert(rc == 0);
844 }
845
Fabio Utzig46490722017-09-04 15:34:32 -0300846 rc = boot_write_swap_size(fap, bs->swap_size);
847 assert(rc == 0);
848
Fabio Utzig2473ac02017-05-02 12:45:02 -0300849 rc = boot_write_magic(fap);
850 assert(rc == 0);
851
852 flash_area_close(fap);
853
854 return 0;
855}
David Brown6b1b3b92017-09-19 08:59:10 -0600856#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -0300857
Fabio Utzig358c9352017-07-25 22:10:45 -0300858#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300859static int
860boot_erase_last_sector_by_id(int flash_area_id)
861{
862 uint8_t slot;
863 uint32_t last_sector;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300864 int rc;
865
866 switch (flash_area_id) {
867 case FLASH_AREA_IMAGE_0:
868 slot = 0;
869 break;
870 case FLASH_AREA_IMAGE_1:
871 slot = 1;
872 break;
873 default:
874 return BOOT_EFLASH;
875 }
876
Marti Bolivard3269fd2017-06-12 16:31:12 -0400877 last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300878 rc = boot_erase_sector(flash_area_id,
Marti Bolivarea088872017-06-12 17:10:49 -0400879 boot_img_sector_off(&boot_data, slot, last_sector),
Marti Bolivard3269fd2017-06-12 16:31:12 -0400880 boot_img_sector_size(&boot_data, slot, last_sector));
Fabio Utzig2473ac02017-05-02 12:45:02 -0300881 assert(rc == 0);
882
883 return rc;
884}
Fabio Utzig358c9352017-07-25 22:10:45 -0300885#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -0300886
Christopher Collins92ea77f2016-12-12 15:59:26 -0800887/**
888 * Swaps the contents of two flash regions within the two image slots.
889 *
890 * @param idx The index of the first sector in the range of
891 * sectors being swapped.
892 * @param sz The number of bytes to swap.
893 * @param bs The current boot status. This struct gets
894 * updated according to the outcome.
895 *
896 * @return 0 on success; nonzero on failure.
897 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300898#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800899static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800900boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
901{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300902 const struct flash_area *fap;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800903 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300904 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800905 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300906 uint32_t scratch_trailer_off;
907 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400908 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800909 int rc;
910
911 /* Calculate offset from start of image area. */
Marti Bolivarea088872017-06-12 17:10:49 -0400912 img_off = boot_img_sector_off(&boot_data, 0, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800913
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300914 copy_sz = sz;
Marti Bolivare10a7392017-06-14 16:20:07 -0400915 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -0400916
917 /* sz in this function is always is always sized on a multiple of the
Marti Bolivarea088872017-06-12 17:10:49 -0400918 * sector size. The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -0400919 * is to determine if we're swapping the last sector. The last sector
920 * needs special handling because it's where the trailer lives. If we're
921 * copying it, we need to use scratch to write the trailer temporarily.
922 *
923 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
924 * controls if special handling is needed (swapping last sector).
925 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400926 last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
Marti Bolivarea088872017-06-12 17:10:49 -0400927 if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300928 copy_sz -= trailer_sz;
929 }
930
Fabio Utzig2473ac02017-05-02 12:45:02 -0300931 bs->use_scratch = (bs->idx == 0 && copy_sz != sz);
932
Christopher Collins92ea77f2016-12-12 15:59:26 -0800933 if (bs->state == 0) {
934 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800935 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800936
937 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300938 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800939 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800940
Fabio Utzig2473ac02017-05-02 12:45:02 -0300941 if (bs->idx == 0) {
942 if (bs->use_scratch) {
Fabio Utzig46490722017-09-04 15:34:32 -0300943 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH, bs);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300944 } else {
945 /* Prepare the status area... here it is known that the
946 * last sector is not being used by the image data so it's
947 * safe to erase.
948 */
949 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300950 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300951
Fabio Utzig46490722017-09-04 15:34:32 -0300952 boot_status_init_by_id(FLASH_AREA_IMAGE_0, bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300953 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300954 }
955
Christopher Collins92ea77f2016-12-12 15:59:26 -0800956 bs->state = 1;
Christopher Collins4772ac42017-02-27 20:08:01 -0800957 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200958 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800959 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300960
Christopher Collins92ea77f2016-12-12 15:59:26 -0800961 if (bs->state == 1) {
962 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800963 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800964
Christopher Collins92ea77f2016-12-12 15:59:26 -0800965 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
966 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800967 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800968
Fabio Utzig2473ac02017-05-02 12:45:02 -0300969 if (bs->idx == 0 && !bs->use_scratch) {
970 /* If not all sectors of the slot are being swapped,
971 * guarantee here that only slot0 will have the state.
972 */
973 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1);
974 assert(rc == 0);
975 }
976
Christopher Collins92ea77f2016-12-12 15:59:26 -0800977 bs->state = 2;
Christopher Collins4772ac42017-02-27 20:08:01 -0800978 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200979 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800980 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300981
Christopher Collins92ea77f2016-12-12 15:59:26 -0800982 if (bs->state == 2) {
983 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800984 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800985
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300986 /* NOTE: also copy trailer from scratch (has status info) */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800987 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300988 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800989 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800990
Fabio Utzig94d998c2017-05-22 11:02:41 -0400991 if (bs->use_scratch) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300992 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
993 assert(rc == 0);
994
995 scratch_trailer_off = boot_status_off(fap);
996
997 flash_area_close(fap);
998
999 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1000 assert(rc == 0);
1001
1002 /* copy current status that is being maintained in scratch */
1003 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Marti Bolivare10a7392017-06-14 16:20:07 -04001004 scratch_trailer_off,
Fabio Utziga0bc9b52017-06-28 09:19:55 -03001005 img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -04001006 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001007 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001008
Fabio Utzig2473ac02017-05-02 12:45:02 -03001009 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1010 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001011 assert(rc == 0);
1012
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001013 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001014 rc = boot_write_image_ok(fap);
1015 assert(rc == 0);
1016 }
1017
Fabio Utzig46490722017-09-04 15:34:32 -03001018 rc = boot_write_swap_size(fap, bs->swap_size);
1019 assert(rc == 0);
1020
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001021 rc = boot_write_magic(fap);
1022 assert(rc == 0);
1023
1024 flash_area_close(fap);
1025 }
1026
Christopher Collins92ea77f2016-12-12 15:59:26 -08001027 bs->idx++;
1028 bs->state = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001029 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -08001030 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001031 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001032 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001033}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001034#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001035
1036/**
1037 * Swaps the two images in flash. If a prior copy operation was interrupted
1038 * by a system reset, this function completes that operation.
1039 *
1040 * @param bs The current boot status. This function reads
1041 * this struct to determine if it is resuming
1042 * an interrupted swap operation. This
1043 * function writes the updated status to this
1044 * function on return.
1045 *
1046 * @return 0 on success; nonzero on failure.
1047 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001048#ifdef MCUBOOT_OVERWRITE_ONLY
David Brown17609d82017-05-05 09:41:34 -06001049static int
1050boot_copy_image(struct boot_status *bs)
1051{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001052 size_t sect_count;
1053 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001054 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001055 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001056 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001057 size_t last_sector;
1058
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001059 (void)bs;
1060
Fabio Utzig13d9e352017-10-05 20:32:31 -03001061#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1062 uint32_t src_size = 0;
1063 rc = boot_read_image_size(1, boot_img_hdr(&boot_data, 1), &src_size);
1064 assert(rc == 0);
1065#endif
David Brown17609d82017-05-05 09:41:34 -06001066
1067 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
1068 BOOT_LOG_INF("Erasing slot0");
1069
Marti Bolivard3269fd2017-06-12 16:31:12 -04001070 sect_count = boot_img_num_sectors(&boot_data, 0);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001071 for (sect = 0, size = 0; sect < sect_count; sect++) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001072 this_size = boot_img_sector_size(&boot_data, 0, sect);
David Brown17609d82017-05-05 09:41:34 -06001073 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
1074 size,
1075 this_size);
1076 assert(rc == 0);
1077
1078 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001079
1080#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1081 if (size >= src_size) {
1082 break;
1083 }
1084#endif
David Brown17609d82017-05-05 09:41:34 -06001085 }
1086
Fabio Utzig6a537ee2017-09-13 17:31:44 -03001087 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%lx bytes", size);
David Brown17609d82017-05-05 09:41:34 -06001088 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
1089 0, 0, size);
1090
Fabio Utzig13d9e352017-10-05 20:32:31 -03001091 /*
1092 * Erases header and trailer. The trailer is erased because when a new
1093 * image is written without a trailer as is the case when using newt, the
1094 * trailer that was left might trigger a new upgrade.
1095 */
David Brown17609d82017-05-05 09:41:34 -06001096 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
Fabio Utzig13d9e352017-10-05 20:32:31 -03001097 boot_img_sector_off(&boot_data, 1, 0),
1098 boot_img_sector_size(&boot_data, 1, 0));
David Brown17609d82017-05-05 09:41:34 -06001099 assert(rc == 0);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001100 last_sector = boot_img_num_sectors(&boot_data, 1) - 1;
1101 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
1102 boot_img_sector_off(&boot_data, 1, last_sector),
1103 boot_img_sector_size(&boot_data, 1, last_sector));
1104 assert(rc == 0);
1105
1106 /* TODO: Perhaps verify slot 0's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001107
1108 return 0;
1109}
1110#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001111static int
1112boot_copy_image(struct boot_status *bs)
1113{
1114 uint32_t sz;
1115 int first_sector_idx;
1116 int last_sector_idx;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001117 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001118 struct image_header *hdr;
1119 uint32_t size;
1120 uint32_t copy_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001121 int rc;
1122
1123 /* FIXME: just do this if asked by user? */
1124
1125 size = copy_size = 0;
1126
Fabio Utzig46490722017-09-04 15:34:32 -03001127 if (bs->idx == 0 && bs->state == 0) {
1128 /*
1129 * No swap ever happened, so need to find the largest image which
1130 * will be used to determine the amount of sectors to swap.
1131 */
1132 hdr = boot_img_hdr(&boot_data, 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001133 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzig46490722017-09-04 15:34:32 -03001134 rc = boot_read_image_size(0, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001135 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001136 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001137
Fabio Utzig46490722017-09-04 15:34:32 -03001138 hdr = boot_img_hdr(&boot_data, 1);
1139 if (hdr->ih_magic == IMAGE_MAGIC) {
1140 rc = boot_read_image_size(1, hdr, &size);
1141 assert(rc == 0);
1142 }
1143
1144 if (size > copy_size) {
1145 copy_size = size;
1146 }
1147
1148 bs->swap_size = copy_size;
1149 } else {
1150 /*
1151 * If a swap was under way, the swap_size should already be present
1152 * in the trailer...
1153 */
1154 rc = boot_read_swap_size(&bs->swap_size);
1155 assert(rc == 0);
1156
1157 copy_size = bs->swap_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001158 }
1159
1160 size = 0;
1161 last_sector_idx = 0;
1162 while (1) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001163 size += boot_img_sector_size(&boot_data, 0, last_sector_idx);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001164 if (size >= copy_size) {
1165 break;
1166 }
1167 last_sector_idx++;
1168 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001169
1170 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001171 while (last_sector_idx >= 0) {
1172 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
1173 if (swap_idx >= bs->idx) {
1174 boot_swap_sectors(first_sector_idx, sz, bs);
1175 }
1176
1177 last_sector_idx = first_sector_idx - 1;
1178 swap_idx++;
1179 }
1180
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001181#ifdef MCUBOOT_VALIDATE_SLOT0
1182 if (boot_status_fails > 0) {
1183 BOOT_LOG_WRN("%d status write fails performing the swap", boot_status_fails);
1184 }
1185#endif
1186
Christopher Collins92ea77f2016-12-12 15:59:26 -08001187 return 0;
1188}
David Brown17609d82017-05-05 09:41:34 -06001189#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001190
1191/**
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001192 * Marks the image in slot 0 as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001193 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001194#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001195static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001196boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001197{
1198 const struct flash_area *fap;
1199 int rc;
1200
1201 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1202 if (rc != 0) {
1203 return BOOT_EFLASH;
1204 }
1205
1206 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001207 flash_area_close(fap);
1208 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001209}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001210#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001211
1212/**
1213 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1214 * the status bytes from the image revert operation don't get processed on a
1215 * subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001216 *
1217 * NOTE: image_ok is tested before writing because if there's a valid permanent
1218 * image installed on slot0 and the new image to be upgrade to has a bad sig,
1219 * image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001220 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001221#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001222static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001223boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001224{
1225 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001226 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001227 int rc;
1228
1229 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1230 if (rc != 0) {
1231 return BOOT_EFLASH;
1232 }
1233
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001234 rc = boot_read_swap_state(fap, &state);
1235 if (rc != 0) {
1236 rc = BOOT_EFLASH;
1237 goto out;
1238 }
1239
1240 if (state.image_ok == BOOT_FLAG_UNSET) {
1241 rc = boot_write_image_ok(fap);
1242 }
1243
1244out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001245 flash_area_close(fap);
1246 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001247}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001248#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001249
1250/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001251 * Performs an image swap if one is required.
1252 *
1253 * @param out_swap_type On success, the type of swap performed gets
1254 * written here.
1255 *
1256 * @return 0 on success; nonzero on failure.
1257 */
1258static int
1259boot_swap_if_needed(int *out_swap_type)
1260{
1261 struct boot_status bs;
1262 int swap_type;
1263 int rc;
1264
1265 /* Determine if we rebooted in the middle of an image swap
1266 * operation.
1267 */
1268 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001269 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001270 if (rc != 0) {
1271 return rc;
1272 }
1273
1274 /* If a partial swap was detected, complete it. */
1275 if (bs.idx != 0 || bs.state != 0) {
1276 rc = boot_copy_image(&bs);
1277 assert(rc == 0);
1278
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001279 /* NOTE: here we have finished a swap resume. The initial request
1280 * was either a TEST or PERM swap, which now after the completed
1281 * swap will be determined to be respectively REVERT (was TEST)
1282 * or NONE (was PERM).
1283 */
1284
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001285 /* Extrapolate the type of the partial swap. We need this
1286 * information to know how to mark the swap complete in flash.
1287 */
1288 swap_type = boot_previous_swap_type();
1289 } else {
1290 swap_type = boot_validated_swap_type();
1291 switch (swap_type) {
1292 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001293 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001294 case BOOT_SWAP_TYPE_REVERT:
1295 rc = boot_copy_image(&bs);
1296 assert(rc == 0);
1297 break;
1298 }
1299 }
1300
1301 *out_swap_type = swap_type;
1302 return 0;
1303}
1304
1305/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001306 * Prepares the booting process. This function moves images around in flash as
1307 * appropriate, and tells you what address to boot from.
1308 *
1309 * @param rsp On success, indicates how booting should occur.
1310 *
1311 * @return 0 on success; nonzero on failure.
1312 */
1313int
1314boot_go(struct boot_rsp *rsp)
1315{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001316 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001317 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001318 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001319 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001320 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001321
1322 /* The array of slot sectors are defined here (as opposed to file scope) so
1323 * that they don't get allocated for non-boot-loader apps. This is
1324 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001325 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001326 */
Marti Bolivarc50926f2017-06-14 09:35:40 -04001327 static boot_sector_t slot0_sectors[BOOT_MAX_IMG_SECTORS];
1328 static boot_sector_t slot1_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001329 boot_data.imgs[0].sectors = slot0_sectors;
1330 boot_data.imgs[1].sectors = slot1_sectors;
1331
Marti Bolivarc0b47912017-06-13 17:18:09 -04001332 /* Open boot_data image areas for the duration of this call. */
1333 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1334 fa_id = flash_area_id_from_image_slot(slot);
1335 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1336 assert(rc == 0);
1337 }
1338 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1339 &BOOT_SCRATCH_AREA(&boot_data));
1340 assert(rc == 0);
1341
Christopher Collins92ea77f2016-12-12 15:59:26 -08001342 /* Determine the sector layout of the image slots and scratch area. */
1343 rc = boot_read_sectors();
1344 if (rc != 0) {
Fabio Utziga1fae672018-03-30 10:52:38 -03001345 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1346 BOOT_MAX_IMG_SECTORS);
Marti Bolivarc0b47912017-06-13 17:18:09 -04001347 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001348 }
1349
1350 /* Attempt to read an image header from each slot. */
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001351 rc = boot_read_image_headers(false);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001352 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001353 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001354 }
1355
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001356 /* If the image slots aren't compatible, no swap is possible. Just boot
1357 * into slot 0.
1358 */
1359 if (boot_slots_compatible()) {
1360 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001361 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001362 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001363 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001364 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001365
1366 /*
1367 * The following states need image_ok be explicitly set after the
1368 * swap was finished to avoid a new revert.
1369 */
1370 if (swap_type == BOOT_SWAP_TYPE_REVERT || swap_type == BOOT_SWAP_TYPE_FAIL) {
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001371#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001372 rc = boot_set_image_ok();
1373 if (rc != 0) {
1374 swap_type = BOOT_SWAP_TYPE_PANIC;
1375 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001376#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001377 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001378 } else {
1379 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001380 }
1381
1382 switch (swap_type) {
1383 case BOOT_SWAP_TYPE_NONE:
1384 slot = 0;
1385 break;
1386
Fabio Utzig695d5642017-07-20 09:47:16 -03001387 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1388 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001389 case BOOT_SWAP_TYPE_REVERT:
1390 slot = 1;
David Brown52eee562017-07-05 11:25:09 -06001391 reload_headers = true;
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001392#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001393 rc = boot_set_copy_done();
1394 if (rc != 0) {
1395 swap_type = BOOT_SWAP_TYPE_PANIC;
1396 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001397#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001398 break;
1399
1400 case BOOT_SWAP_TYPE_FAIL:
1401 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1402 * try to boot into it again on the next reboot. Do this by pretending
1403 * we just reverted back to slot 0.
1404 */
1405 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001406 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001407 break;
1408
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001409 default:
Fabio Utzig695d5642017-07-20 09:47:16 -03001410 swap_type = BOOT_SWAP_TYPE_PANIC;
1411 }
1412
1413 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1414 BOOT_LOG_ERR("panic!");
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001415 assert(0);
Fabio Utzig695d5642017-07-20 09:47:16 -03001416
1417 /* Loop forever... */
1418 while (1) {}
Christopher Collins92ea77f2016-12-12 15:59:26 -08001419 }
1420
David Brown52eee562017-07-05 11:25:09 -06001421 if (reload_headers) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001422 rc = boot_read_image_headers(false);
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001423 if (rc != 0) {
1424 goto out;
1425 }
1426 /* Since headers were reloaded, it can be assumed we just performed a
1427 * swap or overwrite. Now the header info that should be used to
1428 * provide the data for the bootstrap, which previously was at Slot 1,
1429 * was updated to Slot 0.
1430 */
1431 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001432 }
1433
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001434#ifdef MCUBOOT_VALIDATE_SLOT0
David Brown554c52e2017-06-30 16:01:07 -06001435 rc = boot_validate_slot(0);
Fabio Utzig57c40f72017-12-12 21:48:30 -02001436 ASSERT(rc == 0);
David Brown554c52e2017-06-30 16:01:07 -06001437 if (rc != 0) {
1438 rc = BOOT_EBADIMAGE;
1439 goto out;
1440 }
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001441#else
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001442 /* Even if we're not re-validating slot 0, we could be booting
1443 * onto an empty flash chip. At least do a basic sanity check that
1444 * the magic number on the image is OK.
1445 */
1446 if (boot_data.imgs[0].hdr.ih_magic != IMAGE_MAGIC) {
Fabio Utzig67716012018-02-26 10:36:15 -03001447 BOOT_LOG_ERR("bad image magic 0x%lx", (unsigned long)boot_data.imgs[0].hdr.ih_magic);
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001448 rc = BOOT_EBADIMAGE;
1449 goto out;
1450 }
David Brown554c52e2017-06-30 16:01:07 -06001451#endif
1452
Christopher Collins92ea77f2016-12-12 15:59:26 -08001453 /* Always boot from the primary slot. */
Marti Bolivar428cdbf2017-05-01 22:32:42 -04001454 rsp->br_flash_dev_id = boot_img_fa_device_id(&boot_data, 0);
Marti Bolivar88f48d92017-05-01 22:30:02 -04001455 rsp->br_image_off = boot_img_slot_off(&boot_data, 0);
Marti Bolivarf804f622017-06-12 15:41:48 -04001456 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001457
Marti Bolivarc0b47912017-06-13 17:18:09 -04001458 out:
1459 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1460 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1461 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1462 }
1463 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001464}
1465
1466int
1467split_go(int loader_slot, int split_slot, void **entry)
1468{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001469 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001470 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001471 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001472 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001473 int rc;
1474
Christopher Collins92ea77f2016-12-12 15:59:26 -08001475 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1476 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001477 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001478 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001479 boot_data.imgs[loader_slot].sectors = sectors + 0;
1480 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1481
1482 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1483 rc = flash_area_open(loader_flash_id,
1484 &BOOT_IMG_AREA(&boot_data, split_slot));
1485 assert(rc == 0);
1486 split_flash_id = flash_area_id_from_image_slot(split_slot);
1487 rc = flash_area_open(split_flash_id,
1488 &BOOT_IMG_AREA(&boot_data, split_slot));
1489 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001490
1491 /* Determine the sector layout of the image slots and scratch area. */
1492 rc = boot_read_sectors();
1493 if (rc != 0) {
1494 rc = SPLIT_GO_ERR;
1495 goto done;
1496 }
1497
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001498 rc = boot_read_image_headers(true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001499 if (rc != 0) {
1500 goto done;
1501 }
1502
Christopher Collins92ea77f2016-12-12 15:59:26 -08001503 /* Don't check the bootable image flag because we could really call a
1504 * bootable or non-bootable image. Just validate that the image check
1505 * passes which is distinct from the normal check.
1506 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001507 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001508 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001509 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001510 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001511 if (rc != 0) {
1512 rc = SPLIT_GO_NON_MATCHING;
1513 goto done;
1514 }
1515
Marti Bolivarea088872017-06-12 17:10:49 -04001516 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001517 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001518 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001519 rc = SPLIT_GO_OK;
1520
1521done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001522 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1523 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001524 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001525 return rc;
1526}