blob: 902fe7775b7cdedc123094ec07f342006d204484 [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
Christopher Collins92ea77f2016-12-12 15:59:26 -080024#include "sysflash/sysflash.h"
25#include "hal/hal_bsp.h"
26#include "hal/hal_flash.h"
27#include "flash_map/flash_map.h"
28#include "os/os.h"
29#include "bootutil/image.h"
30#include "bootutil/bootutil.h"
31#include "bootutil_priv.h"
32
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030033#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
34#include "bootutil/bootutil_log.h"
35
Christopher Collins92ea77f2016-12-12 15:59:26 -080036int boot_current_slot;
37
Fabio Utzig24a273d2017-04-20 08:21:31 -030038const uint32_t boot_img_magic[] = {
Christopher Collins92ea77f2016-12-12 15:59:26 -080039 0xf395c277,
40 0x7fefd260,
41 0x0f505235,
42 0x8079b62c,
43};
44
Fabio Utzig24a273d2017-04-20 08:21:31 -030045const uint32_t BOOT_MAGIC_SZ = sizeof boot_img_magic;
46
Christopher Collins92ea77f2016-12-12 15:59:26 -080047struct boot_swap_table {
48 /** * For each field, a value of 0 means "any". */
49 uint8_t bsw_magic_slot0;
50 uint8_t bsw_magic_slot1;
51 uint8_t bsw_image_ok_slot0;
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -080052 uint8_t bsw_image_ok_slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -080053
54 uint8_t bsw_swap_type;
55};
56
57/**
58 * This set of tables maps image trailer contents to swap operation type.
59 * When searching for a match, these tables must be iterated sequentially.
60 */
61static const struct boot_swap_table boot_swap_tables[] = {
62 {
63 /* | slot-0 | slot-1 |
64 *----------+------------+------------|
65 * magic | Unset | Unset |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -080066 * image-ok | Any | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -080067 * ---------+------------+------------'
68 * swap: none |
69 * -----------------------------------'
70 */
71 .bsw_magic_slot0 = BOOT_MAGIC_UNSET,
72 .bsw_magic_slot1 = BOOT_MAGIC_UNSET,
73 .bsw_image_ok_slot0 = 0,
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -080074 .bsw_image_ok_slot1 = 0,
Christopher Collins92ea77f2016-12-12 15:59:26 -080075 .bsw_swap_type = BOOT_SWAP_TYPE_NONE,
76 },
77
78 {
79 /* | slot-0 | slot-1 |
80 *----------+------------+------------|
81 * magic | Any | Good |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -080082 * image-ok | Any | Unset |
83 * ---------+------------+------------`
84 * swap: test |
85 * -----------------------------------'
Christopher Collins92ea77f2016-12-12 15:59:26 -080086 */
87 .bsw_magic_slot0 = 0,
88 .bsw_magic_slot1 = BOOT_MAGIC_GOOD,
89 .bsw_image_ok_slot0 = 0,
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -080090 .bsw_image_ok_slot1 = 0xff,
Christopher Collins92ea77f2016-12-12 15:59:26 -080091 .bsw_swap_type = BOOT_SWAP_TYPE_TEST,
92 },
93
94 {
95 /* | slot-0 | slot-1 |
96 *----------+------------+------------|
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -080097 * magic | Any | Good |
98 * image-ok | Any | 0x01 |
99 * ---------+------------+------------`
100 * swap: permanent |
101 * -----------------------------------'
102 */
103 .bsw_magic_slot0 = 0,
104 .bsw_magic_slot1 = BOOT_MAGIC_GOOD,
105 .bsw_image_ok_slot0 = 0,
106 .bsw_image_ok_slot1 = 0x01,
107 .bsw_swap_type = BOOT_SWAP_TYPE_PERM,
108 },
109
110 {
111 /* | slot-0 | slot-1 |
112 *----------+------------+------------|
Christopher Collins92ea77f2016-12-12 15:59:26 -0800113 * magic | Good | Unset |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800114 * image-ok | 0xff | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800115 * ---------+------------+------------'
116 * swap: revert (test image running) |
117 * -----------------------------------'
118 */
119 .bsw_magic_slot0 = BOOT_MAGIC_GOOD,
120 .bsw_magic_slot1 = BOOT_MAGIC_UNSET,
121 .bsw_image_ok_slot0 = 0xff,
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800122 .bsw_image_ok_slot1 = 0,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800123 .bsw_swap_type = BOOT_SWAP_TYPE_REVERT,
124 },
125
126 {
127 /* | slot-0 | slot-1 |
128 *----------+------------+------------|
129 * magic | Good | Unset |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800130 * image-ok | 0x01 | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800131 * ---------+------------+------------'
132 * swap: none (confirmed test image) |
133 * -----------------------------------'
134 */
135 .bsw_magic_slot0 = BOOT_MAGIC_GOOD,
136 .bsw_magic_slot1 = BOOT_MAGIC_UNSET,
137 .bsw_image_ok_slot0 = 0x01,
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800138 .bsw_image_ok_slot1 = 0,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800139 .bsw_swap_type = BOOT_SWAP_TYPE_NONE,
140 },
141};
142
143#define BOOT_SWAP_TABLES_COUNT \
144 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
145
146int
147boot_magic_code(const uint32_t *magic)
148{
149 int i;
150
Fabio Utzig24a273d2017-04-20 08:21:31 -0300151 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800152 return BOOT_MAGIC_GOOD;
153 }
154
Fabio Utzig24a273d2017-04-20 08:21:31 -0300155 for (i = 0; i < BOOT_MAGIC_SZ / sizeof *magic; i++) {
156 if (magic[i] != 0xffffffff) {
157 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800158 }
159 }
160
Fabio Utzig24a273d2017-04-20 08:21:31 -0300161 return BOOT_MAGIC_UNSET;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800162}
163
164uint32_t
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300165boot_slots_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800166{
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300167 return BOOT_MAGIC_SZ +
Fabio Utzig2473ac02017-05-02 12:45:02 -0300168 /* state for all sectors */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300169 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
Fabio Utzig2473ac02017-05-02 12:45:02 -0300170 /* copy_done + image_ok */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300171 min_write_sz * 2;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800172}
173
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300174static uint32_t
175boot_scratch_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800176{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300177 return BOOT_MAGIC_SZ + /* magic */
178 BOOT_STATUS_STATE_COUNT * min_write_sz + /* state for one sector */
179 min_write_sz; /* image_ok */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800180}
181
182static uint32_t
183boot_magic_off(const struct flash_area *fap)
184{
185 uint32_t off_from_end;
186 uint8_t elem_sz;
187
188 elem_sz = flash_area_align(fap);
189
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300190 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
191 off_from_end = boot_scratch_trailer_sz(elem_sz);
192 } else {
193 off_from_end = boot_slots_trailer_sz(elem_sz);
194 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800195
196 assert(off_from_end <= fap->fa_size);
197 return fap->fa_size - off_from_end;
198}
199
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400200int
201boot_status_entries(const struct flash_area *fap)
202{
203 switch (fap->fa_id) {
204 case FLASH_AREA_IMAGE_0:
205 case FLASH_AREA_IMAGE_1:
206 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
207 case FLASH_AREA_IMAGE_SCRATCH:
208 return BOOT_STATUS_STATE_COUNT;
209 default:
210 return BOOT_EBADARGS;
211 }
212}
213
Christopher Collins92ea77f2016-12-12 15:59:26 -0800214uint32_t
215boot_status_off(const struct flash_area *fap)
216{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300217 return boot_magic_off(fap) + BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800218}
219
220static uint32_t
221boot_copy_done_off(const struct flash_area *fap)
222{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300223 assert(fap->fa_id != FLASH_AREA_IMAGE_SCRATCH);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800224 return fap->fa_size - flash_area_align(fap) * 2;
225}
226
227static uint32_t
228boot_image_ok_off(const struct flash_area *fap)
229{
230 return fap->fa_size - flash_area_align(fap);
231}
232
233int
234boot_read_swap_state(const struct flash_area *fap,
235 struct boot_swap_state *state)
236{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300237 uint32_t magic[BOOT_MAGIC_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800238 uint32_t off;
239 int rc;
240
241 off = boot_magic_off(fap);
Fabio Utzig24a273d2017-04-20 08:21:31 -0300242 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800243 if (rc != 0) {
244 return BOOT_EFLASH;
245 }
246 state->magic = boot_magic_code(magic);
247
Fabio Utzig2473ac02017-05-02 12:45:02 -0300248 if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) {
249 off = boot_copy_done_off(fap);
250 rc = flash_area_read(fap, off, &state->copy_done, 1);
251 if (rc != 0) {
252 return BOOT_EFLASH;
253 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800254 }
255
256 off = boot_image_ok_off(fap);
257 rc = flash_area_read(fap, off, &state->image_ok, 1);
258 if (rc != 0) {
259 return BOOT_EFLASH;
260 }
261
262 return 0;
263}
264
265/**
266 * Reads the image trailer from the scratch area.
267 */
268int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300269boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800270{
271 const struct flash_area *fap;
272 int rc;
273
Fabio Utzig2473ac02017-05-02 12:45:02 -0300274 switch (flash_area_id) {
275 case FLASH_AREA_IMAGE_SCRATCH:
276 case FLASH_AREA_IMAGE_0:
277 case FLASH_AREA_IMAGE_1:
278 rc = flash_area_open(flash_area_id, &fap);
279 if (rc != 0) {
280 return BOOT_EFLASH;
281 }
282 break;
283 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400284 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300285 }
286
287 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400288 flash_area_close(fap);
289 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800290}
291
292int
293boot_write_magic(const struct flash_area *fap)
294{
295 uint32_t off;
296 int rc;
297
298 off = boot_magic_off(fap);
299
Fabio Utzig24a273d2017-04-20 08:21:31 -0300300 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800301 if (rc != 0) {
302 return BOOT_EFLASH;
303 }
304
305 return 0;
306}
307
Fabio Utzig2473ac02017-05-02 12:45:02 -0300308static int
309boot_write_flag(int flag, const struct flash_area *fap)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800310{
311 uint32_t off;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800312 int rc;
Fabio Utzig644b8d42017-04-20 07:56:05 -0300313 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700314 uint8_t align;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800315
Fabio Utzig2473ac02017-05-02 12:45:02 -0300316 switch (flag) {
317 case BOOT_FLAG_COPY_DONE:
318 off = boot_copy_done_off(fap);
319 break;
320 case BOOT_FLAG_IMAGE_OK:
321 off = boot_image_ok_off(fap);
322 break;
323 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400324 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300325 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800326
David Brown9d725462017-01-23 15:50:58 -0700327 align = hal_flash_align(fap->fa_device_id);
Fabio Utzig644b8d42017-04-20 07:56:05 -0300328 assert(align <= BOOT_MAX_ALIGN);
329 memset(buf, 0xFF, BOOT_MAX_ALIGN);
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400330 buf[0] = BOOT_FLAG_SET;
David Brown9d725462017-01-23 15:50:58 -0700331
332 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800333 if (rc != 0) {
334 return BOOT_EFLASH;
335 }
336
337 return 0;
338}
339
340int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300341boot_write_copy_done(const struct flash_area *fap)
342{
343 return boot_write_flag(BOOT_FLAG_COPY_DONE, fap);
344}
345
346int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800347boot_write_image_ok(const struct flash_area *fap)
348{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300349 return boot_write_flag(BOOT_FLAG_IMAGE_OK, fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800350}
351
352int
353boot_swap_type(void)
354{
355 const struct boot_swap_table *table;
356 struct boot_swap_state state_slot0;
357 struct boot_swap_state state_slot1;
358 int rc;
359 int i;
360
Fabio Utzig2473ac02017-05-02 12:45:02 -0300361 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800362 assert(rc == 0);
363
Fabio Utzig2473ac02017-05-02 12:45:02 -0300364 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &state_slot1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800365 assert(rc == 0);
366
367 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
368 table = boot_swap_tables + i;
369
370 if ((table->bsw_magic_slot0 == 0 ||
371 table->bsw_magic_slot0 == state_slot0.magic) &&
372 (table->bsw_magic_slot1 == 0 ||
373 table->bsw_magic_slot1 == state_slot1.magic) &&
374 (table->bsw_image_ok_slot0 == 0 ||
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800375 table->bsw_image_ok_slot0 == state_slot0.image_ok) &&
376 (table->bsw_image_ok_slot1 == 0 ||
377 table->bsw_image_ok_slot1 == state_slot1.image_ok)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400378 BOOT_LOG_INF("Swap type: %s",
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300379 table->bsw_swap_type == BOOT_SWAP_TYPE_NONE ? "none" :
380 table->bsw_swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
381 table->bsw_swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
382 table->bsw_swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
383 table->bsw_swap_type == BOOT_SWAP_TYPE_FAIL ? "fail" :
384 "BUG; can't happen");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800385 return table->bsw_swap_type;
386 }
387 }
388
389 assert(0);
390 return BOOT_SWAP_TYPE_NONE;
391}
392
393/**
394 * Marks the image in slot 1 as pending. On the next reboot, the system will
395 * perform a one-time boot of the slot 1 image.
396 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800397 * @param permanent Whether the image should be used permanently or
398 * only tested once:
399 * 0=run image once, then confirm or revert.
400 * 1=run image forever.
401 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800402 * @return 0 on success; nonzero on failure.
403 */
404int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800405boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800406{
407 const struct flash_area *fap;
408 struct boot_swap_state state_slot1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800409 int rc;
410
Fabio Utzig2473ac02017-05-02 12:45:02 -0300411 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &state_slot1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800412 if (rc != 0) {
413 return rc;
414 }
415
416 switch (state_slot1.magic) {
417 case BOOT_MAGIC_GOOD:
418 /* Swap already scheduled. */
419 return 0;
420
421 case BOOT_MAGIC_UNSET:
Fabio Utzig2473ac02017-05-02 12:45:02 -0300422 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800423 if (rc != 0) {
424 rc = BOOT_EFLASH;
425 } else {
426 rc = boot_write_magic(fap);
427 }
428
Christopher Collins7835c1e2016-12-21 10:10:51 -0800429 if (rc == 0 && permanent) {
430 rc = boot_write_image_ok(fap);
431 }
432
Christopher Collins92ea77f2016-12-12 15:59:26 -0800433 flash_area_close(fap);
434 return rc;
435
436 default:
437 /* XXX: Temporary assert. */
438 assert(0);
439 return -1;
440 }
441}
442
443/**
444 * 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.
445 *
446 * @return 0 on success; nonzero on failure.
447 */
448int
449boot_set_confirmed(void)
450{
451 const struct flash_area *fap;
452 struct boot_swap_state state_slot0;
453 int rc;
454
Fabio Utzig2473ac02017-05-02 12:45:02 -0300455 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800456 if (rc != 0) {
457 return rc;
458 }
459
460 switch (state_slot0.magic) {
461 case BOOT_MAGIC_GOOD:
462 /* Confirm needed; proceed. */
463 break;
464
465 case BOOT_MAGIC_UNSET:
466 /* Already confirmed. */
467 return 0;
468
469 case BOOT_MAGIC_BAD:
470 /* Unexpected state. */
471 return BOOT_EBADVECT;
472 }
473
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400474 if (state_slot0.copy_done == BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800475 /* Swap never completed. This is unexpected. */
476 return BOOT_EBADVECT;
477 }
478
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400479 if (state_slot0.image_ok != BOOT_FLAG_UNSET) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800480 /* Already confirmed. */
481 return 0;
482 }
483
484 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
485 if (rc) {
486 rc = BOOT_EFLASH;
487 goto done;
488 }
489
490 rc = boot_write_image_ok(fap);
491 if (rc != 0) {
492 goto done;
493 }
494
495 rc = 0;
496
497done:
498 flash_area_close(fap);
499 return rc;
500}