blob: 782ab6cd6ea0e1bfabeed21980fb3d460cf917fb [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);
396
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300397 max_entries = BOOT_STATUS_STATE_COUNT;
398 if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) {
399 max_entries *= BOOT_STATUS_MAX_ENTRIES;
400 }
401
Christopher Collins92ea77f2016-12-12 15:59:26 -0800402 found = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300403 for (i = 0; i < max_entries; i++) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800404 rc = flash_area_read(fap, off + i * boot_data.write_sz, &status, 1);
405 if (rc != 0) {
406 return BOOT_EFLASH;
407 }
408
409 if (status == 0xff) {
410 if (found) {
411 break;
412 }
413 } else if (!found) {
414 found = 1;
415 }
416 }
417
418 if (found) {
419 i--;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300420 /* FIXME: is this test required? */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300421 if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) {
422 bs->idx = i / BOOT_STATUS_STATE_COUNT;
423 bs->state = i % BOOT_STATUS_STATE_COUNT;
424 } else {
425 bs->idx = 0;
426 bs->state = i;
427 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800428 }
429
430 return 0;
431}
432
433/**
434 * Reads the boot status from the flash. The boot status contains
435 * the current state of an interrupted image copy operation. If the boot
436 * status is not present, or it indicates that previous copy finished,
437 * there is no operation in progress.
438 */
439static int
440boot_read_status(struct boot_status *bs)
441{
442 const struct flash_area *fap;
443 int status_loc;
444 int area_id;
445 int rc;
446
447 memset(bs, 0, sizeof *bs);
448
449 status_loc = boot_status_source();
450 switch (status_loc) {
451 case BOOT_STATUS_SOURCE_NONE:
452 return 0;
453
454 case BOOT_STATUS_SOURCE_SCRATCH:
455 area_id = FLASH_AREA_IMAGE_SCRATCH;
456 break;
457
458 case BOOT_STATUS_SOURCE_SLOT0:
459 area_id = FLASH_AREA_IMAGE_0;
460 break;
461
462 default:
463 assert(0);
464 return BOOT_EBADARGS;
465 }
466
467 rc = flash_area_open(area_id, &fap);
468 if (rc != 0) {
469 return BOOT_EFLASH;
470 }
471
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300472 return boot_read_status_bytes(fap, bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800473}
474
475/**
476 * Writes the supplied boot status to the flash file system. The boot status
477 * contains the current state of an in-progress image copy operation.
478 *
479 * @param bs The boot status to write.
480 *
481 * @return 0 on success; nonzero on failure.
482 */
483int
484boot_write_status(struct boot_status *bs)
485{
486 const struct flash_area *fap;
487 uint32_t off;
488 int area_id;
489 int rc;
David Brown9d725462017-01-23 15:50:58 -0700490 uint8_t buf[8];
491 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800492
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300493 /* NOTE: The first sector copied (that is the last sector on slot) contains
494 * the trailer. Since in the last step SLOT 0 is erased, the first
495 * two status writes go to the scratch which will be copied to SLOT 0!
496 */
497
Fabio Utzig2473ac02017-05-02 12:45:02 -0300498 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800499 /* Write to scratch. */
500 area_id = FLASH_AREA_IMAGE_SCRATCH;
501 } else {
502 /* Write to slot 0. */
503 area_id = FLASH_AREA_IMAGE_0;
504 }
505
506 rc = flash_area_open(area_id, &fap);
507 if (rc != 0) {
508 rc = BOOT_EFLASH;
509 goto done;
510 }
511
512 off = boot_status_off(fap) +
513 boot_status_internal_off(bs->idx, bs->state, boot_data.write_sz);
514
David Brown9d725462017-01-23 15:50:58 -0700515 align = hal_flash_align(fap->fa_device_id);
David Brown9d725462017-01-23 15:50:58 -0700516 memset(buf, 0xFF, 8);
517 buf[0] = bs->state;
518
519 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800520 if (rc != 0) {
521 rc = BOOT_EFLASH;
522 goto done;
523 }
524
525 rc = 0;
526
527done:
528 flash_area_close(fap);
529 return rc;
530}
531
532/*
533 * Validate image hash/signature in a slot.
534 */
535static int
536boot_image_check(struct image_header *hdr, const struct flash_area *fap)
537{
David Browndb1d9d32017-01-06 11:07:54 -0700538 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800539
Christopher Collins92ea77f2016-12-12 15:59:26 -0800540 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
541 NULL, 0, NULL)) {
542 return BOOT_EBADIMAGE;
543 }
544 return 0;
545}
546
547static int
548split_image_check(struct image_header *app_hdr,
549 const struct flash_area *app_fap,
550 struct image_header *loader_hdr,
551 const struct flash_area *loader_fap)
552{
553 static void *tmpbuf;
554 uint8_t loader_hash[32];
555
556 if (!tmpbuf) {
557 tmpbuf = malloc(BOOT_TMPBUF_SZ);
558 if (!tmpbuf) {
559 return BOOT_ENOMEM;
560 }
561 }
562
563 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
564 NULL, 0, loader_hash)) {
565 return BOOT_EBADIMAGE;
566 }
567
568 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
569 loader_hash, 32, NULL)) {
570 return BOOT_EBADIMAGE;
571 }
572
573 return 0;
574}
575
576static int
David Brownd930ec62016-12-14 07:59:48 -0700577boot_validate_slot(int slot)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800578{
579 const struct flash_area *fap;
580 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300581
David Brownd930ec62016-12-14 07:59:48 -0700582 if (boot_data.imgs[slot].hdr.ih_magic == 0xffffffff ||
583 boot_data.imgs[slot].hdr.ih_flags & IMAGE_F_NON_BOOTABLE) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800584
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300585 /* No bootable image in slot; continue booting from slot 0. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800586 return -1;
587 }
588
David Brownd930ec62016-12-14 07:59:48 -0700589 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800590 if (rc != 0) {
591 return BOOT_EFLASH;
592 }
593
David Brownd930ec62016-12-14 07:59:48 -0700594 if ((boot_data.imgs[slot].hdr.ih_magic != IMAGE_MAGIC ||
David Brownb38e0442017-02-24 13:57:12 -0700595 boot_image_check(&boot_data.imgs[slot].hdr, fap) != 0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800596
David Brownb38e0442017-02-24 13:57:12 -0700597 if (slot != 0) {
598 flash_area_erase(fap, 0, fap->fa_size);
599 /* Image in slot 1 is invalid. Erase the image and
600 * continue booting from slot 0.
601 */
602 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800603 return -1;
604 }
605
606 flash_area_close(fap);
607
608 /* Image in slot 1 is valid. */
609 return 0;
610}
611
612/**
613 * Determines which swap operation to perform, if any. If it is determined
614 * that a swap operation is required, the image in the second slot is checked
615 * for validity. If the image in the second slot is invalid, it is erased, and
616 * a swap type of "none" is indicated.
617 *
618 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
619 */
620static int
621boot_validated_swap_type(void)
622{
623 int swap_type;
624 int rc;
625
626 swap_type = boot_swap_type();
627 if (swap_type == BOOT_SWAP_TYPE_NONE) {
628 /* Continue using slot 0. */
629 return BOOT_SWAP_TYPE_NONE;
630 }
631
632 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
David Brownd930ec62016-12-14 07:59:48 -0700633 rc = boot_validate_slot(1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800634 if (rc != 0) {
635 return BOOT_SWAP_TYPE_FAIL;
636 }
637
638 return swap_type;
639}
640
641/**
642 * Calculates the number of sectors the scratch area can contain. A "last"
643 * source sector is specified because images are copied backwards in flash
644 * (final index to index number 0).
645 *
646 * @param last_sector_idx The index of the last source sector
647 * (inclusive).
648 * @param out_first_sector_idx The index of the first source sector
649 * (inclusive) gets written here.
650 *
651 * @return The number of bytes comprised by the
652 * [first-sector, last-sector] range.
653 */
David Brown17609d82017-05-05 09:41:34 -0600654#ifndef BOOTUTIL_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800655static uint32_t
656boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
657{
658 uint32_t new_sz;
659 uint32_t sz;
660 int i;
661
662 sz = 0;
663
664 for (i = last_sector_idx; i >= 0; i--) {
665 new_sz = sz + boot_data.imgs[0].sectors[i].fa_size;
666 if (new_sz > boot_data.scratch_sector.fa_size) {
667 break;
668 }
669 sz = new_sz;
670 }
671
672 /* i currently refers to a sector that doesn't fit or it is -1 because all
673 * sectors have been processed. In both cases, exclude sector i.
674 */
675 *out_first_sector_idx = i + 1;
676 return sz;
677}
David Brown17609d82017-05-05 09:41:34 -0600678#endif /* not BOOTUTIL_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800679
680/**
681 * Erases a region of flash.
682 *
683 * @param flash_area_idx The ID of the flash area containing the region
684 * to erase.
685 * @param off The offset within the flash area to start the
686 * erase.
687 * @param sz The number of bytes to erase.
688 *
689 * @return 0 on success; nonzero on failure.
690 */
691static int
692boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
693{
694 const struct flash_area *fap;
695 int rc;
696
697 rc = flash_area_open(flash_area_id, &fap);
698 if (rc != 0) {
699 rc = BOOT_EFLASH;
700 goto done;
701 }
702
703 rc = flash_area_erase(fap, off, sz);
704 if (rc != 0) {
705 rc = BOOT_EFLASH;
706 goto done;
707 }
708
709 rc = 0;
710
711done:
712 flash_area_close(fap);
713 return rc;
714}
715
716/**
717 * Copies the contents of one flash region to another. You must erase the
718 * destination region prior to calling this function.
719 *
720 * @param flash_area_id_src The ID of the source flash area.
721 * @param flash_area_id_dst The ID of the destination flash area.
722 * @param off_src The offset within the source flash area to
723 * copy from.
724 * @param off_dst The offset within the destination flash area to
725 * copy to.
726 * @param sz The number of bytes to copy.
727 *
728 * @return 0 on success; nonzero on failure.
729 */
730static int
731boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
732 uint32_t off_src, uint32_t off_dst, uint32_t sz)
733{
734 const struct flash_area *fap_src;
735 const struct flash_area *fap_dst;
736 uint32_t bytes_copied;
737 int chunk_sz;
738 int rc;
739
740 static uint8_t buf[1024];
741
742 fap_src = NULL;
743 fap_dst = NULL;
744
745 rc = flash_area_open(flash_area_id_src, &fap_src);
746 if (rc != 0) {
747 rc = BOOT_EFLASH;
748 goto done;
749 }
750
751 rc = flash_area_open(flash_area_id_dst, &fap_dst);
752 if (rc != 0) {
753 rc = BOOT_EFLASH;
754 goto done;
755 }
756
757 bytes_copied = 0;
758 while (bytes_copied < sz) {
759 if (sz - bytes_copied > sizeof buf) {
760 chunk_sz = sizeof buf;
761 } else {
762 chunk_sz = sz - bytes_copied;
763 }
764
765 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
766 if (rc != 0) {
767 rc = BOOT_EFLASH;
768 goto done;
769 }
770
771 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
772 if (rc != 0) {
773 rc = BOOT_EFLASH;
774 goto done;
775 }
776
777 bytes_copied += chunk_sz;
778 }
779
780 rc = 0;
781
782done:
783 flash_area_close(fap_src);
784 flash_area_close(fap_dst);
785 return rc;
786}
787
Fabio Utzig2473ac02017-05-02 12:45:02 -0300788static inline int
789boot_status_init_by_id(int flash_area_id)
790{
791 const struct flash_area *fap;
792 struct boot_swap_state swap_state;
793 int rc;
794
795 rc = flash_area_open(flash_area_id, &fap);
796 assert(rc == 0);
797
798 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
799 assert(rc == 0);
800
801 if (swap_state.image_ok == 0x01) {
802 rc = boot_write_image_ok(fap);
803 assert(rc == 0);
804 }
805
806 rc = boot_write_magic(fap);
807 assert(rc == 0);
808
809 flash_area_close(fap);
810
811 return 0;
812}
813
814static int
815boot_erase_last_sector_by_id(int flash_area_id)
816{
817 uint8_t slot;
818 uint32_t last_sector;
819 struct flash_area *sectors;
820 int rc;
821
822 switch (flash_area_id) {
823 case FLASH_AREA_IMAGE_0:
824 slot = 0;
825 break;
826 case FLASH_AREA_IMAGE_1:
827 slot = 1;
828 break;
829 default:
830 return BOOT_EFLASH;
831 }
832
833 last_sector = boot_data.imgs[slot].num_sectors - 1;
834 sectors = boot_data.imgs[slot].sectors;
835 rc = boot_erase_sector(flash_area_id,
836 sectors[last_sector].fa_off - sectors[0].fa_off,
837 sectors[last_sector].fa_size);
838 assert(rc == 0);
839
840 return rc;
841}
842
Christopher Collins92ea77f2016-12-12 15:59:26 -0800843/**
844 * Swaps the contents of two flash regions within the two image slots.
845 *
846 * @param idx The index of the first sector in the range of
847 * sectors being swapped.
848 * @param sz The number of bytes to swap.
849 * @param bs The current boot status. This struct gets
850 * updated according to the outcome.
851 *
852 * @return 0 on success; nonzero on failure.
853 */
David Brown17609d82017-05-05 09:41:34 -0600854#ifndef BOOTUTIL_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800855static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800856boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
857{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300858 const struct flash_area *fap;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800859 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300860 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800861 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300862 uint32_t scratch_trailer_off;
863 struct boot_swap_state swap_state;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800864 int rc;
865
866 /* Calculate offset from start of image area. */
867 img_off = boot_data.imgs[0].sectors[idx].fa_off -
868 boot_data.imgs[0].sectors[0].fa_off;
869
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300870 copy_sz = sz;
871
872 trailer_sz = boot_slots_trailer_sz(boot_data.write_sz);
873 if (boot_data.imgs[0].sectors[idx].fa_off + sz >
874 boot_data.imgs[0].sectors[boot_data.imgs[0].num_sectors - 1].fa_off) {
875 copy_sz -= trailer_sz;
876 }
877
Fabio Utzig2473ac02017-05-02 12:45:02 -0300878 bs->use_scratch = (bs->idx == 0 && copy_sz != sz);
879
Christopher Collins92ea77f2016-12-12 15:59:26 -0800880 if (bs->state == 0) {
881 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800882 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800883
884 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300885 img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800886 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800887
Fabio Utzig2473ac02017-05-02 12:45:02 -0300888 if (bs->idx == 0) {
889 if (bs->use_scratch) {
890 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH);
891 } else {
892 /* Prepare the status area... here it is known that the
893 * last sector is not being used by the image data so it's
894 * safe to erase.
895 */
896 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300897 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300898
899 boot_status_init_by_id(FLASH_AREA_IMAGE_0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300900 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300901 }
902
Christopher Collins92ea77f2016-12-12 15:59:26 -0800903 bs->state = 1;
Christopher Collins4772ac42017-02-27 20:08:01 -0800904 rc = boot_write_status(bs);
905 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800906 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300907
Christopher Collins92ea77f2016-12-12 15:59:26 -0800908 if (bs->state == 1) {
909 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800910 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800911
Christopher Collins92ea77f2016-12-12 15:59:26 -0800912 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
913 img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800914 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800915
Fabio Utzig2473ac02017-05-02 12:45:02 -0300916 if (bs->idx == 0 && !bs->use_scratch) {
917 /* If not all sectors of the slot are being swapped,
918 * guarantee here that only slot0 will have the state.
919 */
920 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1);
921 assert(rc == 0);
922 }
923
Christopher Collins92ea77f2016-12-12 15:59:26 -0800924 bs->state = 2;
Christopher Collins4772ac42017-02-27 20:08:01 -0800925 rc = boot_write_status(bs);
926 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800927 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300928
Christopher Collins92ea77f2016-12-12 15:59:26 -0800929 if (bs->state == 2) {
930 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800931 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800932
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300933 /* NOTE: also copy trailer from scratch (has status info) */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800934 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300935 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800936 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800937
Fabio Utzig2473ac02017-05-02 12:45:02 -0300938 if (bs->idx == 0 && bs->use_scratch) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300939 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
940 assert(rc == 0);
941
942 scratch_trailer_off = boot_status_off(fap);
943
944 flash_area_close(fap);
945
946 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
947 assert(rc == 0);
948
949 /* copy current status that is being maintained in scratch */
950 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
951 scratch_trailer_off,
952 img_off + copy_sz + BOOT_MAGIC_SZ,
953 BOOT_STATUS_STATE_COUNT * boot_data.write_sz);
954 assert(rc == 0);
955
Fabio Utzig2473ac02017-05-02 12:45:02 -0300956 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
957 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300958 assert(rc == 0);
959
960 if (swap_state.image_ok == 0x01) {
961 rc = boot_write_image_ok(fap);
962 assert(rc == 0);
963 }
964
965 rc = boot_write_magic(fap);
966 assert(rc == 0);
967
968 flash_area_close(fap);
969 }
970
Christopher Collins92ea77f2016-12-12 15:59:26 -0800971 bs->idx++;
972 bs->state = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300973 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -0800974 rc = boot_write_status(bs);
975 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800976 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800977}
David Brown17609d82017-05-05 09:41:34 -0600978#endif /* not BOOTUTIL_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800979
980/**
981 * Swaps the two images in flash. If a prior copy operation was interrupted
982 * by a system reset, this function completes that operation.
983 *
984 * @param bs The current boot status. This function reads
985 * this struct to determine if it is resuming
986 * an interrupted swap operation. This
987 * function writes the updated status to this
988 * function on return.
989 *
990 * @return 0 on success; nonzero on failure.
991 */
David Brown17609d82017-05-05 09:41:34 -0600992#ifdef BOOTUTIL_OVERWRITE_ONLY
993static int
994boot_copy_image(struct boot_status *bs)
995{
996 int sect_count;
997 int sect;
998 int rc;
999 uint32_t size = 0;
1000 uint32_t this_size;
1001
1002 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
1003 BOOT_LOG_INF("Erasing slot0");
1004
1005 sect_count = boot_data.imgs[0].num_sectors;
1006 for (sect = 0; sect < sect_count; sect++) {
1007 this_size = boot_data.imgs[0].sectors[sect].fa_size;
1008 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
1009 size,
1010 this_size);
1011 assert(rc == 0);
1012
1013 size += this_size;
1014 }
1015
1016 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%x bytes",
1017 size);
1018 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
1019 0, 0, size);
1020
1021 /* Erase slot 1 so that we don't do the upgrade on every boot.
1022 * TODO: Perhaps verify slot 0's signature again? */
1023 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
1024 0, boot_data.imgs[1].sectors[0].fa_size);
1025 assert(rc == 0);
1026
1027 return 0;
1028}
1029#else
Christopher Collins92ea77f2016-12-12 15:59:26 -08001030static int
1031boot_copy_image(struct boot_status *bs)
1032{
1033 uint32_t sz;
1034 int first_sector_idx;
1035 int last_sector_idx;
1036 int swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001037 struct image_header *hdr;
1038 uint32_t size;
1039 uint32_t copy_size;
1040 struct image_header tmp_hdr;
1041 int rc;
1042
1043 /* FIXME: just do this if asked by user? */
1044
1045 size = copy_size = 0;
1046
1047 hdr = &boot_data.imgs[0].hdr;
1048 if (hdr->ih_magic == IMAGE_MAGIC) {
1049 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1050 }
1051
1052 hdr = &boot_data.imgs[1].hdr;
1053 if (hdr->ih_magic == IMAGE_MAGIC) {
1054 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1055 }
1056
1057 if (!size || !copy_size || size == copy_size) {
1058 rc = boot_read_image_header(2, &tmp_hdr);
1059 assert(rc == 0);
1060
1061 hdr = &tmp_hdr;
1062 if (hdr->ih_magic == IMAGE_MAGIC) {
1063 if (!size) {
1064 size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1065 } else {
1066 copy_size = hdr->ih_hdr_size + hdr->ih_img_size + hdr->ih_tlv_size;
1067 }
1068 }
1069 }
1070
1071 if (size > copy_size) {
1072 copy_size = size;
1073 }
1074
1075 size = 0;
1076 last_sector_idx = 0;
1077 while (1) {
1078 size += boot_data.imgs[0].sectors[last_sector_idx].fa_size;
1079 if (size >= copy_size) {
1080 break;
1081 }
1082 last_sector_idx++;
1083 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001084
1085 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001086 while (last_sector_idx >= 0) {
1087 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
1088 if (swap_idx >= bs->idx) {
1089 boot_swap_sectors(first_sector_idx, sz, bs);
1090 }
1091
1092 last_sector_idx = first_sector_idx - 1;
1093 swap_idx++;
1094 }
1095
1096 return 0;
1097}
David Brown17609d82017-05-05 09:41:34 -06001098#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001099
1100/**
1101 * Marks a test image in slot 0 as fully copied.
1102 */
1103static int
1104boot_finalize_test_swap(void)
1105{
1106 const struct flash_area *fap;
1107 int rc;
1108
1109 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1110 if (rc != 0) {
1111 return BOOT_EFLASH;
1112 }
1113
1114 rc = boot_write_copy_done(fap);
1115 if (rc != 0) {
1116 return rc;
1117 }
1118
1119 return 0;
1120}
1121
1122/**
1123 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1124 * the status bytes from the image revert operation don't get processed on a
1125 * subsequent boot.
1126 */
1127static int
1128boot_finalize_revert_swap(void)
1129{
1130 const struct flash_area *fap;
1131 struct boot_swap_state state_slot0;
1132 int rc;
1133
1134 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1135 if (rc != 0) {
1136 return BOOT_EFLASH;
1137 }
1138
1139 rc = boot_read_swap_state(fap, &state_slot0);
1140 if (rc != 0) {
1141 return BOOT_EFLASH;
1142 }
1143
1144 if (state_slot0.magic == BOOT_MAGIC_UNSET) {
1145 rc = boot_write_magic(fap);
1146 if (rc != 0) {
1147 return rc;
1148 }
1149 }
1150
1151 if (state_slot0.copy_done == 0xff) {
1152 rc = boot_write_copy_done(fap);
1153 if (rc != 0) {
1154 return rc;
1155 }
1156 }
1157
1158 if (state_slot0.image_ok == 0xff) {
1159 rc = boot_write_image_ok(fap);
1160 if (rc != 0) {
1161 return rc;
1162 }
1163 }
1164
1165 return 0;
1166}
1167
1168/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001169 * Performs an image swap if one is required.
1170 *
1171 * @param out_swap_type On success, the type of swap performed gets
1172 * written here.
1173 *
1174 * @return 0 on success; nonzero on failure.
1175 */
1176static int
1177boot_swap_if_needed(int *out_swap_type)
1178{
1179 struct boot_status bs;
1180 int swap_type;
1181 int rc;
1182
1183 /* Determine if we rebooted in the middle of an image swap
1184 * operation.
1185 */
1186 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001187 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001188 if (rc != 0) {
1189 return rc;
1190 }
1191
1192 /* If a partial swap was detected, complete it. */
1193 if (bs.idx != 0 || bs.state != 0) {
1194 rc = boot_copy_image(&bs);
1195 assert(rc == 0);
1196
1197 /* Extrapolate the type of the partial swap. We need this
1198 * information to know how to mark the swap complete in flash.
1199 */
1200 swap_type = boot_previous_swap_type();
1201 } else {
1202 swap_type = boot_validated_swap_type();
1203 switch (swap_type) {
1204 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001205 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001206 case BOOT_SWAP_TYPE_REVERT:
1207 rc = boot_copy_image(&bs);
1208 assert(rc == 0);
1209 break;
1210 }
1211 }
1212
1213 *out_swap_type = swap_type;
1214 return 0;
1215}
1216
1217/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001218 * Prepares the booting process. This function moves images around in flash as
1219 * appropriate, and tells you what address to boot from.
1220 *
1221 * @param rsp On success, indicates how booting should occur.
1222 *
1223 * @return 0 on success; nonzero on failure.
1224 */
1225int
1226boot_go(struct boot_rsp *rsp)
1227{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001228 int swap_type;
1229 int slot;
1230 int rc;
1231
1232 /* The array of slot sectors are defined here (as opposed to file scope) so
1233 * that they don't get allocated for non-boot-loader apps. This is
1234 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001235 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001236 */
1237 static struct flash_area slot0_sectors[BOOT_MAX_IMG_SECTORS];
1238 static struct flash_area slot1_sectors[BOOT_MAX_IMG_SECTORS];
1239 boot_data.imgs[0].sectors = slot0_sectors;
1240 boot_data.imgs[1].sectors = slot1_sectors;
1241
1242 /* Determine the sector layout of the image slots and scratch area. */
1243 rc = boot_read_sectors();
1244 if (rc != 0) {
1245 return rc;
1246 }
1247
1248 /* Attempt to read an image header from each slot. */
1249 rc = boot_read_image_headers();
1250 if (rc != 0) {
1251 return rc;
1252 }
1253
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001254 /* If the image slots aren't compatible, no swap is possible. Just boot
1255 * into slot 0.
1256 */
1257 if (boot_slots_compatible()) {
1258 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001259 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001260 if (rc != 0) {
1261 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001262 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001263 } else {
1264 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001265 }
1266
1267 switch (swap_type) {
1268 case BOOT_SWAP_TYPE_NONE:
David Brownd930ec62016-12-14 07:59:48 -07001269#ifdef BOOTUTIL_VALIDATE_SLOT0
1270 rc = boot_validate_slot(0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001271 assert(rc == 0);
David Brownd930ec62016-12-14 07:59:48 -07001272 if (rc != 0) {
1273 return BOOT_EBADIMAGE;
1274 }
1275#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001276 slot = 0;
1277 break;
1278
1279 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001280 case BOOT_SWAP_TYPE_PERM:
Christopher Collins92ea77f2016-12-12 15:59:26 -08001281 slot = 1;
1282 boot_finalize_test_swap();
1283 break;
1284
1285 case BOOT_SWAP_TYPE_REVERT:
1286 slot = 1;
1287 boot_finalize_revert_swap();
1288 break;
1289
1290 case BOOT_SWAP_TYPE_FAIL:
1291 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1292 * try to boot into it again on the next reboot. Do this by pretending
1293 * we just reverted back to slot 0.
1294 */
1295 slot = 0;
1296 boot_finalize_revert_swap();
1297 break;
1298
1299 default:
1300 assert(0);
1301 slot = 0;
1302 break;
1303 }
1304
1305 /* Always boot from the primary slot. */
1306 rsp->br_flash_id = boot_data.imgs[0].sectors[0].fa_device_id;
1307 rsp->br_image_addr = boot_data.imgs[0].sectors[0].fa_off;
1308 rsp->br_hdr = &boot_data.imgs[slot].hdr;
1309
1310 return 0;
1311}
1312
1313int
1314split_go(int loader_slot, int split_slot, void **entry)
1315{
1316 const struct flash_area *loader_fap;
1317 const struct flash_area *app_fap;
1318 struct flash_area *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001319 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001320 int loader_flash_id;
1321 int app_flash_id;
1322 int rc;
1323
1324 app_fap = NULL;
1325 loader_fap = NULL;
1326
1327 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1328 if (sectors == NULL) {
1329 rc = SPLIT_GO_ERR;
1330 goto done;
1331 }
1332 boot_data.imgs[0].sectors = sectors + 0;
1333 boot_data.imgs[1].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1334
1335 /* Determine the sector layout of the image slots and scratch area. */
1336 rc = boot_read_sectors();
1337 if (rc != 0) {
1338 rc = SPLIT_GO_ERR;
1339 goto done;
1340 }
1341
1342 rc = boot_read_image_headers();
1343 if (rc != 0) {
1344 goto done;
1345 }
1346
1347 app_flash_id = flash_area_id_from_image_slot(split_slot);
1348 rc = flash_area_open(app_flash_id, &app_fap);
1349 if (rc != 0) {
1350 rc = BOOT_EFLASH;
1351 goto done;
1352 }
1353
1354 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1355 rc = flash_area_open(loader_flash_id, &loader_fap);
1356 if (rc != 0) {
1357 rc = BOOT_EFLASH;
1358 goto done;
1359 }
1360
1361 /* Don't check the bootable image flag because we could really call a
1362 * bootable or non-bootable image. Just validate that the image check
1363 * passes which is distinct from the normal check.
1364 */
1365 rc = split_image_check(&boot_data.imgs[split_slot].hdr,
1366 app_fap,
1367 &boot_data.imgs[loader_slot].hdr,
1368 loader_fap);
1369 if (rc != 0) {
1370 rc = SPLIT_GO_NON_MATCHING;
1371 goto done;
1372 }
1373
1374 entry_val = boot_data.imgs[split_slot].sectors[0].fa_off +
1375 boot_data.imgs[split_slot].hdr.ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001376 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001377 rc = SPLIT_GO_OK;
1378
1379done:
1380 free(sectors);
1381 flash_area_close(app_fap);
1382 flash_area_close(loader_fap);
1383 return rc;
1384}