blob: 82d39f8c9003e04b430673b5164e1eaa3d2eacbf [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/**
21 * This file provides an interface to the boot loader. Functions defined in
22 * this file should only be called while the boot loader is running.
23 */
24
25#include <assert.h>
26#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060027#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080028#include <inttypes.h>
29#include <stdlib.h>
30#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080031#include <hal/hal_flash.h>
32#include <os/os_malloc.h>
33#include "bootutil/bootutil.h"
34#include "bootutil/image.h"
35#include "bootutil_priv.h"
36
Marti Bolivarfd20c762017-02-07 16:52:50 -050037#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
38#include "bootutil/bootutil_log.h"
39
Fabio Utzigba1fbe62017-07-21 14:01:20 -030040#ifdef MCUBOOT_MYNEWT
41#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030042#endif
43
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040044static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080045
46struct boot_status_table {
47 /**
48 * For each field, a value of 0 means "any".
49 */
50 uint8_t bst_magic_slot0;
51 uint8_t bst_magic_scratch;
52 uint8_t bst_copy_done_slot0;
53 uint8_t bst_status_source;
54};
55
56/**
57 * This set of tables maps swap state contents to boot status location.
58 * When searching for a match, these tables must be iterated in order.
59 */
60static const struct boot_status_table boot_status_tables[] = {
61 {
62 /* | slot-0 | scratch |
63 * ----------+------------+------------|
64 * magic | Good | Any |
65 * copy-done | 0x01 | N/A |
66 * ----------+------------+------------'
67 * source: none |
68 * ------------------------------------'
69 */
70 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
71 .bst_magic_scratch = 0,
72 .bst_copy_done_slot0 = 0x01,
73 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
74 },
75
76 {
77 /* | slot-0 | scratch |
78 * ----------+------------+------------|
79 * magic | Good | Any |
80 * copy-done | 0xff | N/A |
81 * ----------+------------+------------'
82 * source: slot 0 |
83 * ------------------------------------'
84 */
85 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
86 .bst_magic_scratch = 0,
87 .bst_copy_done_slot0 = 0xff,
88 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
89 },
90
91 {
92 /* | slot-0 | scratch |
93 * ----------+------------+------------|
94 * magic | Any | Good |
95 * copy-done | Any | N/A |
96 * ----------+------------+------------'
97 * source: scratch |
98 * ------------------------------------'
99 */
100 .bst_magic_slot0 = 0,
101 .bst_magic_scratch = BOOT_MAGIC_GOOD,
102 .bst_copy_done_slot0 = 0,
103 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
104 },
105
106 {
107 /* | slot-0 | scratch |
108 * ----------+------------+------------|
109 * magic | Unset | Any |
110 * copy-done | 0xff | N/A |
111 * ----------+------------+------------|
112 * source: varies |
113 * ------------------------------------+------------------------------+
114 * This represents one of two cases: |
115 * o No swaps ever (no status to read, so no harm in checking). |
116 * o Mid-revert; status in slot 0. |
117 * -------------------------------------------------------------------'
118 */
119 .bst_magic_slot0 = BOOT_MAGIC_UNSET,
120 .bst_magic_scratch = 0,
121 .bst_copy_done_slot0 = 0xff,
122 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
123 },
124};
125
126#define BOOT_STATUS_TABLES_COUNT \
127 (sizeof boot_status_tables / sizeof boot_status_tables[0])
128
Marti Bolivarfd20c762017-02-07 16:52:50 -0500129#define BOOT_LOG_SWAP_STATE(area, state) \
130 BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \
131 (area), \
132 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
133 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
134 "bad"), \
135 (state)->copy_done, \
136 (state)->image_ok)
137
Christopher Collins92ea77f2016-12-12 15:59:26 -0800138/**
139 * Determines where in flash the most recent boot status is stored. The boot
140 * status is necessary for completing a swap that was interrupted by a boot
141 * loader reset.
142 *
143 * @return A BOOT_STATUS_SOURCE_[...] code indicating where * status should be read from.
144 */
145static int
146boot_status_source(void)
147{
148 const struct boot_status_table *table;
149 struct boot_swap_state state_scratch;
150 struct boot_swap_state state_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800151 int rc;
152 int i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500153 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800154
Fabio Utzig2473ac02017-05-02 12:45:02 -0300155 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800156 assert(rc == 0);
157
Fabio Utzig2473ac02017-05-02 12:45:02 -0300158 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800159 assert(rc == 0);
160
Marti Bolivarfd20c762017-02-07 16:52:50 -0500161 BOOT_LOG_SWAP_STATE("Image 0", &state_slot0);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500162 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
163
Christopher Collins92ea77f2016-12-12 15:59:26 -0800164 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300165 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800166
167 if ((table->bst_magic_slot0 == 0 ||
168 table->bst_magic_slot0 == state_slot0.magic) &&
169 (table->bst_magic_scratch == 0 ||
170 table->bst_magic_scratch == state_scratch.magic) &&
171 (table->bst_copy_done_slot0 == 0 ||
172 table->bst_copy_done_slot0 == state_slot0.copy_done)) {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500173 source = table->bst_status_source;
174 BOOT_LOG_INF("Boot source: %s",
175 source == BOOT_STATUS_SOURCE_NONE ? "none" :
176 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
177 source == BOOT_STATUS_SOURCE_SLOT0 ? "slot 0" :
178 "BUG; can't happen");
179 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800180 }
181 }
182
Marti Bolivarfd20c762017-02-07 16:52:50 -0500183 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800184 return BOOT_STATUS_SOURCE_NONE;
185}
186
187/**
188 * Calculates the type of swap that just completed.
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300189 *
190 * This is used when a swap is interrupted by an external event. After
191 * finishing the swap operation determines what the initial request was.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800192 */
193static int
194boot_previous_swap_type(void)
195{
196 int post_swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800197
198 post_swap_type = boot_swap_type();
199
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300200 switch (post_swap_type) {
201 case BOOT_SWAP_TYPE_NONE : return BOOT_SWAP_TYPE_PERM;
202 case BOOT_SWAP_TYPE_REVERT : return BOOT_SWAP_TYPE_TEST;
203 case BOOT_SWAP_TYPE_PANIC : return BOOT_SWAP_TYPE_PANIC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800204 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300205
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300206 return BOOT_SWAP_TYPE_FAIL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800207}
208
Fabio Utzig358c9352017-07-25 22:10:45 -0300209#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800210static int
Fabio Utzigc08ed212017-06-20 19:28:36 -0300211boot_read_header_from_scratch(struct image_header *out_hdr)
212{
213 const struct flash_area *fap;
214 int rc;
215
216 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
217 if (rc != 0) {
218 return BOOT_EFLASH;
219 }
220
221 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
222 if (rc != 0) {
223 rc = BOOT_EFLASH;
224 }
225
226 flash_area_close(fap);
227 return rc;
228}
Fabio Utzig358c9352017-07-25 22:10:45 -0300229#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzigc08ed212017-06-20 19:28:36 -0300230
David Brownf5b33d82017-09-01 10:58:27 -0600231/*
232 * Compute the total size of the given image. Includes the size of
233 * the TLVs.
234 */
235static int
236boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
237{
238 const struct flash_area *fap;
239 struct image_tlv_info info;
240 int area_id;
241 int rc;
242
243 area_id = flash_area_id_from_image_slot(slot);
244 rc = flash_area_open(area_id, &fap);
245 if (rc != 0) {
246 rc = BOOT_EFLASH;
247 goto done;
248 }
249
250 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
251 &info, sizeof(info));
252 if (rc != 0) {
253 rc = BOOT_EFLASH;
254 goto done;
255 }
256 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
257 rc = BOOT_EBADIMAGE;
258 goto done;
259 }
260 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
261 rc = 0;
262
263done:
264 flash_area_close(fap);
265 return 0;
266}
267
Fabio Utzigc08ed212017-06-20 19:28:36 -0300268static int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800269boot_read_image_header(int slot, struct image_header *out_hdr)
270{
271 const struct flash_area *fap;
272 int area_id;
273 int rc;
274
275 area_id = flash_area_id_from_image_slot(slot);
276 rc = flash_area_open(area_id, &fap);
277 if (rc != 0) {
278 rc = BOOT_EFLASH;
279 goto done;
280 }
281
282 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
283 if (rc != 0) {
284 rc = BOOT_EFLASH;
285 goto done;
286 }
287
288 rc = 0;
289
290done:
291 flash_area_close(fap);
292 return rc;
293}
294
295static int
296boot_read_image_headers(void)
297{
298 int rc;
299 int i;
300
301 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400302 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800303 if (rc != 0) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300304 /* If at least the first slot's header was read successfully, then
305 * the boot loader can attempt a boot. Failure to read any headers
306 * is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800307 */
308 if (i > 0) {
309 return 0;
310 } else {
311 return rc;
312 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800313 }
314 }
315
316 return 0;
317}
318
319static uint8_t
320boot_write_sz(void)
321{
322 uint8_t elem_sz;
323 uint8_t align;
324
325 /* Figure out what size to write update status update as. The size depends
326 * on what the minimum write size is for scratch area, active image slot.
327 * We need to use the bigger of those 2 values.
328 */
Marti Bolivare2587152017-06-12 15:52:05 -0400329 elem_sz = hal_flash_align(boot_img_fa_device_id(&boot_data, 0));
330 align = hal_flash_align(boot_scratch_fa_device_id(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800331 if (align > elem_sz) {
332 elem_sz = align;
333 }
334
335 return elem_sz;
336}
337
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800338static int
339boot_slots_compatible(void)
340{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400341 size_t num_sectors_0 = boot_img_num_sectors(&boot_data, 0);
342 size_t num_sectors_1 = boot_img_num_sectors(&boot_data, 1);
343 size_t size_0, size_1;
344 size_t i;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800345
346 /* Ensure both image slots have identical sector layouts. */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400347 if (num_sectors_0 != num_sectors_1) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800348 return 0;
349 }
Marti Bolivard3269fd2017-06-12 16:31:12 -0400350 for (i = 0; i < num_sectors_0; i++) {
351 size_0 = boot_img_sector_size(&boot_data, 0, i);
352 size_1 = boot_img_sector_size(&boot_data, 1, i);
353 if (size_0 != size_1) {
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;
409 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 Utzig7ebb7c22017-04-26 10:59:31 -0300416 for (i = 0; i < max_entries; i++) {
Marti Bolivare10a7392017-06-14 16:20:07 -0400417 rc = flash_area_read(fap, off + i * BOOT_WRITE_SZ(&boot_data),
418 &status, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800419 if (rc != 0) {
420 return BOOT_EFLASH;
421 }
422
423 if (status == 0xff) {
424 if (found) {
425 break;
426 }
427 } else if (!found) {
428 found = 1;
429 }
430 }
431
432 if (found) {
433 i--;
Fabio Utzig94d998c2017-05-22 11:02:41 -0400434 bs->idx = i / BOOT_STATUS_STATE_COUNT;
435 bs->state = i % BOOT_STATUS_STATE_COUNT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800436 }
437
438 return 0;
439}
440
441/**
442 * Reads the boot status from the flash. The boot status contains
443 * the current state of an interrupted image copy operation. If the boot
444 * status is not present, or it indicates that previous copy finished,
445 * there is no operation in progress.
446 */
447static int
448boot_read_status(struct boot_status *bs)
449{
450 const struct flash_area *fap;
451 int status_loc;
452 int area_id;
453 int rc;
454
455 memset(bs, 0, sizeof *bs);
456
457 status_loc = boot_status_source();
458 switch (status_loc) {
459 case BOOT_STATUS_SOURCE_NONE:
460 return 0;
461
462 case BOOT_STATUS_SOURCE_SCRATCH:
463 area_id = FLASH_AREA_IMAGE_SCRATCH;
464 break;
465
466 case BOOT_STATUS_SOURCE_SLOT0:
467 area_id = FLASH_AREA_IMAGE_0;
468 break;
469
470 default:
471 assert(0);
472 return BOOT_EBADARGS;
473 }
474
475 rc = flash_area_open(area_id, &fap);
476 if (rc != 0) {
477 return BOOT_EFLASH;
478 }
479
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300480 return boot_read_status_bytes(fap, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800481}
482
483/**
484 * Writes the supplied boot status to the flash file system. The boot status
485 * contains the current state of an in-progress image copy operation.
486 *
487 * @param bs The boot status to write.
488 *
489 * @return 0 on success; nonzero on failure.
490 */
491int
492boot_write_status(struct boot_status *bs)
493{
494 const struct flash_area *fap;
495 uint32_t off;
496 int area_id;
497 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300498 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700499 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800500
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300501 /* NOTE: The first sector copied (that is the last sector on slot) contains
502 * the trailer. Since in the last step SLOT 0 is erased, the first
503 * two status writes go to the scratch which will be copied to SLOT 0!
504 */
505
Fabio Utzig2473ac02017-05-02 12:45:02 -0300506 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800507 /* Write to scratch. */
508 area_id = FLASH_AREA_IMAGE_SCRATCH;
509 } else {
510 /* Write to slot 0. */
511 area_id = FLASH_AREA_IMAGE_0;
512 }
513
514 rc = flash_area_open(area_id, &fap);
515 if (rc != 0) {
516 rc = BOOT_EFLASH;
517 goto done;
518 }
519
520 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400521 boot_status_internal_off(bs->idx, bs->state,
522 BOOT_WRITE_SZ(&boot_data));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800523
David Brown9d725462017-01-23 15:50:58 -0700524 align = hal_flash_align(fap->fa_device_id);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300525 memset(buf, 0xFF, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700526 buf[0] = bs->state;
527
528 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800529 if (rc != 0) {
530 rc = BOOT_EFLASH;
531 goto done;
532 }
533
534 rc = 0;
535
536done:
537 flash_area_close(fap);
538 return rc;
539}
540
541/*
542 * Validate image hash/signature in a slot.
543 */
544static int
545boot_image_check(struct image_header *hdr, const struct flash_area *fap)
546{
David Browndb1d9d32017-01-06 11:07:54 -0700547 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800548
Christopher Collins92ea77f2016-12-12 15:59:26 -0800549 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
550 NULL, 0, NULL)) {
551 return BOOT_EBADIMAGE;
552 }
553 return 0;
554}
555
556static int
557split_image_check(struct image_header *app_hdr,
558 const struct flash_area *app_fap,
559 struct image_header *loader_hdr,
560 const struct flash_area *loader_fap)
561{
562 static void *tmpbuf;
563 uint8_t loader_hash[32];
564
565 if (!tmpbuf) {
566 tmpbuf = malloc(BOOT_TMPBUF_SZ);
567 if (!tmpbuf) {
568 return BOOT_ENOMEM;
569 }
570 }
571
572 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
573 NULL, 0, loader_hash)) {
574 return BOOT_EBADIMAGE;
575 }
576
577 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
578 loader_hash, 32, NULL)) {
579 return BOOT_EBADIMAGE;
580 }
581
582 return 0;
583}
584
585static int
David Brownd930ec62016-12-14 07:59:48 -0700586boot_validate_slot(int slot)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800587{
588 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400589 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800590 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300591
Marti Bolivarf804f622017-06-12 15:41:48 -0400592 hdr = boot_img_hdr(&boot_data, slot);
593 if (hdr->ih_magic == 0xffffffff || hdr->ih_flags & IMAGE_F_NON_BOOTABLE) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300594 /* No bootable image in slot; continue booting from slot 0. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800595 return -1;
596 }
597
David Brownd930ec62016-12-14 07:59:48 -0700598 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800599 if (rc != 0) {
600 return BOOT_EFLASH;
601 }
602
Marti Bolivarf804f622017-06-12 15:41:48 -0400603 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap) != 0)) {
David Brownb38e0442017-02-24 13:57:12 -0700604 if (slot != 0) {
605 flash_area_erase(fap, 0, fap->fa_size);
606 /* Image in slot 1 is invalid. Erase the image and
607 * continue booting from slot 0.
608 */
609 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800610 return -1;
611 }
612
613 flash_area_close(fap);
614
615 /* Image in slot 1 is valid. */
616 return 0;
617}
618
619/**
620 * Determines which swap operation to perform, if any. If it is determined
621 * that a swap operation is required, the image in the second slot is checked
622 * for validity. If the image in the second slot is invalid, it is erased, and
623 * a swap type of "none" is indicated.
624 *
625 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
626 */
627static int
628boot_validated_swap_type(void)
629{
630 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800631
632 swap_type = boot_swap_type();
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300633 switch (swap_type) {
634 case BOOT_SWAP_TYPE_TEST:
635 case BOOT_SWAP_TYPE_PERM:
636 case BOOT_SWAP_TYPE_REVERT:
637 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
638 if (boot_validate_slot(1) != 0) {
639 swap_type = BOOT_SWAP_TYPE_FAIL;
640 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800641 }
642
643 return swap_type;
644}
645
646/**
647 * Calculates the number of sectors the scratch area can contain. A "last"
648 * source sector is specified because images are copied backwards in flash
649 * (final index to index number 0).
650 *
651 * @param last_sector_idx The index of the last source sector
652 * (inclusive).
653 * @param out_first_sector_idx The index of the first source sector
654 * (inclusive) gets written here.
655 *
656 * @return The number of bytes comprised by the
657 * [first-sector, last-sector] range.
658 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300659#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800660static uint32_t
661boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
662{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400663 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800664 uint32_t new_sz;
665 uint32_t sz;
666 int i;
667
668 sz = 0;
669
Marti Bolivard3269fd2017-06-12 16:31:12 -0400670 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800671 for (i = last_sector_idx; i >= 0; i--) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400672 new_sz = sz + boot_img_sector_size(&boot_data, 0, i);
673 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800674 break;
675 }
676 sz = new_sz;
677 }
678
679 /* i currently refers to a sector that doesn't fit or it is -1 because all
680 * sectors have been processed. In both cases, exclude sector i.
681 */
682 *out_first_sector_idx = i + 1;
683 return sz;
684}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300685#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800686
687/**
688 * Erases a region of flash.
689 *
690 * @param flash_area_idx The ID of the flash area containing the region
691 * to erase.
692 * @param off The offset within the flash area to start the
693 * erase.
694 * @param sz The number of bytes to erase.
695 *
696 * @return 0 on success; nonzero on failure.
697 */
698static int
699boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
700{
701 const struct flash_area *fap;
702 int rc;
703
704 rc = flash_area_open(flash_area_id, &fap);
705 if (rc != 0) {
706 rc = BOOT_EFLASH;
707 goto done;
708 }
709
710 rc = flash_area_erase(fap, off, sz);
711 if (rc != 0) {
712 rc = BOOT_EFLASH;
713 goto done;
714 }
715
716 rc = 0;
717
718done:
719 flash_area_close(fap);
720 return rc;
721}
722
723/**
724 * Copies the contents of one flash region to another. You must erase the
725 * destination region prior to calling this function.
726 *
727 * @param flash_area_id_src The ID of the source flash area.
728 * @param flash_area_id_dst The ID of the destination flash area.
729 * @param off_src The offset within the source flash area to
730 * copy from.
731 * @param off_dst The offset within the destination flash area to
732 * copy to.
733 * @param sz The number of bytes to copy.
734 *
735 * @return 0 on success; nonzero on failure.
736 */
737static int
738boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
739 uint32_t off_src, uint32_t off_dst, uint32_t sz)
740{
741 const struct flash_area *fap_src;
742 const struct flash_area *fap_dst;
743 uint32_t bytes_copied;
744 int chunk_sz;
745 int rc;
746
747 static uint8_t buf[1024];
748
749 fap_src = NULL;
750 fap_dst = NULL;
751
752 rc = flash_area_open(flash_area_id_src, &fap_src);
753 if (rc != 0) {
754 rc = BOOT_EFLASH;
755 goto done;
756 }
757
758 rc = flash_area_open(flash_area_id_dst, &fap_dst);
759 if (rc != 0) {
760 rc = BOOT_EFLASH;
761 goto done;
762 }
763
764 bytes_copied = 0;
765 while (bytes_copied < sz) {
766 if (sz - bytes_copied > sizeof buf) {
767 chunk_sz = sizeof buf;
768 } else {
769 chunk_sz = sz - bytes_copied;
770 }
771
772 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
773 if (rc != 0) {
774 rc = BOOT_EFLASH;
775 goto done;
776 }
777
778 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
779 if (rc != 0) {
780 rc = BOOT_EFLASH;
781 goto done;
782 }
783
784 bytes_copied += chunk_sz;
785 }
786
787 rc = 0;
788
789done:
Fabio Utzige7686262017-06-28 09:26:54 -0300790 if (fap_src) {
791 flash_area_close(fap_src);
792 }
793 if (fap_dst) {
794 flash_area_close(fap_dst);
795 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800796 return rc;
797}
798
Fabio Utzig2473ac02017-05-02 12:45:02 -0300799static inline int
800boot_status_init_by_id(int flash_area_id)
801{
802 const struct flash_area *fap;
803 struct boot_swap_state swap_state;
804 int rc;
805
806 rc = flash_area_open(flash_area_id, &fap);
807 assert(rc == 0);
808
809 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
810 assert(rc == 0);
811
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400812 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300813 rc = boot_write_image_ok(fap);
814 assert(rc == 0);
815 }
816
817 rc = boot_write_magic(fap);
818 assert(rc == 0);
819
820 flash_area_close(fap);
821
822 return 0;
823}
824
Fabio Utzig358c9352017-07-25 22:10:45 -0300825#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300826static int
827boot_erase_last_sector_by_id(int flash_area_id)
828{
829 uint8_t slot;
830 uint32_t last_sector;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300831 int rc;
832
833 switch (flash_area_id) {
834 case FLASH_AREA_IMAGE_0:
835 slot = 0;
836 break;
837 case FLASH_AREA_IMAGE_1:
838 slot = 1;
839 break;
840 default:
841 return BOOT_EFLASH;
842 }
843
Marti Bolivard3269fd2017-06-12 16:31:12 -0400844 last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300845 rc = boot_erase_sector(flash_area_id,
Marti Bolivarea088872017-06-12 17:10:49 -0400846 boot_img_sector_off(&boot_data, slot, last_sector),
Marti Bolivard3269fd2017-06-12 16:31:12 -0400847 boot_img_sector_size(&boot_data, slot, last_sector));
Fabio Utzig2473ac02017-05-02 12:45:02 -0300848 assert(rc == 0);
849
850 return rc;
851}
Fabio Utzig358c9352017-07-25 22:10:45 -0300852#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -0300853
Christopher Collins92ea77f2016-12-12 15:59:26 -0800854/**
855 * Swaps the contents of two flash regions within the two image slots.
856 *
857 * @param idx The index of the first sector in the range of
858 * sectors being swapped.
859 * @param sz The number of bytes to swap.
860 * @param bs The current boot status. This struct gets
861 * updated according to the outcome.
862 *
863 * @return 0 on success; nonzero on failure.
864 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300865#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800866static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800867boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
868{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300869 const struct flash_area *fap;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800870 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300871 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800872 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300873 uint32_t scratch_trailer_off;
874 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400875 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800876 int rc;
877
878 /* Calculate offset from start of image area. */
Marti Bolivarea088872017-06-12 17:10:49 -0400879 img_off = boot_img_sector_off(&boot_data, 0, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800880
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300881 copy_sz = sz;
Marti Bolivare10a7392017-06-14 16:20:07 -0400882 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -0400883
884 /* sz in this function is always is always sized on a multiple of the
Marti Bolivarea088872017-06-12 17:10:49 -0400885 * sector size. The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -0400886 * is to determine if we're swapping the last sector. The last sector
887 * needs special handling because it's where the trailer lives. If we're
888 * copying it, we need to use scratch to write the trailer temporarily.
889 *
890 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
891 * controls if special handling is needed (swapping last sector).
892 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400893 last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
Marti Bolivarea088872017-06-12 17:10:49 -0400894 if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300895 copy_sz -= trailer_sz;
896 }
897
Fabio Utzig2473ac02017-05-02 12:45:02 -0300898 bs->use_scratch = (bs->idx == 0 && copy_sz != sz);
899
Christopher Collins92ea77f2016-12-12 15:59:26 -0800900 if (bs->state == 0) {
901 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800902 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800903
904 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300905 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800906 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800907
Fabio Utzig2473ac02017-05-02 12:45:02 -0300908 if (bs->idx == 0) {
909 if (bs->use_scratch) {
910 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH);
911 } else {
912 /* Prepare the status area... here it is known that the
913 * last sector is not being used by the image data so it's
914 * safe to erase.
915 */
916 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300917 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300918
919 boot_status_init_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300920 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300921 }
922
Christopher Collins92ea77f2016-12-12 15:59:26 -0800923 bs->state = 1;
Christopher Collins4772ac42017-02-27 20:08:01 -0800924 rc = boot_write_status(bs);
925 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800926 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300927
Christopher Collins92ea77f2016-12-12 15:59:26 -0800928 if (bs->state == 1) {
929 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800930 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800931
Christopher Collins92ea77f2016-12-12 15:59:26 -0800932 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
933 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800934 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800935
Fabio Utzig2473ac02017-05-02 12:45:02 -0300936 if (bs->idx == 0 && !bs->use_scratch) {
937 /* If not all sectors of the slot are being swapped,
938 * guarantee here that only slot0 will have the state.
939 */
940 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1);
941 assert(rc == 0);
942 }
943
Christopher Collins92ea77f2016-12-12 15:59:26 -0800944 bs->state = 2;
Christopher Collins4772ac42017-02-27 20:08:01 -0800945 rc = boot_write_status(bs);
946 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800947 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300948
Christopher Collins92ea77f2016-12-12 15:59:26 -0800949 if (bs->state == 2) {
950 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800951 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800952
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300953 /* NOTE: also copy trailer from scratch (has status info) */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800954 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300955 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800956 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800957
Fabio Utzig94d998c2017-05-22 11:02:41 -0400958 if (bs->use_scratch) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300959 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
960 assert(rc == 0);
961
962 scratch_trailer_off = boot_status_off(fap);
963
964 flash_area_close(fap);
965
966 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
967 assert(rc == 0);
968
969 /* copy current status that is being maintained in scratch */
970 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Marti Bolivare10a7392017-06-14 16:20:07 -0400971 scratch_trailer_off,
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300972 img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -0400973 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300974 assert(rc == 0);
975
Fabio Utzig2473ac02017-05-02 12:45:02 -0300976 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
977 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300978 assert(rc == 0);
979
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400980 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300981 rc = boot_write_image_ok(fap);
982 assert(rc == 0);
983 }
984
985 rc = boot_write_magic(fap);
986 assert(rc == 0);
987
988 flash_area_close(fap);
989 }
990
Christopher Collins92ea77f2016-12-12 15:59:26 -0800991 bs->idx++;
992 bs->state = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300993 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -0800994 rc = boot_write_status(bs);
995 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800996 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800997}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300998#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800999
1000/**
1001 * Swaps the two images in flash. If a prior copy operation was interrupted
1002 * by a system reset, this function completes that operation.
1003 *
1004 * @param bs The current boot status. This function reads
1005 * this struct to determine if it is resuming
1006 * an interrupted swap operation. This
1007 * function writes the updated status to this
1008 * function on return.
1009 *
1010 * @return 0 on success; nonzero on failure.
1011 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001012#ifdef MCUBOOT_OVERWRITE_ONLY
David Brown17609d82017-05-05 09:41:34 -06001013static int
1014boot_copy_image(struct boot_status *bs)
1015{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001016 size_t sect_count;
1017 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001018 int rc;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001019 size_t size = 0;
1020 size_t this_size;
David Brown17609d82017-05-05 09:41:34 -06001021
1022 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
1023 BOOT_LOG_INF("Erasing slot0");
1024
Marti Bolivard3269fd2017-06-12 16:31:12 -04001025 sect_count = boot_img_num_sectors(&boot_data, 0);
David Brown17609d82017-05-05 09:41:34 -06001026 for (sect = 0; sect < sect_count; sect++) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001027 this_size = boot_img_sector_size(&boot_data, 0, sect);
David Brown17609d82017-05-05 09:41:34 -06001028 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
1029 size,
1030 this_size);
1031 assert(rc == 0);
1032
1033 size += this_size;
1034 }
1035
1036 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%x bytes",
1037 size);
1038 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
1039 0, 0, size);
1040
1041 /* Erase slot 1 so that we don't do the upgrade on every boot.
1042 * TODO: Perhaps verify slot 0's signature again? */
1043 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
Marti Bolivard3269fd2017-06-12 16:31:12 -04001044 0, boot_img_sector_size(&boot_data, 1, 0));
David Brown17609d82017-05-05 09:41:34 -06001045 assert(rc == 0);
1046
1047 return 0;
1048}
1049#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001050static int
1051boot_copy_image(struct boot_status *bs)
1052{
1053 uint32_t sz;
1054 int first_sector_idx;
1055 int last_sector_idx;
1056 int swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001057 struct image_header *hdr;
1058 uint32_t size;
1059 uint32_t copy_size;
1060 struct image_header tmp_hdr;
1061 int rc;
1062
1063 /* FIXME: just do this if asked by user? */
1064
1065 size = copy_size = 0;
1066
Marti Bolivarf804f622017-06-12 15:41:48 -04001067 hdr = boot_img_hdr(&boot_data, 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001068 if (hdr->ih_magic == IMAGE_MAGIC) {
David Brownf5b33d82017-09-01 10:58:27 -06001069 rc = boot_read_image_size(0, hdr, &copy_size);
1070 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001071 }
1072
Marti Bolivarf804f622017-06-12 15:41:48 -04001073 hdr = boot_img_hdr(&boot_data, 1);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001074 if (hdr->ih_magic == IMAGE_MAGIC) {
David Brownf5b33d82017-09-01 10:58:27 -06001075 rc = boot_read_image_size(1, hdr, &size);
1076 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001077 }
1078
1079 if (!size || !copy_size || size == copy_size) {
Fabio Utzigc08ed212017-06-20 19:28:36 -03001080 rc = boot_read_header_from_scratch(&tmp_hdr);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001081 assert(rc == 0);
1082
1083 hdr = &tmp_hdr;
1084 if (hdr->ih_magic == IMAGE_MAGIC) {
1085 if (!size) {
David Brownf5b33d82017-09-01 10:58:27 -06001086 rc = boot_read_image_size(2, hdr, &size);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001087 } else {
David Brownf5b33d82017-09-01 10:58:27 -06001088 rc = boot_read_image_size(2, hdr, &size);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001089 }
David Brownf5b33d82017-09-01 10:58:27 -06001090 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001091 }
1092 }
1093
1094 if (size > copy_size) {
1095 copy_size = size;
1096 }
1097
1098 size = 0;
1099 last_sector_idx = 0;
1100 while (1) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001101 size += boot_img_sector_size(&boot_data, 0, last_sector_idx);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001102 if (size >= copy_size) {
1103 break;
1104 }
1105 last_sector_idx++;
1106 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001107
1108 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001109 while (last_sector_idx >= 0) {
1110 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
1111 if (swap_idx >= bs->idx) {
1112 boot_swap_sectors(first_sector_idx, sz, bs);
1113 }
1114
1115 last_sector_idx = first_sector_idx - 1;
1116 swap_idx++;
1117 }
1118
1119 return 0;
1120}
David Brown17609d82017-05-05 09:41:34 -06001121#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001122
1123/**
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001124 * Marks the image in slot 0 as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001125 */
1126static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001127boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001128{
1129 const struct flash_area *fap;
1130 int rc;
1131
1132 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1133 if (rc != 0) {
1134 return BOOT_EFLASH;
1135 }
1136
1137 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001138 flash_area_close(fap);
1139 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001140}
1141
1142/**
1143 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1144 * the status bytes from the image revert operation don't get processed on a
1145 * subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001146 *
1147 * NOTE: image_ok is tested before writing because if there's a valid permanent
1148 * image installed on slot0 and the new image to be upgrade to has a bad sig,
1149 * image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001150 */
1151static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001152boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001153{
1154 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001155 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001156 int rc;
1157
1158 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1159 if (rc != 0) {
1160 return BOOT_EFLASH;
1161 }
1162
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001163 rc = boot_read_swap_state(fap, &state);
1164 if (rc != 0) {
1165 rc = BOOT_EFLASH;
1166 goto out;
1167 }
1168
1169 if (state.image_ok == BOOT_FLAG_UNSET) {
1170 rc = boot_write_image_ok(fap);
1171 }
1172
1173out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001174 flash_area_close(fap);
1175 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001176}
1177
1178/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001179 * Performs an image swap if one is required.
1180 *
1181 * @param out_swap_type On success, the type of swap performed gets
1182 * written here.
1183 *
1184 * @return 0 on success; nonzero on failure.
1185 */
1186static int
1187boot_swap_if_needed(int *out_swap_type)
1188{
1189 struct boot_status bs;
1190 int swap_type;
1191 int rc;
1192
1193 /* Determine if we rebooted in the middle of an image swap
1194 * operation.
1195 */
1196 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001197 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001198 if (rc != 0) {
1199 return rc;
1200 }
1201
1202 /* If a partial swap was detected, complete it. */
1203 if (bs.idx != 0 || bs.state != 0) {
1204 rc = boot_copy_image(&bs);
1205 assert(rc == 0);
1206
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001207 /* NOTE: here we have finished a swap resume. The initial request
1208 * was either a TEST or PERM swap, which now after the completed
1209 * swap will be determined to be respectively REVERT (was TEST)
1210 * or NONE (was PERM).
1211 */
1212
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001213 /* Extrapolate the type of the partial swap. We need this
1214 * information to know how to mark the swap complete in flash.
1215 */
1216 swap_type = boot_previous_swap_type();
1217 } else {
1218 swap_type = boot_validated_swap_type();
1219 switch (swap_type) {
1220 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001221 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001222 case BOOT_SWAP_TYPE_REVERT:
1223 rc = boot_copy_image(&bs);
1224 assert(rc == 0);
1225 break;
1226 }
1227 }
1228
1229 *out_swap_type = swap_type;
1230 return 0;
1231}
1232
1233/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001234 * Prepares the booting process. This function moves images around in flash as
1235 * appropriate, and tells you what address to boot from.
1236 *
1237 * @param rsp On success, indicates how booting should occur.
1238 *
1239 * @return 0 on success; nonzero on failure.
1240 */
1241int
1242boot_go(struct boot_rsp *rsp)
1243{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001244 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001245 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001246 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001247 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001248 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001249
1250 /* The array of slot sectors are defined here (as opposed to file scope) so
1251 * that they don't get allocated for non-boot-loader apps. This is
1252 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001253 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001254 */
Marti Bolivarc50926f2017-06-14 09:35:40 -04001255 static boot_sector_t slot0_sectors[BOOT_MAX_IMG_SECTORS];
1256 static boot_sector_t slot1_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001257 boot_data.imgs[0].sectors = slot0_sectors;
1258 boot_data.imgs[1].sectors = slot1_sectors;
1259
Marti Bolivarc0b47912017-06-13 17:18:09 -04001260 /* Open boot_data image areas for the duration of this call. */
1261 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1262 fa_id = flash_area_id_from_image_slot(slot);
1263 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1264 assert(rc == 0);
1265 }
1266 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1267 &BOOT_SCRATCH_AREA(&boot_data));
1268 assert(rc == 0);
1269
Christopher Collins92ea77f2016-12-12 15:59:26 -08001270 /* Determine the sector layout of the image slots and scratch area. */
1271 rc = boot_read_sectors();
1272 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001273 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001274 }
1275
1276 /* Attempt to read an image header from each slot. */
1277 rc = boot_read_image_headers();
1278 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001279 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001280 }
1281
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001282 /* If the image slots aren't compatible, no swap is possible. Just boot
1283 * into slot 0.
1284 */
1285 if (boot_slots_compatible()) {
1286 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001287 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001288 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001289 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001290 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001291
1292 /*
1293 * The following states need image_ok be explicitly set after the
1294 * swap was finished to avoid a new revert.
1295 */
1296 if (swap_type == BOOT_SWAP_TYPE_REVERT || swap_type == BOOT_SWAP_TYPE_FAIL) {
Fabio Utzig695d5642017-07-20 09:47:16 -03001297 rc = boot_set_image_ok();
1298 if (rc != 0) {
1299 swap_type = BOOT_SWAP_TYPE_PANIC;
1300 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001301 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001302 } else {
1303 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001304 }
1305
1306 switch (swap_type) {
1307 case BOOT_SWAP_TYPE_NONE:
1308 slot = 0;
1309 break;
1310
Fabio Utzig695d5642017-07-20 09:47:16 -03001311 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1312 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001313 case BOOT_SWAP_TYPE_REVERT:
1314 slot = 1;
David Brown52eee562017-07-05 11:25:09 -06001315 reload_headers = true;
Fabio Utzig695d5642017-07-20 09:47:16 -03001316 rc = boot_set_copy_done();
1317 if (rc != 0) {
1318 swap_type = BOOT_SWAP_TYPE_PANIC;
1319 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001320 break;
1321
1322 case BOOT_SWAP_TYPE_FAIL:
1323 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1324 * try to boot into it again on the next reboot. Do this by pretending
1325 * we just reverted back to slot 0.
1326 */
1327 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001328 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001329 break;
1330
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001331 default:
Fabio Utzig695d5642017-07-20 09:47:16 -03001332 swap_type = BOOT_SWAP_TYPE_PANIC;
1333 }
1334
1335 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1336 BOOT_LOG_ERR("panic!");
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001337 assert(0);
Fabio Utzig695d5642017-07-20 09:47:16 -03001338
1339 /* Loop forever... */
1340 while (1) {}
Christopher Collins92ea77f2016-12-12 15:59:26 -08001341 }
1342
David Brown554c52e2017-06-30 16:01:07 -06001343#ifdef MCUBOOT_VALIDATE_SLOT0
David Brown52eee562017-07-05 11:25:09 -06001344 if (reload_headers) {
1345 rc = boot_read_image_headers();
1346 if (rc != 0) {
1347 goto out;
1348 }
1349 }
1350
David Brown554c52e2017-06-30 16:01:07 -06001351 rc = boot_validate_slot(0);
1352 assert(rc == 0);
1353 if (rc != 0) {
1354 rc = BOOT_EBADIMAGE;
1355 goto out;
1356 }
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001357#else
1358 (void)reload_headers;
David Brown554c52e2017-06-30 16:01:07 -06001359#endif
1360
Christopher Collins92ea77f2016-12-12 15:59:26 -08001361 /* Always boot from the primary slot. */
Marti Bolivar428cdbf2017-05-01 22:32:42 -04001362 rsp->br_flash_dev_id = boot_img_fa_device_id(&boot_data, 0);
Marti Bolivar88f48d92017-05-01 22:30:02 -04001363 rsp->br_image_off = boot_img_slot_off(&boot_data, 0);
Marti Bolivarf804f622017-06-12 15:41:48 -04001364 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001365
Marti Bolivarc0b47912017-06-13 17:18:09 -04001366 out:
1367 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1368 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1369 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1370 }
1371 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001372}
1373
1374int
1375split_go(int loader_slot, int split_slot, void **entry)
1376{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001377 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001378 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001379 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001380 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001381 int rc;
1382
Christopher Collins92ea77f2016-12-12 15:59:26 -08001383 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1384 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001385 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001386 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001387 boot_data.imgs[loader_slot].sectors = sectors + 0;
1388 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1389
1390 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1391 rc = flash_area_open(loader_flash_id,
1392 &BOOT_IMG_AREA(&boot_data, split_slot));
1393 assert(rc == 0);
1394 split_flash_id = flash_area_id_from_image_slot(split_slot);
1395 rc = flash_area_open(split_flash_id,
1396 &BOOT_IMG_AREA(&boot_data, split_slot));
1397 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001398
1399 /* Determine the sector layout of the image slots and scratch area. */
1400 rc = boot_read_sectors();
1401 if (rc != 0) {
1402 rc = SPLIT_GO_ERR;
1403 goto done;
1404 }
1405
1406 rc = boot_read_image_headers();
1407 if (rc != 0) {
1408 goto done;
1409 }
1410
Christopher Collins92ea77f2016-12-12 15:59:26 -08001411 /* Don't check the bootable image flag because we could really call a
1412 * bootable or non-bootable image. Just validate that the image check
1413 * passes which is distinct from the normal check.
1414 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001415 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001416 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001417 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001418 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001419 if (rc != 0) {
1420 rc = SPLIT_GO_NON_MATCHING;
1421 goto done;
1422 }
1423
Marti Bolivarea088872017-06-12 17:10:49 -04001424 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001425 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001426 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001427 rc = SPLIT_GO_OK;
1428
1429done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001430 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1431 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001432 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001433 return rc;
1434}