blob: 1b0a1f0d525822b7041306a0b0329ddcf50cfb34 [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>
27#include <inttypes.h>
28#include <stdlib.h>
29#include <string.h>
30#include "sysflash/sysflash.h"
31#include "flash_map/flash_map.h"
32#include <hal/hal_flash.h>
33#include <os/os_malloc.h>
34#include "bootutil/bootutil.h"
35#include "bootutil/image.h"
36#include "bootutil_priv.h"
37
Marti Bolivarfd20c762017-02-07 16:52:50 -050038#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
39#include "bootutil/bootutil_log.h"
40
Christopher Collins92ea77f2016-12-12 15:59:26 -080041#define BOOT_MAX_IMG_SECTORS 120
42
43/** Number of image slots in flash; currently limited to two. */
44#define BOOT_NUM_SLOTS 2
45
46static struct {
47 struct {
48 struct image_header hdr;
49 struct flash_area *sectors;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -080050 int num_sectors;
Christopher Collins92ea77f2016-12-12 15:59:26 -080051 } imgs[BOOT_NUM_SLOTS];
52
Christopher Collins92ea77f2016-12-12 15:59:26 -080053 struct flash_area scratch_sector;
54
55 uint8_t write_sz;
56} boot_data;
57
58struct boot_status_table {
59 /**
60 * For each field, a value of 0 means "any".
61 */
62 uint8_t bst_magic_slot0;
63 uint8_t bst_magic_scratch;
64 uint8_t bst_copy_done_slot0;
65 uint8_t bst_status_source;
66};
67
68/**
69 * This set of tables maps swap state contents to boot status location.
70 * When searching for a match, these tables must be iterated in order.
71 */
72static const struct boot_status_table boot_status_tables[] = {
73 {
74 /* | slot-0 | scratch |
75 * ----------+------------+------------|
76 * magic | Good | Any |
77 * copy-done | 0x01 | N/A |
78 * ----------+------------+------------'
79 * source: none |
80 * ------------------------------------'
81 */
82 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
83 .bst_magic_scratch = 0,
84 .bst_copy_done_slot0 = 0x01,
85 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
86 },
87
88 {
89 /* | slot-0 | scratch |
90 * ----------+------------+------------|
91 * magic | Good | Any |
92 * copy-done | 0xff | N/A |
93 * ----------+------------+------------'
94 * source: slot 0 |
95 * ------------------------------------'
96 */
97 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
98 .bst_magic_scratch = 0,
99 .bst_copy_done_slot0 = 0xff,
100 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
101 },
102
103 {
104 /* | slot-0 | scratch |
105 * ----------+------------+------------|
106 * magic | Any | Good |
107 * copy-done | Any | N/A |
108 * ----------+------------+------------'
109 * source: scratch |
110 * ------------------------------------'
111 */
112 .bst_magic_slot0 = 0,
113 .bst_magic_scratch = BOOT_MAGIC_GOOD,
114 .bst_copy_done_slot0 = 0,
115 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
116 },
117
118 {
119 /* | slot-0 | scratch |
120 * ----------+------------+------------|
121 * magic | Unset | Any |
122 * copy-done | 0xff | N/A |
123 * ----------+------------+------------|
124 * source: varies |
125 * ------------------------------------+------------------------------+
126 * This represents one of two cases: |
127 * o No swaps ever (no status to read, so no harm in checking). |
128 * o Mid-revert; status in slot 0. |
129 * -------------------------------------------------------------------'
130 */
131 .bst_magic_slot0 = BOOT_MAGIC_UNSET,
132 .bst_magic_scratch = 0,
133 .bst_copy_done_slot0 = 0xff,
134 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
135 },
136};
137
138#define BOOT_STATUS_TABLES_COUNT \
139 (sizeof boot_status_tables / sizeof boot_status_tables[0])
140
141/**
142 * This table indicates the next swap type that should be performed. The first
143 * column contains the current swap type. The second column contains the swap
144 * type that should be effected after the first completes.
145 */
146static const uint8_t boot_swap_trans_table[][2] = {
147 /* From To */
148 { BOOT_SWAP_TYPE_REVERT, BOOT_SWAP_TYPE_NONE },
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800149 { BOOT_SWAP_TYPE_PERM, BOOT_SWAP_TYPE_NONE },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800150 { BOOT_SWAP_TYPE_TEST, BOOT_SWAP_TYPE_REVERT },
151};
152
153#define BOOT_SWAP_TRANS_TABLE_SIZE \
154 (sizeof boot_swap_trans_table / sizeof boot_swap_trans_table[0])
155
Marti Bolivarfd20c762017-02-07 16:52:50 -0500156#define BOOT_LOG_SWAP_STATE(area, state) \
157 BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \
158 (area), \
159 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
160 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
161 "bad"), \
162 (state)->copy_done, \
163 (state)->image_ok)
164
Christopher Collins92ea77f2016-12-12 15:59:26 -0800165/**
166 * Determines where in flash the most recent boot status is stored. The boot
167 * status is necessary for completing a swap that was interrupted by a boot
168 * loader reset.
169 *
170 * @return A BOOT_STATUS_SOURCE_[...] code indicating where * status should be read from.
171 */
172static int
173boot_status_source(void)
174{
175 const struct boot_status_table *table;
176 struct boot_swap_state state_scratch;
177 struct boot_swap_state state_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800178 int rc;
179 int i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500180 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800181
Fabio Utzig2473ac02017-05-02 12:45:02 -0300182 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800183 assert(rc == 0);
184
Fabio Utzig2473ac02017-05-02 12:45:02 -0300185 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800186 assert(rc == 0);
187
Marti Bolivarfd20c762017-02-07 16:52:50 -0500188 BOOT_LOG_SWAP_STATE("Image 0", &state_slot0);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500189 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
190
Christopher Collins92ea77f2016-12-12 15:59:26 -0800191 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300192 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800193
194 if ((table->bst_magic_slot0 == 0 ||
195 table->bst_magic_slot0 == state_slot0.magic) &&
196 (table->bst_magic_scratch == 0 ||
197 table->bst_magic_scratch == state_scratch.magic) &&
198 (table->bst_copy_done_slot0 == 0 ||
199 table->bst_copy_done_slot0 == state_slot0.copy_done)) {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500200 source = table->bst_status_source;
201 BOOT_LOG_INF("Boot source: %s",
202 source == BOOT_STATUS_SOURCE_NONE ? "none" :
203 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
204 source == BOOT_STATUS_SOURCE_SLOT0 ? "slot 0" :
205 "BUG; can't happen");
206 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800207 }
208 }
209
Marti Bolivarfd20c762017-02-07 16:52:50 -0500210 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800211 return BOOT_STATUS_SOURCE_NONE;
212}
213
214/**
215 * Calculates the type of swap that just completed.
216 */
217static int
218boot_previous_swap_type(void)
219{
220 int post_swap_type;
221 int i;
222
223 post_swap_type = boot_swap_type();
224
225 for (i = 0; i < BOOT_SWAP_TRANS_TABLE_SIZE; i++){
226 if (boot_swap_trans_table[i][1] == post_swap_type) {
227 return boot_swap_trans_table[i][0];
228 }
229 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300230
Christopher Collins92ea77f2016-12-12 15:59:26 -0800231 /* XXX: Temporary assert. */
232 assert(0);
233
234 return BOOT_SWAP_TYPE_REVERT;
235}
236
237static int
238boot_read_image_header(int slot, struct image_header *out_hdr)
239{
240 const struct flash_area *fap;
241 int area_id;
242 int rc;
243
244 area_id = flash_area_id_from_image_slot(slot);
245 rc = flash_area_open(area_id, &fap);
246 if (rc != 0) {
247 rc = BOOT_EFLASH;
248 goto done;
249 }
250
251 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
252 if (rc != 0) {
253 rc = BOOT_EFLASH;
254 goto done;
255 }
256
257 rc = 0;
258
259done:
260 flash_area_close(fap);
261 return rc;
262}
263
264static int
265boot_read_image_headers(void)
266{
267 int rc;
268 int i;
269
270 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
271 rc = boot_read_image_header(i, &boot_data.imgs[i].hdr);
272 if (rc != 0) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300273 /* If at least the first slot's header was read successfully, then
274 * the boot loader can attempt a boot. Failure to read any headers
275 * is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800276 */
277 if (i > 0) {
278 return 0;
279 } else {
280 return rc;
281 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800282 }
283 }
284
285 return 0;
286}
287
288static uint8_t
289boot_write_sz(void)
290{
291 uint8_t elem_sz;
292 uint8_t align;
293
294 /* Figure out what size to write update status update as. The size depends
295 * on what the minimum write size is for scratch area, active image slot.
296 * We need to use the bigger of those 2 values.
297 */
298 elem_sz = hal_flash_align(boot_data.imgs[0].sectors[0].fa_device_id);
299 align = hal_flash_align(boot_data.scratch_sector.fa_device_id);
300 if (align > elem_sz) {
301 elem_sz = align;
302 }
303
304 return elem_sz;
305}
306
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800307static int
308boot_slots_compatible(void)
309{
310 const struct flash_area *sector0;
311 const struct flash_area *sector1;
312 int i;
313
314 /* Ensure both image slots have identical sector layouts. */
315 if (boot_data.imgs[0].num_sectors != boot_data.imgs[1].num_sectors) {
316 return 0;
317 }
318 for (i = 0; i < boot_data.imgs[0].num_sectors; i++) {
319 sector0 = boot_data.imgs[0].sectors + i;
320 sector1 = boot_data.imgs[1].sectors + i;
321 if (sector0->fa_size != sector1->fa_size) {
322 return 0;
323 }
324 }
325
326 return 1;
327}
328
Christopher Collins92ea77f2016-12-12 15:59:26 -0800329/**
330 * Determines the sector layout of both image slots and the scratch area.
331 * This information is necessary for calculating the number of bytes to erase
332 * and copy during an image swap. The information collected during this
333 * function is used to populate the boot_data global.
334 */
335static int
336boot_read_sectors(void)
337{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800338 const struct flash_area *scratch;
339 int num_sectors_slot0;
340 int num_sectors_slot1;
341 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800342
343 num_sectors_slot0 = BOOT_MAX_IMG_SECTORS;
344 rc = flash_area_to_sectors(FLASH_AREA_IMAGE_0, &num_sectors_slot0,
345 boot_data.imgs[0].sectors);
346 if (rc != 0) {
347 return BOOT_EFLASH;
348 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800349 boot_data.imgs[0].num_sectors = num_sectors_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800350
351 num_sectors_slot1 = BOOT_MAX_IMG_SECTORS;
352 rc = flash_area_to_sectors(FLASH_AREA_IMAGE_1, &num_sectors_slot1,
353 boot_data.imgs[1].sectors);
354 if (rc != 0) {
355 return BOOT_EFLASH;
356 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800357 boot_data.imgs[1].num_sectors = num_sectors_slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800358
359 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &scratch);
360 if (rc != 0) {
361 return BOOT_EFLASH;
362 }
363 boot_data.scratch_sector = *scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800364
365 boot_data.write_sz = boot_write_sz();
366
367 return 0;
368}
369
370static uint32_t
371boot_status_internal_off(int idx, int state, int elem_sz)
372{
373 int idx_sz;
374
375 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
376
377 return idx * idx_sz + state * elem_sz;
378}
379
380/**
381 * Reads the status of a partially-completed swap, if any. This is necessary
382 * to recover in case the boot lodaer was reset in the middle of a swap
383 * operation.
384 */
385static int
386boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
387{
388 uint32_t off;
389 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300390 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800391 int found;
392 int rc;
393 int i;
394
395 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400396 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300397
Christopher Collins92ea77f2016-12-12 15:59:26 -0800398 found = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300399 for (i = 0; i < max_entries; i++) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800400 rc = flash_area_read(fap, off + i * boot_data.write_sz, &status, 1);
401 if (rc != 0) {
402 return BOOT_EFLASH;
403 }
404
405 if (status == 0xff) {
406 if (found) {
407 break;
408 }
409 } else if (!found) {
410 found = 1;
411 }
412 }
413
414 if (found) {
415 i--;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300416 /* FIXME: is this test required? */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300417 if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) {
418 bs->idx = i / BOOT_STATUS_STATE_COUNT;
419 bs->state = i % BOOT_STATUS_STATE_COUNT;
420 } else {
421 bs->idx = 0;
422 bs->state = i;
423 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800424 }
425
426 return 0;
427}
428
429/**
430 * Reads the boot status from the flash. The boot status contains
431 * the current state of an interrupted image copy operation. If the boot
432 * status is not present, or it indicates that previous copy finished,
433 * there is no operation in progress.
434 */
435static int
436boot_read_status(struct boot_status *bs)
437{
438 const struct flash_area *fap;
439 int status_loc;
440 int area_id;
441 int rc;
442
443 memset(bs, 0, sizeof *bs);
444
445 status_loc = boot_status_source();
446 switch (status_loc) {
447 case BOOT_STATUS_SOURCE_NONE:
448 return 0;
449
450 case BOOT_STATUS_SOURCE_SCRATCH:
451 area_id = FLASH_AREA_IMAGE_SCRATCH;
452 break;
453
454 case BOOT_STATUS_SOURCE_SLOT0:
455 area_id = FLASH_AREA_IMAGE_0;
456 break;
457
458 default:
459 assert(0);
460 return BOOT_EBADARGS;
461 }
462
463 rc = flash_area_open(area_id, &fap);
464 if (rc != 0) {
465 return BOOT_EFLASH;
466 }
467
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300468 return boot_read_status_bytes(fap, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800469}
470
471/**
472 * Writes the supplied boot status to the flash file system. The boot status
473 * contains the current state of an in-progress image copy operation.
474 *
475 * @param bs The boot status to write.
476 *
477 * @return 0 on success; nonzero on failure.
478 */
479int
480boot_write_status(struct boot_status *bs)
481{
482 const struct flash_area *fap;
483 uint32_t off;
484 int area_id;
485 int rc;
David Brown9d725462017-01-23 15:50:58 -0700486 uint8_t buf[8];
487 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800488
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300489 /* NOTE: The first sector copied (that is the last sector on slot) contains
490 * the trailer. Since in the last step SLOT 0 is erased, the first
491 * two status writes go to the scratch which will be copied to SLOT 0!
492 */
493
Fabio Utzig2473ac02017-05-02 12:45:02 -0300494 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800495 /* Write to scratch. */
496 area_id = FLASH_AREA_IMAGE_SCRATCH;
497 } else {
498 /* Write to slot 0. */
499 area_id = FLASH_AREA_IMAGE_0;
500 }
501
502 rc = flash_area_open(area_id, &fap);
503 if (rc != 0) {
504 rc = BOOT_EFLASH;
505 goto done;
506 }
507
508 off = boot_status_off(fap) +
509 boot_status_internal_off(bs->idx, bs->state, boot_data.write_sz);
510
David Brown9d725462017-01-23 15:50:58 -0700511 align = hal_flash_align(fap->fa_device_id);
David Brown9d725462017-01-23 15:50:58 -0700512 memset(buf, 0xFF, 8);
513 buf[0] = bs->state;
514
515 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800516 if (rc != 0) {
517 rc = BOOT_EFLASH;
518 goto done;
519 }
520
521 rc = 0;
522
523done:
524 flash_area_close(fap);
525 return rc;
526}
527
528/*
529 * Validate image hash/signature in a slot.
530 */
531static int
532boot_image_check(struct image_header *hdr, const struct flash_area *fap)
533{
David Browndb1d9d32017-01-06 11:07:54 -0700534 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800535
Christopher Collins92ea77f2016-12-12 15:59:26 -0800536 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
537 NULL, 0, NULL)) {
538 return BOOT_EBADIMAGE;
539 }
540 return 0;
541}
542
543static int
544split_image_check(struct image_header *app_hdr,
545 const struct flash_area *app_fap,
546 struct image_header *loader_hdr,
547 const struct flash_area *loader_fap)
548{
549 static void *tmpbuf;
550 uint8_t loader_hash[32];
551
552 if (!tmpbuf) {
553 tmpbuf = malloc(BOOT_TMPBUF_SZ);
554 if (!tmpbuf) {
555 return BOOT_ENOMEM;
556 }
557 }
558
559 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
560 NULL, 0, loader_hash)) {
561 return BOOT_EBADIMAGE;
562 }
563
564 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
565 loader_hash, 32, NULL)) {
566 return BOOT_EBADIMAGE;
567 }
568
569 return 0;
570}
571
572static int
David Brownd930ec62016-12-14 07:59:48 -0700573boot_validate_slot(int slot)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800574{
575 const struct flash_area *fap;
576 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300577
David Brownd930ec62016-12-14 07:59:48 -0700578 if (boot_data.imgs[slot].hdr.ih_magic == 0xffffffff ||
579 boot_data.imgs[slot].hdr.ih_flags & IMAGE_F_NON_BOOTABLE) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800580
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300581 /* No bootable image in slot; continue booting from slot 0. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800582 return -1;
583 }
584
David Brownd930ec62016-12-14 07:59:48 -0700585 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800586 if (rc != 0) {
587 return BOOT_EFLASH;
588 }
589
David Brownd930ec62016-12-14 07:59:48 -0700590 if ((boot_data.imgs[slot].hdr.ih_magic != IMAGE_MAGIC ||
David Brownb38e0442017-02-24 13:57:12 -0700591 boot_image_check(&boot_data.imgs[slot].hdr, fap) != 0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800592
David Brownb38e0442017-02-24 13:57:12 -0700593 if (slot != 0) {
594 flash_area_erase(fap, 0, fap->fa_size);
595 /* Image in slot 1 is invalid. Erase the image and
596 * continue booting from slot 0.
597 */
598 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800599 return -1;
600 }
601
602 flash_area_close(fap);
603
604 /* Image in slot 1 is valid. */
605 return 0;
606}
607
608/**
609 * Determines which swap operation to perform, if any. If it is determined
610 * that a swap operation is required, the image in the second slot is checked
611 * for validity. If the image in the second slot is invalid, it is erased, and
612 * a swap type of "none" is indicated.
613 *
614 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
615 */
616static int
617boot_validated_swap_type(void)
618{
619 int swap_type;
620 int rc;
621
622 swap_type = boot_swap_type();
623 if (swap_type == BOOT_SWAP_TYPE_NONE) {
624 /* Continue using slot 0. */
625 return BOOT_SWAP_TYPE_NONE;
626 }
627
628 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
David Brownd930ec62016-12-14 07:59:48 -0700629 rc = boot_validate_slot(1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800630 if (rc != 0) {
631 return BOOT_SWAP_TYPE_FAIL;
632 }
633
634 return swap_type;
635}
636
637/**
638 * Calculates the number of sectors the scratch area can contain. A "last"
639 * source sector is specified because images are copied backwards in flash
640 * (final index to index number 0).
641 *
642 * @param last_sector_idx The index of the last source sector
643 * (inclusive).
644 * @param out_first_sector_idx The index of the first source sector
645 * (inclusive) gets written here.
646 *
647 * @return The number of bytes comprised by the
648 * [first-sector, last-sector] range.
649 */
David Brown17609d82017-05-05 09:41:34 -0600650#ifndef BOOTUTIL_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800651static uint32_t
652boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
653{
654 uint32_t new_sz;
655 uint32_t sz;
656 int i;
657
658 sz = 0;
659
660 for (i = last_sector_idx; i >= 0; i--) {
661 new_sz = sz + boot_data.imgs[0].sectors[i].fa_size;
662 if (new_sz > boot_data.scratch_sector.fa_size) {
663 break;
664 }
665 sz = new_sz;
666 }
667
668 /* i currently refers to a sector that doesn't fit or it is -1 because all
669 * sectors have been processed. In both cases, exclude sector i.
670 */
671 *out_first_sector_idx = i + 1;
672 return sz;
673}
David Brown17609d82017-05-05 09:41:34 -0600674#endif /* not BOOTUTIL_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800675
676/**
677 * Erases a region of flash.
678 *
679 * @param flash_area_idx The ID of the flash area containing the region
680 * to erase.
681 * @param off The offset within the flash area to start the
682 * erase.
683 * @param sz The number of bytes to erase.
684 *
685 * @return 0 on success; nonzero on failure.
686 */
687static int
688boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
689{
690 const struct flash_area *fap;
691 int rc;
692
693 rc = flash_area_open(flash_area_id, &fap);
694 if (rc != 0) {
695 rc = BOOT_EFLASH;
696 goto done;
697 }
698
699 rc = flash_area_erase(fap, off, sz);
700 if (rc != 0) {
701 rc = BOOT_EFLASH;
702 goto done;
703 }
704
705 rc = 0;
706
707done:
708 flash_area_close(fap);
709 return rc;
710}
711
712/**
713 * Copies the contents of one flash region to another. You must erase the
714 * destination region prior to calling this function.
715 *
716 * @param flash_area_id_src The ID of the source flash area.
717 * @param flash_area_id_dst The ID of the destination flash area.
718 * @param off_src The offset within the source flash area to
719 * copy from.
720 * @param off_dst The offset within the destination flash area to
721 * copy to.
722 * @param sz The number of bytes to copy.
723 *
724 * @return 0 on success; nonzero on failure.
725 */
726static int
727boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
728 uint32_t off_src, uint32_t off_dst, uint32_t sz)
729{
730 const struct flash_area *fap_src;
731 const struct flash_area *fap_dst;
732 uint32_t bytes_copied;
733 int chunk_sz;
734 int rc;
735
736 static uint8_t buf[1024];
737
738 fap_src = NULL;
739 fap_dst = NULL;
740
741 rc = flash_area_open(flash_area_id_src, &fap_src);
742 if (rc != 0) {
743 rc = BOOT_EFLASH;
744 goto done;
745 }
746
747 rc = flash_area_open(flash_area_id_dst, &fap_dst);
748 if (rc != 0) {
749 rc = BOOT_EFLASH;
750 goto done;
751 }
752
753 bytes_copied = 0;
754 while (bytes_copied < sz) {
755 if (sz - bytes_copied > sizeof buf) {
756 chunk_sz = sizeof buf;
757 } else {
758 chunk_sz = sz - bytes_copied;
759 }
760
761 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
762 if (rc != 0) {
763 rc = BOOT_EFLASH;
764 goto done;
765 }
766
767 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
768 if (rc != 0) {
769 rc = BOOT_EFLASH;
770 goto done;
771 }
772
773 bytes_copied += chunk_sz;
774 }
775
776 rc = 0;
777
778done:
779 flash_area_close(fap_src);
780 flash_area_close(fap_dst);
781 return rc;
782}
783
Fabio Utzig2473ac02017-05-02 12:45:02 -0300784static inline int
785boot_status_init_by_id(int flash_area_id)
786{
787 const struct flash_area *fap;
788 struct boot_swap_state swap_state;
789 int rc;
790
791 rc = flash_area_open(flash_area_id, &fap);
792 assert(rc == 0);
793
794 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
795 assert(rc == 0);
796
Fabio Utzigba49f842017-05-22 10:52:38 -0400797 if (swap_state.image_ok == BOOT_IMAGE_OK) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300798 rc = boot_write_image_ok(fap);
799 assert(rc == 0);
800 }
801
802 rc = boot_write_magic(fap);
803 assert(rc == 0);
804
805 flash_area_close(fap);
806
807 return 0;
808}
809
810static int
811boot_erase_last_sector_by_id(int flash_area_id)
812{
813 uint8_t slot;
814 uint32_t last_sector;
815 struct flash_area *sectors;
816 int rc;
817
818 switch (flash_area_id) {
819 case FLASH_AREA_IMAGE_0:
820 slot = 0;
821 break;
822 case FLASH_AREA_IMAGE_1:
823 slot = 1;
824 break;
825 default:
826 return BOOT_EFLASH;
827 }
828
829 last_sector = boot_data.imgs[slot].num_sectors - 1;
830 sectors = boot_data.imgs[slot].sectors;
831 rc = boot_erase_sector(flash_area_id,
832 sectors[last_sector].fa_off - sectors[0].fa_off,
833 sectors[last_sector].fa_size);
834 assert(rc == 0);
835
836 return rc;
837}
838
Christopher Collins92ea77f2016-12-12 15:59:26 -0800839/**
840 * Swaps the contents of two flash regions within the two image slots.
841 *
842 * @param idx The index of the first sector in the range of
843 * sectors being swapped.
844 * @param sz The number of bytes to swap.
845 * @param bs The current boot status. This struct gets
846 * updated according to the outcome.
847 *
848 * @return 0 on success; nonzero on failure.
849 */
David Brown17609d82017-05-05 09:41:34 -0600850#ifndef BOOTUTIL_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800851static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800852boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
853{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300854 const struct flash_area *fap;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800855 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300856 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800857 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300858 uint32_t scratch_trailer_off;
859 struct boot_swap_state swap_state;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800860 int rc;
861
862 /* Calculate offset from start of image area. */
863 img_off = boot_data.imgs[0].sectors[idx].fa_off -
864 boot_data.imgs[0].sectors[0].fa_off;
865
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300866 copy_sz = sz;
867
868 trailer_sz = boot_slots_trailer_sz(boot_data.write_sz);
869 if (boot_data.imgs[0].sectors[idx].fa_off + sz >
870 boot_data.imgs[0].sectors[boot_data.imgs[0].num_sectors - 1].fa_off) {
871 copy_sz -= trailer_sz;
872 }
873
Fabio Utzig2473ac02017-05-02 12:45:02 -0300874 bs->use_scratch = (bs->idx == 0 && copy_sz != sz);
875
Christopher Collins92ea77f2016-12-12 15:59:26 -0800876 if (bs->state == 0) {
877 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800878 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800879
880 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300881 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800882 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800883
Fabio Utzig2473ac02017-05-02 12:45:02 -0300884 if (bs->idx == 0) {
885 if (bs->use_scratch) {
886 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH);
887 } else {
888 /* Prepare the status area... here it is known that the
889 * last sector is not being used by the image data so it's
890 * safe to erase.
891 */
892 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300893 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300894
895 boot_status_init_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300896 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300897 }
898
Christopher Collins92ea77f2016-12-12 15:59:26 -0800899 bs->state = 1;
Christopher Collins4772ac42017-02-27 20:08:01 -0800900 rc = boot_write_status(bs);
901 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800902 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300903
Christopher Collins92ea77f2016-12-12 15:59:26 -0800904 if (bs->state == 1) {
905 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800906 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800907
Christopher Collins92ea77f2016-12-12 15:59:26 -0800908 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
909 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800910 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800911
Fabio Utzig2473ac02017-05-02 12:45:02 -0300912 if (bs->idx == 0 && !bs->use_scratch) {
913 /* If not all sectors of the slot are being swapped,
914 * guarantee here that only slot0 will have the state.
915 */
916 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1);
917 assert(rc == 0);
918 }
919
Christopher Collins92ea77f2016-12-12 15:59:26 -0800920 bs->state = 2;
Christopher Collins4772ac42017-02-27 20:08:01 -0800921 rc = boot_write_status(bs);
922 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800923 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300924
Christopher Collins92ea77f2016-12-12 15:59:26 -0800925 if (bs->state == 2) {
926 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800927 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800928
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300929 /* NOTE: also copy trailer from scratch (has status info) */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800930 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300931 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800932 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800933
Fabio Utzig2473ac02017-05-02 12:45:02 -0300934 if (bs->idx == 0 && bs->use_scratch) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300935 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
936 assert(rc == 0);
937
938 scratch_trailer_off = boot_status_off(fap);
939
940 flash_area_close(fap);
941
942 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
943 assert(rc == 0);
944
945 /* copy current status that is being maintained in scratch */
946 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
947 scratch_trailer_off,
948 img_off + copy_sz + BOOT_MAGIC_SZ,
949 BOOT_STATUS_STATE_COUNT * boot_data.write_sz);
950 assert(rc == 0);
951
Fabio Utzig2473ac02017-05-02 12:45:02 -0300952 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
953 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300954 assert(rc == 0);
955
Fabio Utzigba49f842017-05-22 10:52:38 -0400956 if (swap_state.image_ok == BOOT_IMAGE_OK) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300957 rc = boot_write_image_ok(fap);
958 assert(rc == 0);
959 }
960
961 rc = boot_write_magic(fap);
962 assert(rc == 0);
963
964 flash_area_close(fap);
965 }
966
Christopher Collins92ea77f2016-12-12 15:59:26 -0800967 bs->idx++;
968 bs->state = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300969 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -0800970 rc = boot_write_status(bs);
971 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800972 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800973}
David Brown17609d82017-05-05 09:41:34 -0600974#endif /* not BOOTUTIL_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800975
976/**
977 * Swaps the two images in flash. If a prior copy operation was interrupted
978 * by a system reset, this function completes that operation.
979 *
980 * @param bs The current boot status. This function reads
981 * this struct to determine if it is resuming
982 * an interrupted swap operation. This
983 * function writes the updated status to this
984 * function on return.
985 *
986 * @return 0 on success; nonzero on failure.
987 */
David Brown17609d82017-05-05 09:41:34 -0600988#ifdef BOOTUTIL_OVERWRITE_ONLY
989static int
990boot_copy_image(struct boot_status *bs)
991{
992 int sect_count;
993 int sect;
994 int rc;
995 uint32_t size = 0;
996 uint32_t this_size;
997
998 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
999 BOOT_LOG_INF("Erasing slot0");
1000
1001 sect_count = boot_data.imgs[0].num_sectors;
1002 for (sect = 0; sect < sect_count; sect++) {
1003 this_size = boot_data.imgs[0].sectors[sect].fa_size;
1004 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
1005 size,
1006 this_size);
1007 assert(rc == 0);
1008
1009 size += this_size;
1010 }
1011
1012 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%x bytes",
1013 size);
1014 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
1015 0, 0, size);
1016
1017 /* Erase slot 1 so that we don't do the upgrade on every boot.
1018 * TODO: Perhaps verify slot 0's signature again? */
1019 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
1020 0, boot_data.imgs[1].sectors[0].fa_size);
1021 assert(rc == 0);
1022
1023 return 0;
1024}
1025#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001026static int
1027boot_copy_image(struct boot_status *bs)
1028{
1029 uint32_t sz;
1030 int first_sector_idx;
1031 int last_sector_idx;
1032 int swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001033 struct image_header *hdr;
1034 uint32_t size;
1035 uint32_t copy_size;
1036 struct image_header tmp_hdr;
1037 int rc;
1038
1039 /* FIXME: just do this if asked by user? */
1040
1041 size = copy_size = 0;
1042
1043 hdr = &boot_data.imgs[0].hdr;
1044 if (hdr->ih_magic == IMAGE_MAGIC) {
1045 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1046 }
1047
1048 hdr = &boot_data.imgs[1].hdr;
1049 if (hdr->ih_magic == IMAGE_MAGIC) {
1050 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1051 }
1052
1053 if (!size || !copy_size || size == copy_size) {
1054 rc = boot_read_image_header(2, &tmp_hdr);
1055 assert(rc == 0);
1056
1057 hdr = &tmp_hdr;
1058 if (hdr->ih_magic == IMAGE_MAGIC) {
1059 if (!size) {
1060 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1061 } else {
1062 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1063 }
1064 }
1065 }
1066
1067 if (size > copy_size) {
1068 copy_size = size;
1069 }
1070
1071 size = 0;
1072 last_sector_idx = 0;
1073 while (1) {
1074 size += boot_data.imgs[0].sectors[last_sector_idx].fa_size;
1075 if (size >= copy_size) {
1076 break;
1077 }
1078 last_sector_idx++;
1079 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001080
1081 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001082 while (last_sector_idx >= 0) {
1083 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
1084 if (swap_idx >= bs->idx) {
1085 boot_swap_sectors(first_sector_idx, sz, bs);
1086 }
1087
1088 last_sector_idx = first_sector_idx - 1;
1089 swap_idx++;
1090 }
1091
1092 return 0;
1093}
David Brown17609d82017-05-05 09:41:34 -06001094#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001095
1096/**
1097 * Marks a test image in slot 0 as fully copied.
1098 */
1099static int
1100boot_finalize_test_swap(void)
1101{
1102 const struct flash_area *fap;
1103 int rc;
1104
1105 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1106 if (rc != 0) {
1107 return BOOT_EFLASH;
1108 }
1109
1110 rc = boot_write_copy_done(fap);
1111 if (rc != 0) {
1112 return rc;
1113 }
1114
1115 return 0;
1116}
1117
1118/**
1119 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1120 * the status bytes from the image revert operation don't get processed on a
1121 * subsequent boot.
1122 */
1123static int
1124boot_finalize_revert_swap(void)
1125{
1126 const struct flash_area *fap;
1127 struct boot_swap_state state_slot0;
1128 int rc;
1129
1130 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1131 if (rc != 0) {
1132 return BOOT_EFLASH;
1133 }
1134
1135 rc = boot_read_swap_state(fap, &state_slot0);
1136 if (rc != 0) {
1137 return BOOT_EFLASH;
1138 }
1139
1140 if (state_slot0.magic == BOOT_MAGIC_UNSET) {
1141 rc = boot_write_magic(fap);
1142 if (rc != 0) {
1143 return rc;
1144 }
1145 }
1146
1147 if (state_slot0.copy_done == 0xff) {
1148 rc = boot_write_copy_done(fap);
1149 if (rc != 0) {
1150 return rc;
1151 }
1152 }
1153
Fabio Utzigba49f842017-05-22 10:52:38 -04001154 if (state_slot0.image_ok == BOOT_IMAGE_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001155 rc = boot_write_image_ok(fap);
1156 if (rc != 0) {
1157 return rc;
1158 }
1159 }
1160
1161 return 0;
1162}
1163
1164/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001165 * Performs an image swap if one is required.
1166 *
1167 * @param out_swap_type On success, the type of swap performed gets
1168 * written here.
1169 *
1170 * @return 0 on success; nonzero on failure.
1171 */
1172static int
1173boot_swap_if_needed(int *out_swap_type)
1174{
1175 struct boot_status bs;
1176 int swap_type;
1177 int rc;
1178
1179 /* Determine if we rebooted in the middle of an image swap
1180 * operation.
1181 */
1182 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001183 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001184 if (rc != 0) {
1185 return rc;
1186 }
1187
1188 /* If a partial swap was detected, complete it. */
1189 if (bs.idx != 0 || bs.state != 0) {
1190 rc = boot_copy_image(&bs);
1191 assert(rc == 0);
1192
1193 /* Extrapolate the type of the partial swap. We need this
1194 * information to know how to mark the swap complete in flash.
1195 */
1196 swap_type = boot_previous_swap_type();
1197 } else {
1198 swap_type = boot_validated_swap_type();
1199 switch (swap_type) {
1200 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001201 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001202 case BOOT_SWAP_TYPE_REVERT:
1203 rc = boot_copy_image(&bs);
1204 assert(rc == 0);
1205 break;
1206 }
1207 }
1208
1209 *out_swap_type = swap_type;
1210 return 0;
1211}
1212
1213/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001214 * Prepares the booting process. This function moves images around in flash as
1215 * appropriate, and tells you what address to boot from.
1216 *
1217 * @param rsp On success, indicates how booting should occur.
1218 *
1219 * @return 0 on success; nonzero on failure.
1220 */
1221int
1222boot_go(struct boot_rsp *rsp)
1223{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001224 int swap_type;
1225 int slot;
1226 int rc;
1227
1228 /* The array of slot sectors are defined here (as opposed to file scope) so
1229 * that they don't get allocated for non-boot-loader apps. This is
1230 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001231 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001232 */
1233 static struct flash_area slot0_sectors[BOOT_MAX_IMG_SECTORS];
1234 static struct flash_area slot1_sectors[BOOT_MAX_IMG_SECTORS];
1235 boot_data.imgs[0].sectors = slot0_sectors;
1236 boot_data.imgs[1].sectors = slot1_sectors;
1237
1238 /* Determine the sector layout of the image slots and scratch area. */
1239 rc = boot_read_sectors();
1240 if (rc != 0) {
1241 return rc;
1242 }
1243
1244 /* Attempt to read an image header from each slot. */
1245 rc = boot_read_image_headers();
1246 if (rc != 0) {
1247 return rc;
1248 }
1249
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001250 /* If the image slots aren't compatible, no swap is possible. Just boot
1251 * into slot 0.
1252 */
1253 if (boot_slots_compatible()) {
1254 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001255 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001256 if (rc != 0) {
1257 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001258 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001259 } else {
1260 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001261 }
1262
1263 switch (swap_type) {
1264 case BOOT_SWAP_TYPE_NONE:
David Brownd930ec62016-12-14 07:59:48 -07001265#ifdef BOOTUTIL_VALIDATE_SLOT0
1266 rc = boot_validate_slot(0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001267 assert(rc == 0);
David Brownd930ec62016-12-14 07:59:48 -07001268 if (rc != 0) {
1269 return BOOT_EBADIMAGE;
1270 }
1271#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001272 slot = 0;
1273 break;
1274
1275 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001276 case BOOT_SWAP_TYPE_PERM:
Christopher Collins92ea77f2016-12-12 15:59:26 -08001277 slot = 1;
1278 boot_finalize_test_swap();
1279 break;
1280
1281 case BOOT_SWAP_TYPE_REVERT:
1282 slot = 1;
1283 boot_finalize_revert_swap();
1284 break;
1285
1286 case BOOT_SWAP_TYPE_FAIL:
1287 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1288 * try to boot into it again on the next reboot. Do this by pretending
1289 * we just reverted back to slot 0.
1290 */
1291 slot = 0;
1292 boot_finalize_revert_swap();
1293 break;
1294
1295 default:
1296 assert(0);
1297 slot = 0;
1298 break;
1299 }
1300
1301 /* Always boot from the primary slot. */
1302 rsp->br_flash_id = boot_data.imgs[0].sectors[0].fa_device_id;
1303 rsp->br_image_addr = boot_data.imgs[0].sectors[0].fa_off;
1304 rsp->br_hdr = &boot_data.imgs[slot].hdr;
1305
1306 return 0;
1307}
1308
1309int
1310split_go(int loader_slot, int split_slot, void **entry)
1311{
1312 const struct flash_area *loader_fap;
1313 const struct flash_area *app_fap;
1314 struct flash_area *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001315 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001316 int loader_flash_id;
1317 int app_flash_id;
1318 int rc;
1319
1320 app_fap = NULL;
1321 loader_fap = NULL;
1322
1323 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1324 if (sectors == NULL) {
1325 rc = SPLIT_GO_ERR;
1326 goto done;
1327 }
1328 boot_data.imgs[0].sectors = sectors + 0;
1329 boot_data.imgs[1].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1330
1331 /* Determine the sector layout of the image slots and scratch area. */
1332 rc = boot_read_sectors();
1333 if (rc != 0) {
1334 rc = SPLIT_GO_ERR;
1335 goto done;
1336 }
1337
1338 rc = boot_read_image_headers();
1339 if (rc != 0) {
1340 goto done;
1341 }
1342
1343 app_flash_id = flash_area_id_from_image_slot(split_slot);
1344 rc = flash_area_open(app_flash_id, &app_fap);
1345 if (rc != 0) {
1346 rc = BOOT_EFLASH;
1347 goto done;
1348 }
1349
1350 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1351 rc = flash_area_open(loader_flash_id, &loader_fap);
1352 if (rc != 0) {
1353 rc = BOOT_EFLASH;
1354 goto done;
1355 }
1356
1357 /* Don't check the bootable image flag because we could really call a
1358 * bootable or non-bootable image. Just validate that the image check
1359 * passes which is distinct from the normal check.
1360 */
1361 rc = split_image_check(&boot_data.imgs[split_slot].hdr,
1362 app_fap,
1363 &boot_data.imgs[loader_slot].hdr,
1364 loader_fap);
1365 if (rc != 0) {
1366 rc = SPLIT_GO_NON_MATCHING;
1367 goto done;
1368 }
1369
1370 entry_val = boot_data.imgs[split_slot].sectors[0].fa_off +
1371 boot_data.imgs[split_slot].hdr.ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001372 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001373 rc = SPLIT_GO_OK;
1374
1375done:
1376 free(sectors);
1377 flash_area_close(app_fap);
1378 flash_area_close(loader_fap);
1379 return rc;
1380}