blob: 8cab4c4acfc54660facd8e88ab14f862720a2bee [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>
Fabio Utziga0bc9b52017-06-28 09:19:55 -030023#include <stddef.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080024
Christopher Collins92ea77f2016-12-12 15:59:26 -080025#include "sysflash/sysflash.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020026#include "flash_map_backend/flash_map_backend.h"
27
Christopher Collins92ea77f2016-12-12 15:59:26 -080028#include "bootutil/image.h"
29#include "bootutil/bootutil.h"
30#include "bootutil_priv.h"
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030031#include "bootutil/bootutil_log.h"
Fabio Utzigba829042018-09-18 08:29:34 -030032#ifdef MCUBOOT_ENC_IMAGES
33#include "bootutil/enc_key.h"
34#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030035
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010036MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
37
Christopher Collins92ea77f2016-12-12 15:59:26 -080038int boot_current_slot;
39
Fabio Utzig24a273d2017-04-20 08:21:31 -030040const uint32_t boot_img_magic[] = {
Christopher Collins92ea77f2016-12-12 15:59:26 -080041 0xf395c277,
42 0x7fefd260,
43 0x0f505235,
44 0x8079b62c,
45};
46
Hovland, Sigvart1d96f362018-09-25 13:23:42 +020047#define BOOT_MAGIC_ARR_SZ \
48 (sizeof boot_img_magic / sizeof boot_img_magic[0])
49
Fabio Utzig24a273d2017-04-20 08:21:31 -030050const uint32_t BOOT_MAGIC_SZ = sizeof boot_img_magic;
Fabio Utziga0bc9b52017-06-28 09:19:55 -030051const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN;
Fabio Utzig24a273d2017-04-20 08:21:31 -030052
Christopher Collins92ea77f2016-12-12 15:59:26 -080053struct boot_swap_table {
David Vincze2d736ad2019-02-18 11:50:22 +010054 uint8_t magic_primary_slot;
55 uint8_t magic_secondary_slot;
56 uint8_t image_ok_primary_slot;
57 uint8_t image_ok_secondary_slot;
58 uint8_t copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080059
Fabio Utzigb5b2f552017-06-30 10:03:47 -030060 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -080061};
62
63/**
64 * This set of tables maps image trailer contents to swap operation type.
65 * When searching for a match, these tables must be iterated sequentially.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030066 *
David Vincze2d736ad2019-02-18 11:50:22 +010067 * NOTE: the table order is very important. The settings in the secondary
68 * slot always are priority to the primary slot and should be located
69 * earlier in the table.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030070 *
71 * The table lists only states where there is action needs to be taken by
72 * the bootloader, as in starting/finishing a swap operation.
Christopher Collins92ea77f2016-12-12 15:59:26 -080073 */
74static const struct boot_swap_table boot_swap_tables[] = {
75 {
David Vincze2d736ad2019-02-18 11:50:22 +010076 .magic_primary_slot = BOOT_MAGIC_ANY,
77 .magic_secondary_slot = BOOT_MAGIC_GOOD,
78 .image_ok_primary_slot = BOOT_FLAG_ANY,
79 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
80 .copy_done_primary_slot = BOOT_FLAG_ANY,
81 .swap_type = BOOT_SWAP_TYPE_TEST,
Christopher Collins92ea77f2016-12-12 15:59:26 -080082 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080083 {
David Vincze2d736ad2019-02-18 11:50:22 +010084 .magic_primary_slot = BOOT_MAGIC_ANY,
85 .magic_secondary_slot = BOOT_MAGIC_GOOD,
86 .image_ok_primary_slot = BOOT_FLAG_ANY,
87 .image_ok_secondary_slot = BOOT_FLAG_SET,
88 .copy_done_primary_slot = BOOT_FLAG_ANY,
89 .swap_type = BOOT_SWAP_TYPE_PERM,
Christopher Collins92ea77f2016-12-12 15:59:26 -080090 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080091 {
David Vincze2d736ad2019-02-18 11:50:22 +010092 .magic_primary_slot = BOOT_MAGIC_GOOD,
93 .magic_secondary_slot = BOOT_MAGIC_UNSET,
94 .image_ok_primary_slot = BOOT_FLAG_UNSET,
95 .image_ok_secondary_slot = BOOT_FLAG_ANY,
96 .copy_done_primary_slot = BOOT_FLAG_SET,
97 .swap_type = BOOT_SWAP_TYPE_REVERT,
Christopher Collins92ea77f2016-12-12 15:59:26 -080098 },
99};
100
101#define BOOT_SWAP_TABLES_COUNT \
102 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
103
Fabio Utzig39000012018-07-30 12:40:20 -0300104static int
Fabio Utzig178be542018-09-19 08:12:56 -0300105boot_magic_decode(const uint32_t *magic)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800106{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300107 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800108 return BOOT_MAGIC_GOOD;
109 }
Fabio Utzig178be542018-09-19 08:12:56 -0300110 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800111}
112
Fabio Utzig39000012018-07-30 12:40:20 -0300113static int
Fabio Utzig178be542018-09-19 08:12:56 -0300114boot_flag_decode(uint8_t flag)
Fabio Utzig39000012018-07-30 12:40:20 -0300115{
Fabio Utzig39000012018-07-30 12:40:20 -0300116 if (flag != BOOT_FLAG_SET) {
117 return BOOT_FLAG_BAD;
118 }
119 return BOOT_FLAG_SET;
120}
121
Christopher Collinsa1c12042019-05-23 14:00:28 -0700122/**
123 * Determines if a status source table is satisfied by the specified magic
124 * code.
125 *
126 * @param tbl_val A magic field from a status source table.
127 * @param val The magic value in a trailer, encoded as a
128 * BOOT_MAGIC_[...].
129 *
130 * @return 1 if the two values are compatible;
131 * 0 otherwise.
132 */
133int
134boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
135{
136 switch (tbl_val) {
137 case BOOT_MAGIC_ANY:
138 return 1;
139
140 case BOOT_MAGIC_NOTGOOD:
141 return val != BOOT_MAGIC_GOOD;
142
143 default:
144 return tbl_val == val;
145 }
146}
147
Christopher Collins92ea77f2016-12-12 15:59:26 -0800148uint32_t
Christopher Collins2adef702019-05-22 14:37:31 -0700149boot_trailer_sz(uint8_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800150{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300151 return /* state for all sectors */
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300152 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
Fabio Utzigba829042018-09-18 08:29:34 -0300153#ifdef MCUBOOT_ENC_IMAGES
154 /* encryption keys */
155 BOOT_ENC_KEY_SIZE * 2 +
156#endif
Christopher Collins2adef702019-05-22 14:37:31 -0700157 /* swap_type + copy_done + image_ok + swap_size */
158 BOOT_MAX_ALIGN * 4 +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300159 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800160}
161
162static uint32_t
163boot_magic_off(const struct flash_area *fap)
164{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300165 return fap->fa_size - BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800166}
167
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400168int
169boot_status_entries(const struct flash_area *fap)
170{
171 switch (fap->fa_id) {
David Vincze2d736ad2019-02-18 11:50:22 +0100172 case FLASH_AREA_IMAGE_PRIMARY:
173 case FLASH_AREA_IMAGE_SECONDARY:
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400174 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
175 case FLASH_AREA_IMAGE_SCRATCH:
176 return BOOT_STATUS_STATE_COUNT;
177 default:
178 return BOOT_EBADARGS;
179 }
180}
181
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182uint32_t
183boot_status_off(const struct flash_area *fap)
184{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300185 uint32_t off_from_end;
186 uint8_t elem_sz;
187
188 elem_sz = flash_area_align(fap);
189
Christopher Collins2adef702019-05-22 14:37:31 -0700190 off_from_end = boot_trailer_sz(elem_sz);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300191
192 assert(off_from_end <= fap->fa_size);
193 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800194}
195
Christopher Collinsa1c12042019-05-23 14:00:28 -0700196uint32_t
197boot_swap_type_off(const struct flash_area *fap)
198{
199 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 3;
200}
201
Christopher Collins92ea77f2016-12-12 15:59:26 -0800202static uint32_t
203boot_copy_done_off(const struct flash_area *fap)
204{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300205 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 2;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800206}
207
208static uint32_t
209boot_image_ok_off(const struct flash_area *fap)
210{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300211 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800212}
213
Fabio Utzig46490722017-09-04 15:34:32 -0300214static uint32_t
215boot_swap_size_off(const struct flash_area *fap)
216{
Christopher Collins2adef702019-05-22 14:37:31 -0700217 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4;
Fabio Utzig46490722017-09-04 15:34:32 -0300218}
219
Fabio Utzigba829042018-09-18 08:29:34 -0300220#ifdef MCUBOOT_ENC_IMAGES
221static uint32_t
222boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
223{
Christopher Collins2adef702019-05-22 14:37:31 -0700224 return fap->fa_size - BOOT_MAGIC_SZ - BOOT_MAX_ALIGN * 4 -
Fabio Utzigba829042018-09-18 08:29:34 -0300225 ((slot + 1) * BOOT_ENC_KEY_SIZE);
226}
227#endif
228
Christopher Collins92ea77f2016-12-12 15:59:26 -0800229int
230boot_read_swap_state(const struct flash_area *fap,
231 struct boot_swap_state *state)
232{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200233 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800234 uint32_t off;
235 int rc;
236
237 off = boot_magic_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300238 rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
239 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800240 return BOOT_EFLASH;
241 }
Fabio Utzig178be542018-09-19 08:12:56 -0300242 if (rc == 1) {
243 state->magic = BOOT_MAGIC_UNSET;
244 } else {
245 state->magic = boot_magic_decode(magic);
246 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800247
Christopher Collinsa1c12042019-05-23 14:00:28 -0700248 off = boot_swap_type_off(fap);
249 rc = flash_area_read_is_empty(fap, off, &state->swap_type,
250 sizeof state->swap_type);
251 if (rc < 0) {
252 return BOOT_EFLASH;
253 }
254 if (rc == 1 || state->swap_type > BOOT_SWAP_TYPE_REVERT) {
255 state->swap_type = BOOT_SWAP_TYPE_NONE;
256 }
257
Christopher Collins2adef702019-05-22 14:37:31 -0700258 off = boot_copy_done_off(fap);
259 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
260 sizeof state->copy_done);
261 if (rc < 0) {
262 return BOOT_EFLASH;
263 }
264 if (rc == 1) {
265 state->copy_done = BOOT_FLAG_UNSET;
266 } else {
267 state->copy_done = boot_flag_decode(state->copy_done);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800268 }
269
270 off = boot_image_ok_off(fap);
David Vincze2d736ad2019-02-18 11:50:22 +0100271 rc = flash_area_read_is_empty(fap, off, &state->image_ok,
272 sizeof state->image_ok);
Fabio Utzig178be542018-09-19 08:12:56 -0300273 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800274 return BOOT_EFLASH;
275 }
Fabio Utzig178be542018-09-19 08:12:56 -0300276 if (rc == 1) {
277 state->image_ok = BOOT_FLAG_UNSET;
278 } else {
279 state->image_ok = boot_flag_decode(state->image_ok);
280 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800281
282 return 0;
283}
284
285/**
286 * Reads the image trailer from the scratch area.
287 */
288int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300289boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800290{
291 const struct flash_area *fap;
292 int rc;
293
Fabio Utzig2473ac02017-05-02 12:45:02 -0300294 switch (flash_area_id) {
295 case FLASH_AREA_IMAGE_SCRATCH:
David Vincze2d736ad2019-02-18 11:50:22 +0100296 case FLASH_AREA_IMAGE_PRIMARY:
297 case FLASH_AREA_IMAGE_SECONDARY:
Fabio Utzig2473ac02017-05-02 12:45:02 -0300298 rc = flash_area_open(flash_area_id, &fap);
299 if (rc != 0) {
300 return BOOT_EFLASH;
301 }
302 break;
303 default:
Fabio Utzig856f7832017-05-22 11:04:44 -0400304 return BOOT_EBADARGS;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300305 }
306
307 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400308 flash_area_close(fap);
309 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800310}
311
312int
Fabio Utzig46490722017-09-04 15:34:32 -0300313boot_read_swap_size(uint32_t *swap_size)
314{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200315 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Fabio Utzig46490722017-09-04 15:34:32 -0300316 uint32_t off;
317 const struct flash_area *fap;
318 int rc;
319
320 /*
321 * In the middle a swap, tries to locate the saved swap size. Looks
David Vincze2d736ad2019-02-18 11:50:22 +0100322 * for a valid magic, first on the primary slot, then on scratch.
323 * Both "slots" can end up being temporary storage for a swap and it
324 * is assumed that if magic is valid then swap size is too, because
325 * magic is always written in the last step.
Fabio Utzig46490722017-09-04 15:34:32 -0300326 */
327
David Vincze2d736ad2019-02-18 11:50:22 +0100328 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300329 if (rc != 0) {
330 return BOOT_EFLASH;
331 }
332
333 off = boot_magic_off(fap);
334 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
335 if (rc != 0) {
336 rc = BOOT_EFLASH;
337 goto out;
338 }
339
340 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
341 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100342 * If the primary slot's magic is not valid, try scratch...
Fabio Utzig46490722017-09-04 15:34:32 -0300343 */
344
345 flash_area_close(fap);
346
347 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
348 if (rc != 0) {
349 return BOOT_EFLASH;
350 }
351
352 off = boot_magic_off(fap);
353 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
354 if (rc != 0) {
355 rc = BOOT_EFLASH;
356 goto out;
357 }
358
359 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
360 }
361
362 off = boot_swap_size_off(fap);
363 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
364 if (rc != 0) {
365 rc = BOOT_EFLASH;
366 }
367
368out:
369 flash_area_close(fap);
370 return rc;
371}
372
Fabio Utzigba829042018-09-18 08:29:34 -0300373#ifdef MCUBOOT_ENC_IMAGES
374int
375boot_read_enc_key(uint8_t slot, uint8_t *enckey)
376{
377 uint32_t magic[BOOT_MAGIC_SZ];
378 uint32_t off;
379 const struct flash_area *fap;
380 int rc;
381
David Vincze2d736ad2019-02-18 11:50:22 +0100382 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Fabio Utzigba829042018-09-18 08:29:34 -0300383 if (rc != 0) {
384 return BOOT_EFLASH;
385 }
386
387 off = boot_magic_off(fap);
388 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
389 if (rc != 0) {
390 rc = BOOT_EFLASH;
391 goto out;
392 }
393
394 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) != 0) {
395 /*
David Vincze2d736ad2019-02-18 11:50:22 +0100396 * If the primary slot's magic is not valid, try scratch...
Fabio Utzigba829042018-09-18 08:29:34 -0300397 */
398
399 flash_area_close(fap);
400
401 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
402 if (rc != 0) {
403 return BOOT_EFLASH;
404 }
405
406 off = boot_magic_off(fap);
407 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
408 if (rc != 0) {
409 rc = BOOT_EFLASH;
410 goto out;
411 }
412
413 assert(memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0);
414 }
415
416 off = boot_enc_key_off(fap, slot);
417 rc = flash_area_read(fap, off, enckey, BOOT_ENC_KEY_SIZE);
418 if (rc != 0) {
419 rc = BOOT_EFLASH;
420 }
421
422out:
423 flash_area_close(fap);
424 return rc;
425}
426#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300427
428int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800429boot_write_magic(const struct flash_area *fap)
430{
431 uint32_t off;
432 int rc;
433
434 off = boot_magic_off(fap);
435
Christopher Collins2c88e692019-05-22 15:10:14 -0700436 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%x (0x%x)",
437 fap->fa_id, off, fap->fa_off + off);
Fabio Utzig24a273d2017-04-20 08:21:31 -0300438 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800439 if (rc != 0) {
440 return BOOT_EFLASH;
441 }
442
443 return 0;
444}
445
Fabio Utzig2473ac02017-05-02 12:45:02 -0300446static int
Christopher Collinsa1c12042019-05-23 14:00:28 -0700447boot_write_trailer_byte(const struct flash_area *fap, uint32_t off,
448 uint8_t val)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800449{
Fabio Utzig644b8d42017-04-20 07:56:05 -0300450 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700451 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300452 uint8_t erased_val;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700453 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800454
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200455 align = flash_area_align(fap);
Fabio Utzig644b8d42017-04-20 07:56:05 -0300456 assert(align <= BOOT_MAX_ALIGN);
Fabio Utzig39000012018-07-30 12:40:20 -0300457 erased_val = flash_area_erased_val(fap);
458 memset(buf, erased_val, BOOT_MAX_ALIGN);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700459 buf[0] = val;
David Brown9d725462017-01-23 15:50:58 -0700460
461 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800462 if (rc != 0) {
463 return BOOT_EFLASH;
464 }
465
466 return 0;
467}
468
469int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300470boot_write_copy_done(const struct flash_area *fap)
471{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700472 uint32_t off;
473
474 off = boot_copy_done_off(fap);
475 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%x (0x%x)",
476 fap->fa_id, off, fap->fa_off + off);
477 return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300478}
479
480int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800481boot_write_image_ok(const struct flash_area *fap)
482{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700483 uint32_t off;
484
485 off = boot_image_ok_off(fap);
486 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%x (0x%x)",
487 fap->fa_id, off, fap->fa_off + off);
488 return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
489}
490
491/**
492 * Writes the specified value to the `swap-type` field of an image trailer.
493 * This value is persisted so that the boot loader knows what swap operation to
494 * resume in case of an unexpected reset.
495 */
496int
497boot_write_swap_type(const struct flash_area *fap, uint8_t swap_type)
498{
499 uint32_t off;
500
501 off = boot_swap_type_off(fap);
502 BOOT_LOG_DBG("writing swap_type; fa_id=%d off=0x%x (0x%x), swap_type=0x%x",
503 fap->fa_id, off, fap->fa_off + off, swap_type);
504 return boot_write_trailer_byte(fap, off, swap_type);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800505}
506
507int
Fabio Utzig46490722017-09-04 15:34:32 -0300508boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
509{
510 uint32_t off;
511 int rc;
512 uint8_t buf[BOOT_MAX_ALIGN];
513 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300514 uint8_t erased_val;
Fabio Utzig46490722017-09-04 15:34:32 -0300515
516 off = boot_swap_size_off(fap);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200517 align = flash_area_align(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300518 assert(align <= BOOT_MAX_ALIGN);
519 if (align < sizeof swap_size) {
520 align = sizeof swap_size;
521 }
Fabio Utzig39000012018-07-30 12:40:20 -0300522 erased_val = flash_area_erased_val(fap);
523 memset(buf, erased_val, BOOT_MAX_ALIGN);
Fabio Utzig46490722017-09-04 15:34:32 -0300524 memcpy(buf, (uint8_t *)&swap_size, sizeof swap_size);
525
Christopher Collins2c88e692019-05-22 15:10:14 -0700526 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%x (0x%x)",
527 fap->fa_id, off, fap->fa_off + off);
528
Fabio Utzig46490722017-09-04 15:34:32 -0300529 rc = flash_area_write(fap, off, buf, align);
530 if (rc != 0) {
531 return BOOT_EFLASH;
532 }
533
534 return 0;
535}
536
Fabio Utzigba829042018-09-18 08:29:34 -0300537#ifdef MCUBOOT_ENC_IMAGES
538int
539boot_write_enc_key(const struct flash_area *fap, uint8_t slot, const uint8_t *enckey)
540{
541 uint32_t off;
542 int rc;
543
544 off = boot_enc_key_off(fap, slot);
545 rc = flash_area_write(fap, off, enckey, BOOT_ENC_KEY_SIZE);
546 if (rc != 0) {
547 return BOOT_EFLASH;
548 }
549
550 return 0;
551}
552#endif
553
Fabio Utzig46490722017-09-04 15:34:32 -0300554int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800555boot_swap_type(void)
556{
557 const struct boot_swap_table *table;
David Vincze2d736ad2019-02-18 11:50:22 +0100558 struct boot_swap_state primary_slot;
559 struct boot_swap_state secondary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800560 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200561 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800562
David Vincze2d736ad2019-02-18 11:50:22 +0100563 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY, &primary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300564 if (rc) {
565 return BOOT_SWAP_TYPE_PANIC;
566 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800567
David Vincze2d736ad2019-02-18 11:50:22 +0100568 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
569 &secondary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300570 if (rc) {
571 return BOOT_SWAP_TYPE_PANIC;
572 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800573
574 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
575 table = boot_swap_tables + i;
576
Christopher Collinsa1c12042019-05-23 14:00:28 -0700577 if (boot_magic_compatible_check(table->magic_primary_slot,
578 primary_slot.magic) &&
579 boot_magic_compatible_check(table->magic_secondary_slot,
580 secondary_slot.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100581 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
582 table->image_ok_primary_slot == primary_slot.image_ok) &&
583 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
584 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
585 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
586 table->copy_done_primary_slot == primary_slot.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400587 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300588 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
589 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
590 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
591 "BUG; can't happen");
592 assert(table->swap_type == BOOT_SWAP_TYPE_TEST ||
593 table->swap_type == BOOT_SWAP_TYPE_PERM ||
594 table->swap_type == BOOT_SWAP_TYPE_REVERT);
595 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800596 }
597 }
598
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300599 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800600 return BOOT_SWAP_TYPE_NONE;
601}
602
603/**
David Vincze2d736ad2019-02-18 11:50:22 +0100604 * Marks the image in the secondary slot as pending. On the next reboot,
605 * the system will perform a one-time boot of the the secondary slot image.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800606 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800607 * @param permanent Whether the image should be used permanently or
608 * only tested once:
609 * 0=run image once, then confirm or revert.
610 * 1=run image forever.
611 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800612 * @return 0 on success; nonzero on failure.
613 */
614int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800615boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800616{
617 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100618 struct boot_swap_state state_secondary_slot;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700619 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800620 int rc;
621
David Vincze2d736ad2019-02-18 11:50:22 +0100622 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY,
623 &state_secondary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800624 if (rc != 0) {
625 return rc;
626 }
627
David Vincze2d736ad2019-02-18 11:50:22 +0100628 switch (state_secondary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800629 case BOOT_MAGIC_GOOD:
630 /* Swap already scheduled. */
631 return 0;
632
633 case BOOT_MAGIC_UNSET:
David Vincze2d736ad2019-02-18 11:50:22 +0100634 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800635 if (rc != 0) {
636 rc = BOOT_EFLASH;
637 } else {
638 rc = boot_write_magic(fap);
639 }
640
Christopher Collins7835c1e2016-12-21 10:10:51 -0800641 if (rc == 0 && permanent) {
642 rc = boot_write_image_ok(fap);
643 }
644
Christopher Collinsa1c12042019-05-23 14:00:28 -0700645 if (rc == 0) {
646 if (permanent) {
647 swap_type = BOOT_SWAP_TYPE_PERM;
648 } else {
649 swap_type = BOOT_SWAP_TYPE_TEST;
650 }
651 rc = boot_write_swap_type(fap, swap_type);
652 }
653
Christopher Collins92ea77f2016-12-12 15:59:26 -0800654 flash_area_close(fap);
655 return rc;
656
Christopher Collinsae01f152019-01-30 09:56:37 -0800657 case BOOT_MAGIC_BAD:
658 /* The image slot is corrupt. There is no way to recover, so erase the
659 * slot to allow future upgrades.
660 */
David Vincze2d736ad2019-02-18 11:50:22 +0100661 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap);
Christopher Collinsae01f152019-01-30 09:56:37 -0800662 if (rc != 0) {
663 return BOOT_EFLASH;
664 }
665
666 flash_area_erase(fap, 0, fap->fa_size);
667 flash_area_close(fap);
668 return BOOT_EBADIMAGE;
669
Christopher Collins92ea77f2016-12-12 15:59:26 -0800670 default:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800671 assert(0);
Christopher Collinsae01f152019-01-30 09:56:37 -0800672 return BOOT_EBADIMAGE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800673 }
674}
675
676/**
David Vincze2d736ad2019-02-18 11:50:22 +0100677 * Marks the image in the primary slot as confirmed. The system will continue
678 * booting into the image in the primary slot until told to boot from a
679 * different slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800680 *
681 * @return 0 on success; nonzero on failure.
682 */
683int
684boot_set_confirmed(void)
685{
686 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100687 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800688 int rc;
689
David Vincze2d736ad2019-02-18 11:50:22 +0100690 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
691 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800692 if (rc != 0) {
693 return rc;
694 }
695
David Vincze2d736ad2019-02-18 11:50:22 +0100696 switch (state_primary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800697 case BOOT_MAGIC_GOOD:
698 /* Confirm needed; proceed. */
699 break;
700
701 case BOOT_MAGIC_UNSET:
702 /* Already confirmed. */
703 return 0;
704
705 case BOOT_MAGIC_BAD:
706 /* Unexpected state. */
707 return BOOT_EBADVECT;
708 }
709
David Vincze2d736ad2019-02-18 11:50:22 +0100710 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800711 if (rc) {
712 rc = BOOT_EFLASH;
713 goto done;
714 }
715
David Vincze2d736ad2019-02-18 11:50:22 +0100716 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300717 /* Swap never completed. This is unexpected. */
718 rc = BOOT_EBADVECT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800719 goto done;
720 }
721
David Vincze2d736ad2019-02-18 11:50:22 +0100722 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300723 /* Already confirmed. */
724 goto done;
725 }
726
727 rc = boot_write_image_ok(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800728
729done:
730 flash_area_close(fap);
731 return rc;
732}