blob: 153d56b8de4631aaac8daf362cc69c6723f1c030 [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#include <assert.h>
21#include <string.h>
22#include <inttypes.h>
23
24#include "syscfg/syscfg.h"
25#include "sysflash/sysflash.h"
26#include "hal/hal_bsp.h"
27#include "hal/hal_flash.h"
28#include "flash_map/flash_map.h"
29#include "os/os.h"
30#include "bootutil/image.h"
31#include "bootutil/bootutil.h"
32#include "bootutil_priv.h"
33
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030034#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
35#include "bootutil/bootutil_log.h"
36
Christopher Collins92ea77f2016-12-12 15:59:26 -080037int boot_current_slot;
38
Fabio Utzig24a273d2017-04-20 08:21:31 -030039const uint32_t boot_img_magic[] = {
Christopher Collins92ea77f2016-12-12 15:59:26 -080040 0xf395c277,
41 0x7fefd260,
42 0x0f505235,
43 0x8079b62c,
44};
45
Fabio Utzig24a273d2017-04-20 08:21:31 -030046const uint32_t BOOT_MAGIC_SZ = sizeof boot_img_magic;
47
Christopher Collins92ea77f2016-12-12 15:59:26 -080048struct boot_swap_table {
49 /** * For each field, a value of 0 means "any". */
50 uint8_t bsw_magic_slot0;
51 uint8_t bsw_magic_slot1;
52 uint8_t bsw_image_ok_slot0;
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -080053 uint8_t bsw_image_ok_slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -080054
55 uint8_t bsw_swap_type;
56};
57
58/**
59 * This set of tables maps image trailer contents to swap operation type.
60 * When searching for a match, these tables must be iterated sequentially.
61 */
62static const struct boot_swap_table boot_swap_tables[] = {
63 {
64 /* | slot-0 | slot-1 |
65 *----------+------------+------------|
66 * magic | Unset | Unset |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -080067 * image-ok | Any | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -080068 * ---------+------------+------------'
69 * swap: none |
70 * -----------------------------------'
71 */
72 .bsw_magic_slot0 = BOOT_MAGIC_UNSET,
73 .bsw_magic_slot1 = BOOT_MAGIC_UNSET,
74 .bsw_image_ok_slot0 = 0,
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -080075 .bsw_image_ok_slot1 = 0,
Christopher Collins92ea77f2016-12-12 15:59:26 -080076 .bsw_swap_type = BOOT_SWAP_TYPE_NONE,
77 },
78
79 {
80 /* | slot-0 | slot-1 |
81 *----------+------------+------------|
82 * magic | Any | Good |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -080083 * image-ok | Any | Unset |
84 * ---------+------------+------------`
85 * swap: test |
86 * -----------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -080087 */
88 .bsw_magic_slot0 = 0,
89 .bsw_magic_slot1 = BOOT_MAGIC_GOOD,
90 .bsw_image_ok_slot0 = 0,
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -080091 .bsw_image_ok_slot1 = 0xff,
Christopher Collins92ea77f2016-12-12 15:59:26 -080092 .bsw_swap_type = BOOT_SWAP_TYPE_TEST,
93 },
94
95 {
96 /* | slot-0 | slot-1 |
97 *----------+------------+------------|
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -080098 * magic | Any | Good |
99 * image-ok | Any | 0x01 |
100 * ---------+------------+------------`
101 * swap: permanent |
102 * -----------------------------------'
103 */
104 .bsw_magic_slot0 = 0,
105 .bsw_magic_slot1 = BOOT_MAGIC_GOOD,
106 .bsw_image_ok_slot0 = 0,
107 .bsw_image_ok_slot1 = 0x01,
108 .bsw_swap_type = BOOT_SWAP_TYPE_PERM,
109 },
110
111 {
112 /* | slot-0 | slot-1 |
113 *----------+------------+------------|
Christopher Collins92ea77f2016-12-12 15:59:26 -0800114 * magic | Good | Unset |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800115 * image-ok | 0xff | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800116 * ---------+------------+------------'
117 * swap: revert (test image running) |
118 * -----------------------------------'
119 */
120 .bsw_magic_slot0 = BOOT_MAGIC_GOOD,
121 .bsw_magic_slot1 = BOOT_MAGIC_UNSET,
122 .bsw_image_ok_slot0 = 0xff,
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800123 .bsw_image_ok_slot1 = 0,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800124 .bsw_swap_type = BOOT_SWAP_TYPE_REVERT,
125 },
126
127 {
128 /* | slot-0 | slot-1 |
129 *----------+------------+------------|
130 * magic | Good | Unset |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800131 * image-ok | 0x01 | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800132 * ---------+------------+------------'
133 * swap: none (confirmed test image) |
134 * -----------------------------------'
135 */
136 .bsw_magic_slot0 = BOOT_MAGIC_GOOD,
137 .bsw_magic_slot1 = BOOT_MAGIC_UNSET,
138 .bsw_image_ok_slot0 = 0x01,
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800139 .bsw_image_ok_slot1 = 0,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800140 .bsw_swap_type = BOOT_SWAP_TYPE_NONE,
141 },
142};
143
144#define BOOT_SWAP_TABLES_COUNT \
145 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
146
147int
148boot_magic_code(const uint32_t *magic)
149{
150 int i;
151
Fabio Utzig24a273d2017-04-20 08:21:31 -0300152 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800153 return BOOT_MAGIC_GOOD;
154 }
155
Fabio Utzig24a273d2017-04-20 08:21:31 -0300156 for (i = 0; i < BOOT_MAGIC_SZ / sizeof *magic; i++) {
157 if (magic[i] != 0xffffffff) {
158 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800159 }
160 }
161
Fabio Utzig24a273d2017-04-20 08:21:31 -0300162 return BOOT_MAGIC_UNSET;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800163}
164
165uint32_t
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300166boot_slots_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800167{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300168 return BOOT_MAGIC_SZ +
Fabio Utzig2473ac02017-05-02 12:45:02 -0300169 /* state for all sectors */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300170 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
Fabio Utzig2473ac02017-05-02 12:45:02 -0300171 /* copy_done + image_ok */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300172 min_write_sz * 2;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800173}
174
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300175static uint32_t
176boot_scratch_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300178 return BOOT_MAGIC_SZ + /* magic */
179 BOOT_STATUS_STATE_COUNT * min_write_sz + /* state for one sector */
180 min_write_sz; /* image_ok */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800181}
182
183static uint32_t
184boot_magic_off(const struct flash_area *fap)
185{
186 uint32_t off_from_end;
187 uint8_t elem_sz;
188
189 elem_sz = flash_area_align(fap);
190
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300191 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
192 off_from_end = boot_scratch_trailer_sz(elem_sz);
193 } else {
194 off_from_end = boot_slots_trailer_sz(elem_sz);
195 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800196
197 assert(off_from_end <= fap->fa_size);
198 return fap->fa_size - off_from_end;
199}
200
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400201int
202boot_status_entries(const struct flash_area *fap)
203{
204 switch (fap->fa_id) {
205 case FLASH_AREA_IMAGE_0:
206 case FLASH_AREA_IMAGE_1:
207 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
208 case FLASH_AREA_IMAGE_SCRATCH:
209 return BOOT_STATUS_STATE_COUNT;
210 default:
211 return BOOT_EBADARGS;
212 }
213}
214
Christopher Collins92ea77f2016-12-12 15:59:26 -0800215uint32_t
216boot_status_off(const struct flash_area *fap)
217{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300218 return boot_magic_off(fap) + BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800219}
220
221static uint32_t
222boot_copy_done_off(const struct flash_area *fap)
223{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300224 assert(fap->fa_id != FLASH_AREA_IMAGE_SCRATCH);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800225 return fap->fa_size - flash_area_align(fap) * 2;
226}
227
228static uint32_t
229boot_image_ok_off(const struct flash_area *fap)
230{
231 return fap->fa_size - flash_area_align(fap);
232}
233
234int
235boot_read_swap_state(const struct flash_area *fap,
236 struct boot_swap_state *state)
237{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300238 uint32_t magic[BOOT_MAGIC_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800239 uint32_t off;
240 int rc;
241
242 off = boot_magic_off(fap);
Fabio Utzig24a273d2017-04-20 08:21:31 -0300243 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800244 if (rc != 0) {
245 return BOOT_EFLASH;
246 }
247 state->magic = boot_magic_code(magic);
248
Fabio Utzig2473ac02017-05-02 12:45:02 -0300249 if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) {
250 off = boot_copy_done_off(fap);
251 rc = flash_area_read(fap, off, &state->copy_done, 1);
252 if (rc != 0) {
253 return BOOT_EFLASH;
254 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800255 }
256
257 off = boot_image_ok_off(fap);
258 rc = flash_area_read(fap, off, &state->image_ok, 1);
259 if (rc != 0) {
260 return BOOT_EFLASH;
261 }
262
263 return 0;
264}
265
266/**
267 * Reads the image trailer from the scratch area.
268 */
269int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300270boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800271{
272 const struct flash_area *fap;
273 int rc;
274
Fabio Utzig2473ac02017-05-02 12:45:02 -0300275 switch (flash_area_id) {
276 case FLASH_AREA_IMAGE_SCRATCH:
277 case FLASH_AREA_IMAGE_0:
278 case FLASH_AREA_IMAGE_1:
279 rc = flash_area_open(flash_area_id, &fap);
280 if (rc != 0) {
281 return BOOT_EFLASH;
282 }
283 break;
284 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400285 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300286 }
287
288 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400289 flash_area_close(fap);
290 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800291}
292
293int
294boot_write_magic(const struct flash_area *fap)
295{
296 uint32_t off;
297 int rc;
298
299 off = boot_magic_off(fap);
300
Fabio Utzig24a273d2017-04-20 08:21:31 -0300301 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800302 if (rc != 0) {
303 return BOOT_EFLASH;
304 }
305
306 return 0;
307}
308
Fabio Utzig2473ac02017-05-02 12:45:02 -0300309static int
310boot_write_flag(int flag, const struct flash_area *fap)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800311{
312 uint32_t off;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800313 int rc;
Fabio Utzig644b8d42017-04-20 07:56:05 -0300314 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700315 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800316
Fabio Utzig2473ac02017-05-02 12:45:02 -0300317 switch (flag) {
318 case BOOT_FLAG_COPY_DONE:
319 off = boot_copy_done_off(fap);
320 break;
321 case BOOT_FLAG_IMAGE_OK:
322 off = boot_image_ok_off(fap);
323 break;
324 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400325 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300326 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800327
David Brown9d725462017-01-23 15:50:58 -0700328 align = hal_flash_align(fap->fa_device_id);
Fabio Utzig644b8d42017-04-20 07:56:05 -0300329 assert(align <= BOOT_MAX_ALIGN);
330 memset(buf, 0xFF, BOOT_MAX_ALIGN);
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400331 buf[0] = BOOT_FLAG_SET;
David Brown9d725462017-01-23 15:50:58 -0700332
333 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800334 if (rc != 0) {
335 return BOOT_EFLASH;
336 }
337
338 return 0;
339}
340
341int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300342boot_write_copy_done(const struct flash_area *fap)
343{
344 return boot_write_flag(BOOT_FLAG_COPY_DONE, fap);
345}
346
347int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800348boot_write_image_ok(const struct flash_area *fap)
349{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300350 return boot_write_flag(BOOT_FLAG_IMAGE_OK, fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800351}
352
353int
354boot_swap_type(void)
355{
356 const struct boot_swap_table *table;
357 struct boot_swap_state state_slot0;
358 struct boot_swap_state state_slot1;
359 int rc;
360 int i;
361
Fabio Utzig2473ac02017-05-02 12:45:02 -0300362 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800363 assert(rc == 0);
364
Fabio Utzig2473ac02017-05-02 12:45:02 -0300365 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &state_slot1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800366 assert(rc == 0);
367
368 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
369 table = boot_swap_tables + i;
370
371 if ((table->bsw_magic_slot0 == 0 ||
372 table->bsw_magic_slot0 == state_slot0.magic) &&
373 (table->bsw_magic_slot1 == 0 ||
374 table->bsw_magic_slot1 == state_slot1.magic) &&
375 (table->bsw_image_ok_slot0 == 0 ||
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800376 table->bsw_image_ok_slot0 == state_slot0.image_ok) &&
377 (table->bsw_image_ok_slot1 == 0 ||
378 table->bsw_image_ok_slot1 == state_slot1.image_ok)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400379 BOOT_LOG_INF("Swap type: %s",
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300380 table->bsw_swap_type == BOOT_SWAP_TYPE_NONE ? "none" :
381 table->bsw_swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
382 table->bsw_swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
383 table->bsw_swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
384 table->bsw_swap_type == BOOT_SWAP_TYPE_FAIL ? "fail" :
385 "BUG; can't happen");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800386 return table->bsw_swap_type;
387 }
388 }
389
390 assert(0);
391 return BOOT_SWAP_TYPE_NONE;
392}
393
394/**
395 * Marks the image in slot 1 as pending. On the next reboot, the system will
396 * perform a one-time boot of the slot 1 image.
397 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800398 * @param permanent Whether the image should be used permanently or
399 * only tested once:
400 * 0=run image once, then confirm or revert.
401 * 1=run image forever.
402 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800403 * @return 0 on success; nonzero on failure.
404 */
405int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800406boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800407{
408 const struct flash_area *fap;
409 struct boot_swap_state state_slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800410 int rc;
411
Fabio Utzig2473ac02017-05-02 12:45:02 -0300412 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &state_slot1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800413 if (rc != 0) {
414 return rc;
415 }
416
417 switch (state_slot1.magic) {
418 case BOOT_MAGIC_GOOD:
419 /* Swap already scheduled. */
420 return 0;
421
422 case BOOT_MAGIC_UNSET:
Fabio Utzig2473ac02017-05-02 12:45:02 -0300423 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800424 if (rc != 0) {
425 rc = BOOT_EFLASH;
426 } else {
427 rc = boot_write_magic(fap);
428 }
429
Christopher Collins7835c1e2016-12-21 10:10:51 -0800430 if (rc == 0 && permanent) {
431 rc = boot_write_image_ok(fap);
432 }
433
Christopher Collins92ea77f2016-12-12 15:59:26 -0800434 flash_area_close(fap);
435 return rc;
436
437 default:
438 /* XXX: Temporary assert. */
439 assert(0);
440 return -1;
441 }
442}
443
444/**
445 * Marks the image in slot 0 as confirmed. The system will continue booting into the image in slot 0 until told to boot from a different slot.
446 *
447 * @return 0 on success; nonzero on failure.
448 */
449int
450boot_set_confirmed(void)
451{
452 const struct flash_area *fap;
453 struct boot_swap_state state_slot0;
454 int rc;
455
Fabio Utzig2473ac02017-05-02 12:45:02 -0300456 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800457 if (rc != 0) {
458 return rc;
459 }
460
461 switch (state_slot0.magic) {
462 case BOOT_MAGIC_GOOD:
463 /* Confirm needed; proceed. */
464 break;
465
466 case BOOT_MAGIC_UNSET:
467 /* Already confirmed. */
468 return 0;
469
470 case BOOT_MAGIC_BAD:
471 /* Unexpected state. */
472 return BOOT_EBADVECT;
473 }
474
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400475 if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800476 /* Swap never completed. This is unexpected. */
477 return BOOT_EBADVECT;
478 }
479
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400480 if (state_slot0.image_ok != BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800481 /* Already confirmed. */
482 return 0;
483 }
484
485 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
486 if (rc) {
487 rc = BOOT_EFLASH;
488 goto done;
489 }
490
491 rc = boot_write_image_ok(fap);
492 if (rc != 0) {
493 goto done;
494 }
495
496 rc = 0;
497
498done:
499 flash_area_close(fap);
500 return rc;
501}