blob: 56f7ab97f53ecbddf5a6b570172b81df9a4bb2f8 [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 Utzig94d998c2017-05-22 11:02:41 -0400416 bs->idx = i / BOOT_STATUS_STATE_COUNT;
417 bs->state = i % BOOT_STATUS_STATE_COUNT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800418 }
419
420 return 0;
421}
422
423/**
424 * Reads the boot status from the flash. The boot status contains
425 * the current state of an interrupted image copy operation. If the boot
426 * status is not present, or it indicates that previous copy finished,
427 * there is no operation in progress.
428 */
429static int
430boot_read_status(struct boot_status *bs)
431{
432 const struct flash_area *fap;
433 int status_loc;
434 int area_id;
435 int rc;
436
437 memset(bs, 0, sizeof *bs);
438
439 status_loc = boot_status_source();
440 switch (status_loc) {
441 case BOOT_STATUS_SOURCE_NONE:
442 return 0;
443
444 case BOOT_STATUS_SOURCE_SCRATCH:
445 area_id = FLASH_AREA_IMAGE_SCRATCH;
446 break;
447
448 case BOOT_STATUS_SOURCE_SLOT0:
449 area_id = FLASH_AREA_IMAGE_0;
450 break;
451
452 default:
453 assert(0);
454 return BOOT_EBADARGS;
455 }
456
457 rc = flash_area_open(area_id, &fap);
458 if (rc != 0) {
459 return BOOT_EFLASH;
460 }
461
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300462 return boot_read_status_bytes(fap, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800463}
464
465/**
466 * Writes the supplied boot status to the flash file system. The boot status
467 * contains the current state of an in-progress image copy operation.
468 *
469 * @param bs The boot status to write.
470 *
471 * @return 0 on success; nonzero on failure.
472 */
473int
474boot_write_status(struct boot_status *bs)
475{
476 const struct flash_area *fap;
477 uint32_t off;
478 int area_id;
479 int rc;
David Brown9d725462017-01-23 15:50:58 -0700480 uint8_t buf[8];
481 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800482
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300483 /* NOTE: The first sector copied (that is the last sector on slot) contains
484 * the trailer. Since in the last step SLOT 0 is erased, the first
485 * two status writes go to the scratch which will be copied to SLOT 0!
486 */
487
Fabio Utzig2473ac02017-05-02 12:45:02 -0300488 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800489 /* Write to scratch. */
490 area_id = FLASH_AREA_IMAGE_SCRATCH;
491 } else {
492 /* Write to slot 0. */
493 area_id = FLASH_AREA_IMAGE_0;
494 }
495
496 rc = flash_area_open(area_id, &fap);
497 if (rc != 0) {
498 rc = BOOT_EFLASH;
499 goto done;
500 }
501
502 off = boot_status_off(fap) +
503 boot_status_internal_off(bs->idx, bs->state, boot_data.write_sz);
504
David Brown9d725462017-01-23 15:50:58 -0700505 align = hal_flash_align(fap->fa_device_id);
David Brown9d725462017-01-23 15:50:58 -0700506 memset(buf, 0xFF, 8);
507 buf[0] = bs->state;
508
509 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800510 if (rc != 0) {
511 rc = BOOT_EFLASH;
512 goto done;
513 }
514
515 rc = 0;
516
517done:
518 flash_area_close(fap);
519 return rc;
520}
521
522/*
523 * Validate image hash/signature in a slot.
524 */
525static int
526boot_image_check(struct image_header *hdr, const struct flash_area *fap)
527{
David Browndb1d9d32017-01-06 11:07:54 -0700528 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800529
Christopher Collins92ea77f2016-12-12 15:59:26 -0800530 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
531 NULL, 0, NULL)) {
532 return BOOT_EBADIMAGE;
533 }
534 return 0;
535}
536
537static int
538split_image_check(struct image_header *app_hdr,
539 const struct flash_area *app_fap,
540 struct image_header *loader_hdr,
541 const struct flash_area *loader_fap)
542{
543 static void *tmpbuf;
544 uint8_t loader_hash[32];
545
546 if (!tmpbuf) {
547 tmpbuf = malloc(BOOT_TMPBUF_SZ);
548 if (!tmpbuf) {
549 return BOOT_ENOMEM;
550 }
551 }
552
553 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
554 NULL, 0, loader_hash)) {
555 return BOOT_EBADIMAGE;
556 }
557
558 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
559 loader_hash, 32, NULL)) {
560 return BOOT_EBADIMAGE;
561 }
562
563 return 0;
564}
565
566static int
David Brownd930ec62016-12-14 07:59:48 -0700567boot_validate_slot(int slot)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800568{
569 const struct flash_area *fap;
570 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300571
David Brownd930ec62016-12-14 07:59:48 -0700572 if (boot_data.imgs[slot].hdr.ih_magic == 0xffffffff ||
573 boot_data.imgs[slot].hdr.ih_flags & IMAGE_F_NON_BOOTABLE) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800574
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300575 /* No bootable image in slot; continue booting from slot 0. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800576 return -1;
577 }
578
David Brownd930ec62016-12-14 07:59:48 -0700579 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800580 if (rc != 0) {
581 return BOOT_EFLASH;
582 }
583
David Brownd930ec62016-12-14 07:59:48 -0700584 if ((boot_data.imgs[slot].hdr.ih_magic != IMAGE_MAGIC ||
David Brownb38e0442017-02-24 13:57:12 -0700585 boot_image_check(&boot_data.imgs[slot].hdr, fap) != 0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800586
David Brownb38e0442017-02-24 13:57:12 -0700587 if (slot != 0) {
588 flash_area_erase(fap, 0, fap->fa_size);
589 /* Image in slot 1 is invalid. Erase the image and
590 * continue booting from slot 0.
591 */
592 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800593 return -1;
594 }
595
596 flash_area_close(fap);
597
598 /* Image in slot 1 is valid. */
599 return 0;
600}
601
602/**
603 * Determines which swap operation to perform, if any. If it is determined
604 * that a swap operation is required, the image in the second slot is checked
605 * for validity. If the image in the second slot is invalid, it is erased, and
606 * a swap type of "none" is indicated.
607 *
608 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
609 */
610static int
611boot_validated_swap_type(void)
612{
613 int swap_type;
614 int rc;
615
616 swap_type = boot_swap_type();
617 if (swap_type == BOOT_SWAP_TYPE_NONE) {
618 /* Continue using slot 0. */
619 return BOOT_SWAP_TYPE_NONE;
620 }
621
622 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
David Brownd930ec62016-12-14 07:59:48 -0700623 rc = boot_validate_slot(1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800624 if (rc != 0) {
625 return BOOT_SWAP_TYPE_FAIL;
626 }
627
628 return swap_type;
629}
630
631/**
632 * Calculates the number of sectors the scratch area can contain. A "last"
633 * source sector is specified because images are copied backwards in flash
634 * (final index to index number 0).
635 *
636 * @param last_sector_idx The index of the last source sector
637 * (inclusive).
638 * @param out_first_sector_idx The index of the first source sector
639 * (inclusive) gets written here.
640 *
641 * @return The number of bytes comprised by the
642 * [first-sector, last-sector] range.
643 */
David Brown17609d82017-05-05 09:41:34 -0600644#ifndef BOOTUTIL_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800645static uint32_t
646boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
647{
648 uint32_t new_sz;
649 uint32_t sz;
650 int i;
651
652 sz = 0;
653
654 for (i = last_sector_idx; i >= 0; i--) {
655 new_sz = sz + boot_data.imgs[0].sectors[i].fa_size;
656 if (new_sz > boot_data.scratch_sector.fa_size) {
657 break;
658 }
659 sz = new_sz;
660 }
661
662 /* i currently refers to a sector that doesn't fit or it is -1 because all
663 * sectors have been processed. In both cases, exclude sector i.
664 */
665 *out_first_sector_idx = i + 1;
666 return sz;
667}
David Brown17609d82017-05-05 09:41:34 -0600668#endif /* not BOOTUTIL_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800669
670/**
671 * Erases a region of flash.
672 *
673 * @param flash_area_idx The ID of the flash area containing the region
674 * to erase.
675 * @param off The offset within the flash area to start the
676 * erase.
677 * @param sz The number of bytes to erase.
678 *
679 * @return 0 on success; nonzero on failure.
680 */
681static int
682boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
683{
684 const struct flash_area *fap;
685 int rc;
686
687 rc = flash_area_open(flash_area_id, &fap);
688 if (rc != 0) {
689 rc = BOOT_EFLASH;
690 goto done;
691 }
692
693 rc = flash_area_erase(fap, off, sz);
694 if (rc != 0) {
695 rc = BOOT_EFLASH;
696 goto done;
697 }
698
699 rc = 0;
700
701done:
702 flash_area_close(fap);
703 return rc;
704}
705
706/**
707 * Copies the contents of one flash region to another. You must erase the
708 * destination region prior to calling this function.
709 *
710 * @param flash_area_id_src The ID of the source flash area.
711 * @param flash_area_id_dst The ID of the destination flash area.
712 * @param off_src The offset within the source flash area to
713 * copy from.
714 * @param off_dst The offset within the destination flash area to
715 * copy to.
716 * @param sz The number of bytes to copy.
717 *
718 * @return 0 on success; nonzero on failure.
719 */
720static int
721boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
722 uint32_t off_src, uint32_t off_dst, uint32_t sz)
723{
724 const struct flash_area *fap_src;
725 const struct flash_area *fap_dst;
726 uint32_t bytes_copied;
727 int chunk_sz;
728 int rc;
729
730 static uint8_t buf[1024];
731
732 fap_src = NULL;
733 fap_dst = NULL;
734
735 rc = flash_area_open(flash_area_id_src, &fap_src);
736 if (rc != 0) {
737 rc = BOOT_EFLASH;
738 goto done;
739 }
740
741 rc = flash_area_open(flash_area_id_dst, &fap_dst);
742 if (rc != 0) {
743 rc = BOOT_EFLASH;
744 goto done;
745 }
746
747 bytes_copied = 0;
748 while (bytes_copied < sz) {
749 if (sz - bytes_copied > sizeof buf) {
750 chunk_sz = sizeof buf;
751 } else {
752 chunk_sz = sz - bytes_copied;
753 }
754
755 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
756 if (rc != 0) {
757 rc = BOOT_EFLASH;
758 goto done;
759 }
760
761 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
762 if (rc != 0) {
763 rc = BOOT_EFLASH;
764 goto done;
765 }
766
767 bytes_copied += chunk_sz;
768 }
769
770 rc = 0;
771
772done:
773 flash_area_close(fap_src);
774 flash_area_close(fap_dst);
775 return rc;
776}
777
Fabio Utzig2473ac02017-05-02 12:45:02 -0300778static inline int
779boot_status_init_by_id(int flash_area_id)
780{
781 const struct flash_area *fap;
782 struct boot_swap_state swap_state;
783 int rc;
784
785 rc = flash_area_open(flash_area_id, &fap);
786 assert(rc == 0);
787
788 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
789 assert(rc == 0);
790
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400791 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300792 rc = boot_write_image_ok(fap);
793 assert(rc == 0);
794 }
795
796 rc = boot_write_magic(fap);
797 assert(rc == 0);
798
799 flash_area_close(fap);
800
801 return 0;
802}
803
804static int
805boot_erase_last_sector_by_id(int flash_area_id)
806{
807 uint8_t slot;
808 uint32_t last_sector;
809 struct flash_area *sectors;
810 int rc;
811
812 switch (flash_area_id) {
813 case FLASH_AREA_IMAGE_0:
814 slot = 0;
815 break;
816 case FLASH_AREA_IMAGE_1:
817 slot = 1;
818 break;
819 default:
820 return BOOT_EFLASH;
821 }
822
823 last_sector = boot_data.imgs[slot].num_sectors - 1;
824 sectors = boot_data.imgs[slot].sectors;
825 rc = boot_erase_sector(flash_area_id,
826 sectors[last_sector].fa_off - sectors[0].fa_off,
827 sectors[last_sector].fa_size);
828 assert(rc == 0);
829
830 return rc;
831}
832
Christopher Collins92ea77f2016-12-12 15:59:26 -0800833/**
834 * Swaps the contents of two flash regions within the two image slots.
835 *
836 * @param idx The index of the first sector in the range of
837 * sectors being swapped.
838 * @param sz The number of bytes to swap.
839 * @param bs The current boot status. This struct gets
840 * updated according to the outcome.
841 *
842 * @return 0 on success; nonzero on failure.
843 */
David Brown17609d82017-05-05 09:41:34 -0600844#ifndef BOOTUTIL_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800845static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800846boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
847{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300848 const struct flash_area *fap;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800849 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300850 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800851 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300852 uint32_t scratch_trailer_off;
853 struct boot_swap_state swap_state;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800854 int rc;
855
856 /* Calculate offset from start of image area. */
857 img_off = boot_data.imgs[0].sectors[idx].fa_off -
858 boot_data.imgs[0].sectors[0].fa_off;
859
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300860 copy_sz = sz;
861
862 trailer_sz = boot_slots_trailer_sz(boot_data.write_sz);
863 if (boot_data.imgs[0].sectors[idx].fa_off + sz >
864 boot_data.imgs[0].sectors[boot_data.imgs[0].num_sectors - 1].fa_off) {
865 copy_sz -= trailer_sz;
866 }
867
Fabio Utzig2473ac02017-05-02 12:45:02 -0300868 bs->use_scratch = (bs->idx == 0 && copy_sz != sz);
869
Christopher Collins92ea77f2016-12-12 15:59:26 -0800870 if (bs->state == 0) {
871 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800872 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800873
874 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300875 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800876 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800877
Fabio Utzig2473ac02017-05-02 12:45:02 -0300878 if (bs->idx == 0) {
879 if (bs->use_scratch) {
880 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH);
881 } else {
882 /* Prepare the status area... here it is known that the
883 * last sector is not being used by the image data so it's
884 * safe to erase.
885 */
886 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300887 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300888
889 boot_status_init_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300890 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300891 }
892
Christopher Collins92ea77f2016-12-12 15:59:26 -0800893 bs->state = 1;
Christopher Collins4772ac42017-02-27 20:08:01 -0800894 rc = boot_write_status(bs);
895 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800896 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300897
Christopher Collins92ea77f2016-12-12 15:59:26 -0800898 if (bs->state == 1) {
899 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800900 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800901
Christopher Collins92ea77f2016-12-12 15:59:26 -0800902 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
903 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800904 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800905
Fabio Utzig2473ac02017-05-02 12:45:02 -0300906 if (bs->idx == 0 && !bs->use_scratch) {
907 /* If not all sectors of the slot are being swapped,
908 * guarantee here that only slot0 will have the state.
909 */
910 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1);
911 assert(rc == 0);
912 }
913
Christopher Collins92ea77f2016-12-12 15:59:26 -0800914 bs->state = 2;
Christopher Collins4772ac42017-02-27 20:08:01 -0800915 rc = boot_write_status(bs);
916 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800917 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300918
Christopher Collins92ea77f2016-12-12 15:59:26 -0800919 if (bs->state == 2) {
920 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800921 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800922
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300923 /* NOTE: also copy trailer from scratch (has status info) */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800924 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300925 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800926 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800927
Fabio Utzig94d998c2017-05-22 11:02:41 -0400928 if (bs->use_scratch) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300929 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
930 assert(rc == 0);
931
932 scratch_trailer_off = boot_status_off(fap);
933
934 flash_area_close(fap);
935
936 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
937 assert(rc == 0);
938
939 /* copy current status that is being maintained in scratch */
940 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
941 scratch_trailer_off,
942 img_off + copy_sz + BOOT_MAGIC_SZ,
943 BOOT_STATUS_STATE_COUNT * boot_data.write_sz);
944 assert(rc == 0);
945
Fabio Utzig2473ac02017-05-02 12:45:02 -0300946 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
947 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300948 assert(rc == 0);
949
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400950 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300951 rc = boot_write_image_ok(fap);
952 assert(rc == 0);
953 }
954
955 rc = boot_write_magic(fap);
956 assert(rc == 0);
957
958 flash_area_close(fap);
959 }
960
Christopher Collins92ea77f2016-12-12 15:59:26 -0800961 bs->idx++;
962 bs->state = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300963 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -0800964 rc = boot_write_status(bs);
965 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800966 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800967}
David Brown17609d82017-05-05 09:41:34 -0600968#endif /* not BOOTUTIL_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800969
970/**
971 * Swaps the two images in flash. If a prior copy operation was interrupted
972 * by a system reset, this function completes that operation.
973 *
974 * @param bs The current boot status. This function reads
975 * this struct to determine if it is resuming
976 * an interrupted swap operation. This
977 * function writes the updated status to this
978 * function on return.
979 *
980 * @return 0 on success; nonzero on failure.
981 */
David Brown17609d82017-05-05 09:41:34 -0600982#ifdef BOOTUTIL_OVERWRITE_ONLY
983static int
984boot_copy_image(struct boot_status *bs)
985{
986 int sect_count;
987 int sect;
988 int rc;
989 uint32_t size = 0;
990 uint32_t this_size;
991
992 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
993 BOOT_LOG_INF("Erasing slot0");
994
995 sect_count = boot_data.imgs[0].num_sectors;
996 for (sect = 0; sect < sect_count; sect++) {
997 this_size = boot_data.imgs[0].sectors[sect].fa_size;
998 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
999 size,
1000 this_size);
1001 assert(rc == 0);
1002
1003 size += this_size;
1004 }
1005
1006 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%x bytes",
1007 size);
1008 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
1009 0, 0, size);
1010
1011 /* Erase slot 1 so that we don't do the upgrade on every boot.
1012 * TODO: Perhaps verify slot 0's signature again? */
1013 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
1014 0, boot_data.imgs[1].sectors[0].fa_size);
1015 assert(rc == 0);
1016
1017 return 0;
1018}
1019#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001020static int
1021boot_copy_image(struct boot_status *bs)
1022{
1023 uint32_t sz;
1024 int first_sector_idx;
1025 int last_sector_idx;
1026 int swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001027 struct image_header *hdr;
1028 uint32_t size;
1029 uint32_t copy_size;
1030 struct image_header tmp_hdr;
1031 int rc;
1032
1033 /* FIXME: just do this if asked by user? */
1034
1035 size = copy_size = 0;
1036
1037 hdr = &boot_data.imgs[0].hdr;
1038 if (hdr->ih_magic == IMAGE_MAGIC) {
1039 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1040 }
1041
1042 hdr = &boot_data.imgs[1].hdr;
1043 if (hdr->ih_magic == IMAGE_MAGIC) {
1044 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1045 }
1046
1047 if (!size || !copy_size || size == copy_size) {
1048 rc = boot_read_image_header(2, &tmp_hdr);
1049 assert(rc == 0);
1050
1051 hdr = &tmp_hdr;
1052 if (hdr->ih_magic == IMAGE_MAGIC) {
1053 if (!size) {
1054 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1055 } else {
1056 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1057 }
1058 }
1059 }
1060
1061 if (size > copy_size) {
1062 copy_size = size;
1063 }
1064
1065 size = 0;
1066 last_sector_idx = 0;
1067 while (1) {
1068 size += boot_data.imgs[0].sectors[last_sector_idx].fa_size;
1069 if (size >= copy_size) {
1070 break;
1071 }
1072 last_sector_idx++;
1073 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001074
1075 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001076 while (last_sector_idx >= 0) {
1077 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
1078 if (swap_idx >= bs->idx) {
1079 boot_swap_sectors(first_sector_idx, sz, bs);
1080 }
1081
1082 last_sector_idx = first_sector_idx - 1;
1083 swap_idx++;
1084 }
1085
1086 return 0;
1087}
David Brown17609d82017-05-05 09:41:34 -06001088#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001089
1090/**
1091 * Marks a test image in slot 0 as fully copied.
1092 */
1093static int
1094boot_finalize_test_swap(void)
1095{
1096 const struct flash_area *fap;
1097 int rc;
1098
1099 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1100 if (rc != 0) {
1101 return BOOT_EFLASH;
1102 }
1103
1104 rc = boot_write_copy_done(fap);
1105 if (rc != 0) {
1106 return rc;
1107 }
1108
1109 return 0;
1110}
1111
1112/**
1113 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1114 * the status bytes from the image revert operation don't get processed on a
1115 * subsequent boot.
1116 */
1117static int
1118boot_finalize_revert_swap(void)
1119{
1120 const struct flash_area *fap;
1121 struct boot_swap_state state_slot0;
1122 int rc;
1123
1124 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1125 if (rc != 0) {
1126 return BOOT_EFLASH;
1127 }
1128
1129 rc = boot_read_swap_state(fap, &state_slot0);
1130 if (rc != 0) {
1131 return BOOT_EFLASH;
1132 }
1133
1134 if (state_slot0.magic == BOOT_MAGIC_UNSET) {
1135 rc = boot_write_magic(fap);
1136 if (rc != 0) {
1137 return rc;
1138 }
1139 }
1140
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001141 if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001142 rc = boot_write_copy_done(fap);
1143 if (rc != 0) {
1144 return rc;
1145 }
1146 }
1147
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001148 if (state_slot0.image_ok == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001149 rc = boot_write_image_ok(fap);
1150 if (rc != 0) {
1151 return rc;
1152 }
1153 }
1154
1155 return 0;
1156}
1157
1158/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001159 * Performs an image swap if one is required.
1160 *
1161 * @param out_swap_type On success, the type of swap performed gets
1162 * written here.
1163 *
1164 * @return 0 on success; nonzero on failure.
1165 */
1166static int
1167boot_swap_if_needed(int *out_swap_type)
1168{
1169 struct boot_status bs;
1170 int swap_type;
1171 int rc;
1172
1173 /* Determine if we rebooted in the middle of an image swap
1174 * operation.
1175 */
1176 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001177 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001178 if (rc != 0) {
1179 return rc;
1180 }
1181
1182 /* If a partial swap was detected, complete it. */
1183 if (bs.idx != 0 || bs.state != 0) {
1184 rc = boot_copy_image(&bs);
1185 assert(rc == 0);
1186
1187 /* Extrapolate the type of the partial swap. We need this
1188 * information to know how to mark the swap complete in flash.
1189 */
1190 swap_type = boot_previous_swap_type();
1191 } else {
1192 swap_type = boot_validated_swap_type();
1193 switch (swap_type) {
1194 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001195 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001196 case BOOT_SWAP_TYPE_REVERT:
1197 rc = boot_copy_image(&bs);
1198 assert(rc == 0);
1199 break;
1200 }
1201 }
1202
1203 *out_swap_type = swap_type;
1204 return 0;
1205}
1206
1207/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001208 * Prepares the booting process. This function moves images around in flash as
1209 * appropriate, and tells you what address to boot from.
1210 *
1211 * @param rsp On success, indicates how booting should occur.
1212 *
1213 * @return 0 on success; nonzero on failure.
1214 */
1215int
1216boot_go(struct boot_rsp *rsp)
1217{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001218 int swap_type;
1219 int slot;
1220 int rc;
1221
1222 /* The array of slot sectors are defined here (as opposed to file scope) so
1223 * that they don't get allocated for non-boot-loader apps. This is
1224 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001225 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001226 */
1227 static struct flash_area slot0_sectors[BOOT_MAX_IMG_SECTORS];
1228 static struct flash_area slot1_sectors[BOOT_MAX_IMG_SECTORS];
1229 boot_data.imgs[0].sectors = slot0_sectors;
1230 boot_data.imgs[1].sectors = slot1_sectors;
1231
1232 /* Determine the sector layout of the image slots and scratch area. */
1233 rc = boot_read_sectors();
1234 if (rc != 0) {
1235 return rc;
1236 }
1237
1238 /* Attempt to read an image header from each slot. */
1239 rc = boot_read_image_headers();
1240 if (rc != 0) {
1241 return rc;
1242 }
1243
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001244 /* If the image slots aren't compatible, no swap is possible. Just boot
1245 * into slot 0.
1246 */
1247 if (boot_slots_compatible()) {
1248 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001249 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001250 if (rc != 0) {
1251 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001252 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001253 } else {
1254 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001255 }
1256
1257 switch (swap_type) {
1258 case BOOT_SWAP_TYPE_NONE:
David Brownd930ec62016-12-14 07:59:48 -07001259#ifdef BOOTUTIL_VALIDATE_SLOT0
1260 rc = boot_validate_slot(0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001261 assert(rc == 0);
David Brownd930ec62016-12-14 07:59:48 -07001262 if (rc != 0) {
1263 return BOOT_EBADIMAGE;
1264 }
1265#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001266 slot = 0;
1267 break;
1268
1269 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001270 case BOOT_SWAP_TYPE_PERM:
Christopher Collins92ea77f2016-12-12 15:59:26 -08001271 slot = 1;
1272 boot_finalize_test_swap();
1273 break;
1274
1275 case BOOT_SWAP_TYPE_REVERT:
1276 slot = 1;
1277 boot_finalize_revert_swap();
1278 break;
1279
1280 case BOOT_SWAP_TYPE_FAIL:
1281 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1282 * try to boot into it again on the next reboot. Do this by pretending
1283 * we just reverted back to slot 0.
1284 */
1285 slot = 0;
1286 boot_finalize_revert_swap();
1287 break;
1288
1289 default:
1290 assert(0);
1291 slot = 0;
1292 break;
1293 }
1294
1295 /* Always boot from the primary slot. */
1296 rsp->br_flash_id = boot_data.imgs[0].sectors[0].fa_device_id;
1297 rsp->br_image_addr = boot_data.imgs[0].sectors[0].fa_off;
1298 rsp->br_hdr = &boot_data.imgs[slot].hdr;
1299
1300 return 0;
1301}
1302
1303int
1304split_go(int loader_slot, int split_slot, void **entry)
1305{
1306 const struct flash_area *loader_fap;
1307 const struct flash_area *app_fap;
1308 struct flash_area *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001309 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001310 int loader_flash_id;
1311 int app_flash_id;
1312 int rc;
1313
1314 app_fap = NULL;
1315 loader_fap = NULL;
1316
1317 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1318 if (sectors == NULL) {
1319 rc = SPLIT_GO_ERR;
1320 goto done;
1321 }
1322 boot_data.imgs[0].sectors = sectors + 0;
1323 boot_data.imgs[1].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1324
1325 /* Determine the sector layout of the image slots and scratch area. */
1326 rc = boot_read_sectors();
1327 if (rc != 0) {
1328 rc = SPLIT_GO_ERR;
1329 goto done;
1330 }
1331
1332 rc = boot_read_image_headers();
1333 if (rc != 0) {
1334 goto done;
1335 }
1336
1337 app_flash_id = flash_area_id_from_image_slot(split_slot);
1338 rc = flash_area_open(app_flash_id, &app_fap);
1339 if (rc != 0) {
1340 rc = BOOT_EFLASH;
1341 goto done;
1342 }
1343
1344 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1345 rc = flash_area_open(loader_flash_id, &loader_fap);
1346 if (rc != 0) {
1347 rc = BOOT_EFLASH;
1348 goto done;
1349 }
1350
1351 /* Don't check the bootable image flag because we could really call a
1352 * bootable or non-bootable image. Just validate that the image check
1353 * passes which is distinct from the normal check.
1354 */
1355 rc = split_image_check(&boot_data.imgs[split_slot].hdr,
1356 app_fap,
1357 &boot_data.imgs[loader_slot].hdr,
1358 loader_fap);
1359 if (rc != 0) {
1360 rc = SPLIT_GO_NON_MATCHING;
1361 goto done;
1362 }
1363
1364 entry_val = boot_data.imgs[split_slot].sectors[0].fa_off +
1365 boot_data.imgs[split_slot].hdr.ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001366 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001367 rc = SPLIT_GO_OK;
1368
1369done:
1370 free(sectors);
1371 flash_area_close(app_fap);
1372 flash_area_close(loader_fap);
1373 return rc;
1374}