blob: c9562db124b175db62ea590c400a4011464f9864 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
David Brownaac71112020-02-03 16:13:42 -07002 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Copyright (c) 2017-2019 Linaro LTD
5 * Copyright (c) 2016-2019 JUUL Labs
6 * Copyright (c) 2019 Arm Limited
7 *
8 * Original license:
9 *
Christopher Collins92ea77f2016-12-12 15:59:26 -080010 * Licensed to the Apache Software Foundation (ASF) under one
11 * or more contributor license agreements. See the NOTICE file
12 * distributed with this work for additional information
13 * regarding copyright ownership. The ASF licenses this file
14 * to you under the Apache License, Version 2.0 (the
15 * "License"); you may not use this file except in compliance
16 * with the License. You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing,
21 * software distributed under the License is distributed on an
22 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23 * KIND, either express or implied. See the License for the
24 * specific language governing permissions and limitations
25 * under the License.
26 */
27
28#include <assert.h>
29#include <string.h>
30#include <inttypes.h>
Fabio Utziga0bc9b52017-06-28 09:19:55 -030031#include <stddef.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080032
Christopher Collins92ea77f2016-12-12 15:59:26 -080033#include "sysflash/sysflash.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020034#include "flash_map_backend/flash_map_backend.h"
35
Christopher Collins92ea77f2016-12-12 15:59:26 -080036#include "bootutil/image.h"
37#include "bootutil/bootutil.h"
38#include "bootutil_priv.h"
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030039#include "bootutil/bootutil_log.h"
Fabio Utzigba829042018-09-18 08:29:34 -030040#ifdef MCUBOOT_ENC_IMAGES
41#include "bootutil/enc_key.h"
42#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030043
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010044MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
45
Fabio Utzig10ee6482019-08-01 12:04:52 -030046/* Currently only used by imgmgr */
Christopher Collins92ea77f2016-12-12 15:59:26 -080047int boot_current_slot;
48
Fabio Utzig24a273d2017-04-20 08:21:31 -030049const uint32_t boot_img_magic[] = {
Christopher Collins92ea77f2016-12-12 15:59:26 -080050 0xf395c277,
51 0x7fefd260,
52 0x0f505235,
53 0x8079b62c,
54};
55
Hovland, Sigvart1d96f362018-09-25 13:23:42 +020056#define BOOT_MAGIC_ARR_SZ \
57 (sizeof boot_img_magic / sizeof boot_img_magic[0])
58
Christopher Collins92ea77f2016-12-12 15:59:26 -080059struct boot_swap_table {
David Vincze2d736ad2019-02-18 11:50:22 +010060 uint8_t magic_primary_slot;
61 uint8_t magic_secondary_slot;
62 uint8_t image_ok_primary_slot;
63 uint8_t image_ok_secondary_slot;
64 uint8_t copy_done_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -080065
Fabio Utzigb5b2f552017-06-30 10:03:47 -030066 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -080067};
68
69/**
70 * This set of tables maps image trailer contents to swap operation type.
71 * When searching for a match, these tables must be iterated sequentially.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030072 *
David Vincze2d736ad2019-02-18 11:50:22 +010073 * NOTE: the table order is very important. The settings in the secondary
74 * slot always are priority to the primary slot and should be located
75 * earlier in the table.
Fabio Utzigb5b2f552017-06-30 10:03:47 -030076 *
77 * The table lists only states where there is action needs to be taken by
78 * the bootloader, as in starting/finishing a swap operation.
Christopher Collins92ea77f2016-12-12 15:59:26 -080079 */
80static const struct boot_swap_table boot_swap_tables[] = {
81 {
David Vincze2d736ad2019-02-18 11:50:22 +010082 .magic_primary_slot = BOOT_MAGIC_ANY,
83 .magic_secondary_slot = BOOT_MAGIC_GOOD,
84 .image_ok_primary_slot = BOOT_FLAG_ANY,
85 .image_ok_secondary_slot = BOOT_FLAG_UNSET,
86 .copy_done_primary_slot = BOOT_FLAG_ANY,
87 .swap_type = BOOT_SWAP_TYPE_TEST,
Christopher Collins92ea77f2016-12-12 15:59:26 -080088 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080089 {
David Vincze2d736ad2019-02-18 11:50:22 +010090 .magic_primary_slot = BOOT_MAGIC_ANY,
91 .magic_secondary_slot = BOOT_MAGIC_GOOD,
92 .image_ok_primary_slot = BOOT_FLAG_ANY,
93 .image_ok_secondary_slot = BOOT_FLAG_SET,
94 .copy_done_primary_slot = BOOT_FLAG_ANY,
95 .swap_type = BOOT_SWAP_TYPE_PERM,
Christopher Collins92ea77f2016-12-12 15:59:26 -080096 },
Christopher Collins92ea77f2016-12-12 15:59:26 -080097 {
David Vincze2d736ad2019-02-18 11:50:22 +010098 .magic_primary_slot = BOOT_MAGIC_GOOD,
99 .magic_secondary_slot = BOOT_MAGIC_UNSET,
100 .image_ok_primary_slot = BOOT_FLAG_UNSET,
101 .image_ok_secondary_slot = BOOT_FLAG_ANY,
102 .copy_done_primary_slot = BOOT_FLAG_SET,
103 .swap_type = BOOT_SWAP_TYPE_REVERT,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800104 },
105};
106
107#define BOOT_SWAP_TABLES_COUNT \
108 (sizeof boot_swap_tables / sizeof boot_swap_tables[0])
109
Fabio Utzig39000012018-07-30 12:40:20 -0300110static int
Fabio Utzig178be542018-09-19 08:12:56 -0300111boot_magic_decode(const uint32_t *magic)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800112{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300113 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800114 return BOOT_MAGIC_GOOD;
115 }
Fabio Utzig178be542018-09-19 08:12:56 -0300116 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800117}
118
Fabio Utzig39000012018-07-30 12:40:20 -0300119static int
Fabio Utzig178be542018-09-19 08:12:56 -0300120boot_flag_decode(uint8_t flag)
Fabio Utzig39000012018-07-30 12:40:20 -0300121{
Fabio Utzig39000012018-07-30 12:40:20 -0300122 if (flag != BOOT_FLAG_SET) {
123 return BOOT_FLAG_BAD;
124 }
125 return BOOT_FLAG_SET;
126}
127
Christopher Collinsa1c12042019-05-23 14:00:28 -0700128/**
129 * Determines if a status source table is satisfied by the specified magic
130 * code.
131 *
132 * @param tbl_val A magic field from a status source table.
133 * @param val The magic value in a trailer, encoded as a
134 * BOOT_MAGIC_[...].
135 *
136 * @return 1 if the two values are compatible;
137 * 0 otherwise.
138 */
139int
140boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
141{
142 switch (tbl_val) {
143 case BOOT_MAGIC_ANY:
144 return 1;
145
146 case BOOT_MAGIC_NOTGOOD:
147 return val != BOOT_MAGIC_GOOD;
148
149 default:
150 return tbl_val == val;
151 }
152}
153
Christopher Collins92ea77f2016-12-12 15:59:26 -0800154uint32_t
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300155boot_status_sz(uint32_t min_write_sz)
156{
157 return /* state for all sectors */
158 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz;
159}
160
161uint32_t
David Brownab449182019-11-15 09:32:52 -0700162boot_trailer_sz(uint32_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800163{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300164 return /* state for all sectors */
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300165 boot_status_sz(min_write_sz) +
Fabio Utzigba829042018-09-18 08:29:34 -0300166#ifdef MCUBOOT_ENC_IMAGES
167 /* encryption keys */
Fabio Utzig4741c452019-12-19 15:32:41 -0300168# if MCUBOOT_SWAP_SAVE_ENCTLV
169 BOOT_ENC_TLV_ALIGN_SIZE * 2 +
170# else
Fabio Utzigba829042018-09-18 08:29:34 -0300171 BOOT_ENC_KEY_SIZE * 2 +
Fabio Utzig4741c452019-12-19 15:32:41 -0300172# endif
Fabio Utzigba829042018-09-18 08:29:34 -0300173#endif
Christopher Collins2adef702019-05-22 14:37:31 -0700174 /* swap_type + copy_done + image_ok + swap_size */
175 BOOT_MAX_ALIGN * 4 +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300176 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177}
178
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400179int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300180boot_status_entries(int image_index, const struct flash_area *fap)
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400181{
Fabio Utzig12d59162019-11-28 10:01:59 -0300182#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeb75c12a2019-03-22 14:58:33 +0100183 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400184 return BOOT_STATUS_STATE_COUNT;
Fabio Utzig12d59162019-11-28 10:01:59 -0300185 } else
186#endif
187 if (fap->fa_id == FLASH_AREA_IMAGE_PRIMARY(image_index) ||
Fabio Utzigb0f04732019-07-31 09:49:19 -0300188 fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vinczeb75c12a2019-03-22 14:58:33 +0100189 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400190 }
Fabio Utzig9d160092019-08-09 07:46:34 -0300191 return -1;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400192}
193
Christopher Collins92ea77f2016-12-12 15:59:26 -0800194uint32_t
195boot_status_off(const struct flash_area *fap)
196{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300197 uint32_t off_from_end;
198 uint8_t elem_sz;
199
200 elem_sz = flash_area_align(fap);
201
Christopher Collins2adef702019-05-22 14:37:31 -0700202 off_from_end = boot_trailer_sz(elem_sz);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300203
204 assert(off_from_end <= fap->fa_size);
205 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800206}
207
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300208static inline uint32_t
209boot_magic_off(const struct flash_area *fap)
210{
211 return fap->fa_size - BOOT_MAGIC_SZ;
212}
213
214static inline uint32_t
215boot_image_ok_off(const struct flash_area *fap)
216{
217 return boot_magic_off(fap) - BOOT_MAX_ALIGN;
218}
219
220static inline uint32_t
221boot_copy_done_off(const struct flash_area *fap)
222{
223 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
224}
225
Christopher Collinsa1c12042019-05-23 14:00:28 -0700226uint32_t
David Vinczee2453472019-06-17 12:31:59 +0200227boot_swap_info_off(const struct flash_area *fap)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700228{
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300229 return boot_copy_done_off(fap) - BOOT_MAX_ALIGN;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700230}
231
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300232static inline uint32_t
Fabio Utzig46490722017-09-04 15:34:32 -0300233boot_swap_size_off(const struct flash_area *fap)
234{
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300235 return boot_swap_info_off(fap) - BOOT_MAX_ALIGN;
Fabio Utzig46490722017-09-04 15:34:32 -0300236}
237
Fabio Utzigba829042018-09-18 08:29:34 -0300238#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300239static inline uint32_t
Fabio Utzigba829042018-09-18 08:29:34 -0300240boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
241{
Fabio Utzig4741c452019-12-19 15:32:41 -0300242#if MCUBOOT_SWAP_SAVE_ENCTLV
243 return boot_swap_size_off(fap) - ((slot + 1) *
244 ((((BOOT_ENC_TLV_SIZE - 1) / BOOT_MAX_ALIGN) + 1) * BOOT_MAX_ALIGN));
245#else
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300246 return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_KEY_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300247#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300248}
249#endif
250
Christopher Collins92ea77f2016-12-12 15:59:26 -0800251int
252boot_read_swap_state(const struct flash_area *fap,
253 struct boot_swap_state *state)
254{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200255 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800256 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200257 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800258 int rc;
259
260 off = boot_magic_off(fap);
Fabio Utzig178be542018-09-19 08:12:56 -0300261 rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
262 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800263 return BOOT_EFLASH;
264 }
Fabio Utzig178be542018-09-19 08:12:56 -0300265 if (rc == 1) {
266 state->magic = BOOT_MAGIC_UNSET;
267 } else {
268 state->magic = boot_magic_decode(magic);
269 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800270
David Vinczee2453472019-06-17 12:31:59 +0200271 off = boot_swap_info_off(fap);
272 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700273 if (rc < 0) {
274 return BOOT_EFLASH;
275 }
David Vinczee2453472019-06-17 12:31:59 +0200276
277 /* Extract the swap type and image number */
278 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
279 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
280
Christopher Collinsa1c12042019-05-23 14:00:28 -0700281 if (rc == 1 || state->swap_type > BOOT_SWAP_TYPE_REVERT) {
282 state->swap_type = BOOT_SWAP_TYPE_NONE;
David Vinczee2453472019-06-17 12:31:59 +0200283 state->image_num = 0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700284 }
285
Christopher Collins2adef702019-05-22 14:37:31 -0700286 off = boot_copy_done_off(fap);
287 rc = flash_area_read_is_empty(fap, off, &state->copy_done,
288 sizeof state->copy_done);
289 if (rc < 0) {
290 return BOOT_EFLASH;
291 }
292 if (rc == 1) {
293 state->copy_done = BOOT_FLAG_UNSET;
294 } else {
295 state->copy_done = boot_flag_decode(state->copy_done);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800296 }
297
298 off = boot_image_ok_off(fap);
David Vincze2d736ad2019-02-18 11:50:22 +0100299 rc = flash_area_read_is_empty(fap, off, &state->image_ok,
300 sizeof state->image_ok);
Fabio Utzig178be542018-09-19 08:12:56 -0300301 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800302 return BOOT_EFLASH;
303 }
Fabio Utzig178be542018-09-19 08:12:56 -0300304 if (rc == 1) {
305 state->image_ok = BOOT_FLAG_UNSET;
306 } else {
307 state->image_ok = boot_flag_decode(state->image_ok);
308 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800309
310 return 0;
311}
312
313/**
314 * Reads the image trailer from the scratch area.
315 */
316int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300317boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800318{
319 const struct flash_area *fap;
320 int rc;
321
Fabio Utzigb0f04732019-07-31 09:49:19 -0300322 rc = flash_area_open(flash_area_id, &fap);
323 if (rc != 0) {
324 return BOOT_EFLASH;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300325 }
326
327 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400328 flash_area_close(fap);
329 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800330}
331
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300332/**
333 * This functions tries to locate the status area after an aborted swap,
334 * by looking for the magic in the possible locations.
335 *
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300336 * If the magic is successfully found, a flash_area * is returned and it
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300337 * is the responsibility of the called to close it.
338 *
339 * @returns 0 on success, -1 on errors
340 */
341static int
342boot_find_status(int image_index, const struct flash_area **fap)
343{
344 uint32_t magic[BOOT_MAGIC_ARR_SZ];
345 uint32_t off;
346 uint8_t areas[2] = {
Fabio Utzig12d59162019-11-28 10:01:59 -0300347#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300348 FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig12d59162019-11-28 10:01:59 -0300349#endif
Fabio Utzig3c446072019-11-22 12:48:26 -0300350 FLASH_AREA_IMAGE_PRIMARY(image_index),
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300351 };
352 unsigned int i;
353 int rc;
354
355 /*
356 * In the middle a swap, tries to locate the area that is currently
357 * storing a valid magic, first on the primary slot, then on scratch.
358 * Both "slots" can end up being temporary storage for a swap and it
359 * is assumed that if magic is valid then other metadata is too,
360 * because magic is always written in the last step.
361 */
362
363 for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) {
364 rc = flash_area_open(areas[i], fap);
365 if (rc != 0) {
366 return rc;
367 }
368
369 off = boot_magic_off(*fap);
370 rc = flash_area_read(*fap, off, magic, BOOT_MAGIC_SZ);
371 if (rc != 0) {
372 flash_area_close(*fap);
373 return rc;
374 }
375
376 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
377 return 0;
378 }
379
380 flash_area_close(*fap);
381 }
382
383 /* If we got here, no magic was found */
384 return -1;
385}
386
Christopher Collins92ea77f2016-12-12 15:59:26 -0800387int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300388boot_read_swap_size(int image_index, uint32_t *swap_size)
Fabio Utzig46490722017-09-04 15:34:32 -0300389{
Fabio Utzig46490722017-09-04 15:34:32 -0300390 uint32_t off;
391 const struct flash_area *fap;
392 int rc;
393
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300394 rc = boot_find_status(image_index, &fap);
395 if (rc == 0) {
396 off = boot_swap_size_off(fap);
397 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -0300398 flash_area_close(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300399 }
400
Fabio Utzig46490722017-09-04 15:34:32 -0300401 return rc;
402}
403
Fabio Utzigba829042018-09-18 08:29:34 -0300404#ifdef MCUBOOT_ENC_IMAGES
405int
Fabio Utzig4741c452019-12-19 15:32:41 -0300406boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs)
Fabio Utzigba829042018-09-18 08:29:34 -0300407{
Fabio Utzigba829042018-09-18 08:29:34 -0300408 uint32_t off;
409 const struct flash_area *fap;
Fabio Utzig4741c452019-12-19 15:32:41 -0300410#if MCUBOOT_SWAP_SAVE_ENCTLV
411 int i;
412#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300413 int rc;
414
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300415 rc = boot_find_status(image_index, &fap);
416 if (rc == 0) {
417 off = boot_enc_key_off(fap, slot);
Fabio Utzig4741c452019-12-19 15:32:41 -0300418#if MCUBOOT_SWAP_SAVE_ENCTLV
419 rc = flash_area_read(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
420 if (rc == 0) {
421 for (i = 0; i < BOOT_ENC_TLV_ALIGN_SIZE; i++) {
422 if (bs->enctlv[slot][i] != 0xff) {
423 break;
424 }
425 }
426 /* Only try to decrypt non-erased TLV metadata */
427 if (i != BOOT_ENC_TLV_ALIGN_SIZE) {
428 rc = boot_enc_decrypt(bs->enctlv[slot], bs->enckey[slot]);
429 }
430 }
431#else
432 rc = flash_area_read(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE);
433#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300434 flash_area_close(fap);
Fabio Utzigba829042018-09-18 08:29:34 -0300435 }
436
Fabio Utzigba829042018-09-18 08:29:34 -0300437 return rc;
438}
439#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300440
441int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800442boot_write_magic(const struct flash_area *fap)
443{
444 uint32_t off;
445 int rc;
446
447 off = boot_magic_off(fap);
448
Ben McCrea4c0ee952019-08-28 09:13:24 -0700449 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600450 fap->fa_id, (unsigned long)off,
451 (unsigned long)(fap->fa_off + off));
Fabio Utzig24a273d2017-04-20 08:21:31 -0300452 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800453 if (rc != 0) {
454 return BOOT_EFLASH;
455 }
456
457 return 0;
458}
459
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300460/**
461 * Write trailer data; status bytes, swap_size, etc
462 *
463 * @returns 0 on success, != 0 on error.
464 */
Fabio Utzig2473ac02017-05-02 12:45:02 -0300465static int
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300466boot_write_trailer(const struct flash_area *fap, uint32_t off,
467 const uint8_t *inbuf, uint8_t inlen)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800468{
Fabio Utzig644b8d42017-04-20 07:56:05 -0300469 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700470 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300471 uint8_t erased_val;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700472 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800473
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200474 align = flash_area_align(fap);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300475 if (inlen > BOOT_MAX_ALIGN || align > BOOT_MAX_ALIGN) {
476 return -1;
477 }
Fabio Utzig39000012018-07-30 12:40:20 -0300478 erased_val = flash_area_erased_val(fap);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300479 if (align < inlen) {
480 align = inlen;
481 }
482 memcpy(buf, inbuf, inlen);
483 memset(&buf[inlen], erased_val, align - inlen);
David Brown9d725462017-01-23 15:50:58 -0700484
485 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800486 if (rc != 0) {
487 return BOOT_EFLASH;
488 }
489
490 return 0;
491}
492
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300493static int
494boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
495 uint8_t flag_val)
496{
497 const uint8_t buf[1] = { flag_val };
498 return boot_write_trailer(fap, off, buf, 1);
499}
500
Christopher Collins92ea77f2016-12-12 15:59:26 -0800501int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300502boot_write_copy_done(const struct flash_area *fap)
503{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700504 uint32_t off;
505
506 off = boot_copy_done_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700507 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600508 fap->fa_id, (unsigned long)off,
509 (unsigned long)(fap->fa_off + off));
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300510 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300511}
512
513int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800514boot_write_image_ok(const struct flash_area *fap)
515{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700516 uint32_t off;
517
518 off = boot_image_ok_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700519 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600520 fap->fa_id, (unsigned long)off,
521 (unsigned long)(fap->fa_off + off));
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300522 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700523}
524
525/**
526 * Writes the specified value to the `swap-type` field of an image trailer.
527 * This value is persisted so that the boot loader knows what swap operation to
528 * resume in case of an unexpected reset.
529 */
530int
David Vinczee2453472019-06-17 12:31:59 +0200531boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
532 uint8_t image_num)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700533{
534 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200535 uint8_t swap_info;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700536
David Vinczee2453472019-06-17 12:31:59 +0200537 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
538 off = boot_swap_info_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700539 BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%lx (0x%lx), swap_type=0x%x"
David Vinczee2453472019-06-17 12:31:59 +0200540 " image_num=0x%x",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600541 fap->fa_id, (unsigned long)off,
542 (unsigned long)(fap->fa_off + off), swap_type, image_num);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300543 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800544}
545
546int
Fabio Utzig46490722017-09-04 15:34:32 -0300547boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
548{
549 uint32_t off;
Fabio Utzig46490722017-09-04 15:34:32 -0300550
551 off = boot_swap_size_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700552 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600553 fap->fa_id, (unsigned long)off,
554 (unsigned long)fap->fa_off + off);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300555 return boot_write_trailer(fap, off, (const uint8_t *) &swap_size, 4);
Fabio Utzig46490722017-09-04 15:34:32 -0300556}
557
Fabio Utzigba829042018-09-18 08:29:34 -0300558#ifdef MCUBOOT_ENC_IMAGES
559int
Fabio Utzig4741c452019-12-19 15:32:41 -0300560boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
561 const struct boot_status *bs)
Fabio Utzigba829042018-09-18 08:29:34 -0300562{
563 uint32_t off;
564 int rc;
565
566 off = boot_enc_key_off(fap, slot);
Fabio Utzig5e6ea222019-12-10 09:49:59 -0300567 BOOT_LOG_DBG("writing enc_key; fa_id=%d off=0x%lx (0x%lx)",
568 fap->fa_id, (unsigned long)off,
569 (unsigned long)fap->fa_off + off);
Fabio Utzig4741c452019-12-19 15:32:41 -0300570#if MCUBOOT_SWAP_SAVE_ENCTLV
571 rc = flash_area_write(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
572#else
573 rc = flash_area_write(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE);
574#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300575 if (rc != 0) {
576 return BOOT_EFLASH;
577 }
578
579 return 0;
580}
581#endif
582
Fabio Utzig46490722017-09-04 15:34:32 -0300583int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300584boot_swap_type_multi(int image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800585{
586 const struct boot_swap_table *table;
David Vincze2d736ad2019-02-18 11:50:22 +0100587 struct boot_swap_state primary_slot;
588 struct boot_swap_state secondary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800589 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200590 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800591
Fabio Utzigb0f04732019-07-31 09:49:19 -0300592 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
593 &primary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300594 if (rc) {
595 return BOOT_SWAP_TYPE_PANIC;
596 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800597
Fabio Utzigb0f04732019-07-31 09:49:19 -0300598 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
David Vincze2d736ad2019-02-18 11:50:22 +0100599 &secondary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300600 if (rc) {
601 return BOOT_SWAP_TYPE_PANIC;
602 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800603
604 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
605 table = boot_swap_tables + i;
606
Christopher Collinsa1c12042019-05-23 14:00:28 -0700607 if (boot_magic_compatible_check(table->magic_primary_slot,
608 primary_slot.magic) &&
609 boot_magic_compatible_check(table->magic_secondary_slot,
610 secondary_slot.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100611 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
612 table->image_ok_primary_slot == primary_slot.image_ok) &&
613 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
614 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
615 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
616 table->copy_done_primary_slot == primary_slot.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400617 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300618 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
619 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
620 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
621 "BUG; can't happen");
Fabio Utzigf0dbd422019-08-09 10:22:05 -0300622 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
623 table->swap_type != BOOT_SWAP_TYPE_PERM &&
624 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
625 return BOOT_SWAP_TYPE_PANIC;
626 }
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300627 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800628 }
629 }
630
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300631 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800632 return BOOT_SWAP_TYPE_NONE;
633}
634
Fabio Utzig75e9a592019-08-27 09:10:36 -0300635/*
636 * This function is not used by the bootloader itself, but its required API
637 * by external tooling like mcumgr.
638 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300639int
640boot_swap_type(void)
641{
642 return boot_swap_type_multi(0);
643}
644
Christopher Collins92ea77f2016-12-12 15:59:26 -0800645/**
David Vincze2d736ad2019-02-18 11:50:22 +0100646 * Marks the image in the secondary slot as pending. On the next reboot,
647 * the system will perform a one-time boot of the the secondary slot image.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800648 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800649 * @param permanent Whether the image should be used permanently or
650 * only tested once:
651 * 0=run image once, then confirm or revert.
652 * 1=run image forever.
653 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800654 * @return 0 on success; nonzero on failure.
655 */
656int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800657boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800658{
659 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100660 struct boot_swap_state state_secondary_slot;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700661 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800662 int rc;
663
Fabio Utzigb0f04732019-07-31 09:49:19 -0300664 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(0),
David Vincze2d736ad2019-02-18 11:50:22 +0100665 &state_secondary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800666 if (rc != 0) {
667 return rc;
668 }
669
David Vincze2d736ad2019-02-18 11:50:22 +0100670 switch (state_secondary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800671 case BOOT_MAGIC_GOOD:
672 /* Swap already scheduled. */
673 return 0;
674
675 case BOOT_MAGIC_UNSET:
Fabio Utzigb0f04732019-07-31 09:49:19 -0300676 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800677 if (rc != 0) {
678 rc = BOOT_EFLASH;
679 } else {
680 rc = boot_write_magic(fap);
681 }
682
Christopher Collins7835c1e2016-12-21 10:10:51 -0800683 if (rc == 0 && permanent) {
684 rc = boot_write_image_ok(fap);
685 }
686
Christopher Collinsa1c12042019-05-23 14:00:28 -0700687 if (rc == 0) {
688 if (permanent) {
689 swap_type = BOOT_SWAP_TYPE_PERM;
690 } else {
691 swap_type = BOOT_SWAP_TYPE_TEST;
692 }
David Vinczee2453472019-06-17 12:31:59 +0200693 rc = boot_write_swap_info(fap, swap_type, 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700694 }
695
Christopher Collins92ea77f2016-12-12 15:59:26 -0800696 flash_area_close(fap);
697 return rc;
698
Christopher Collinsae01f152019-01-30 09:56:37 -0800699 case BOOT_MAGIC_BAD:
700 /* The image slot is corrupt. There is no way to recover, so erase the
701 * slot to allow future upgrades.
702 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300703 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Christopher Collinsae01f152019-01-30 09:56:37 -0800704 if (rc != 0) {
705 return BOOT_EFLASH;
706 }
707
708 flash_area_erase(fap, 0, fap->fa_size);
709 flash_area_close(fap);
710 return BOOT_EBADIMAGE;
711
Christopher Collins92ea77f2016-12-12 15:59:26 -0800712 default:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800713 assert(0);
Christopher Collinsae01f152019-01-30 09:56:37 -0800714 return BOOT_EBADIMAGE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800715 }
716}
717
718/**
David Vincze2d736ad2019-02-18 11:50:22 +0100719 * Marks the image in the primary slot as confirmed. The system will continue
720 * booting into the image in the primary slot until told to boot from a
721 * different slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800722 *
723 * @return 0 on success; nonzero on failure.
724 */
725int
726boot_set_confirmed(void)
727{
728 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100729 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800730 int rc;
731
Fabio Utzigb0f04732019-07-31 09:49:19 -0300732 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(0),
David Vincze2d736ad2019-02-18 11:50:22 +0100733 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800734 if (rc != 0) {
735 return rc;
736 }
737
David Vincze2d736ad2019-02-18 11:50:22 +0100738 switch (state_primary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800739 case BOOT_MAGIC_GOOD:
740 /* Confirm needed; proceed. */
741 break;
742
743 case BOOT_MAGIC_UNSET:
744 /* Already confirmed. */
745 return 0;
746
747 case BOOT_MAGIC_BAD:
748 /* Unexpected state. */
749 return BOOT_EBADVECT;
750 }
751
Fabio Utzigb0f04732019-07-31 09:49:19 -0300752 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800753 if (rc) {
754 rc = BOOT_EFLASH;
755 goto done;
756 }
757
David Vincze2d736ad2019-02-18 11:50:22 +0100758 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300759 /* Swap never completed. This is unexpected. */
760 rc = BOOT_EBADVECT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800761 goto done;
762 }
763
David Vincze2d736ad2019-02-18 11:50:22 +0100764 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300765 /* Already confirmed. */
766 goto done;
767 }
768
769 rc = boot_write_image_ok(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800770
771done:
772 flash_area_close(fap);
773 return rc;
774}