blob: 57ceff4a27a1f29e1b71aeed3fc720637f4313f0 [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
Raef Colese8fe6cf2020-05-26 13:07:40 +01006 * Copyright (c) 2019-2020 Arm Limited
David Brownaac71112020-02-03 16:13:42 -07007 *
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
Christopher Collins92ea77f2016-12-12 15:59:26 -080028#include <string.h>
29#include <inttypes.h>
Fabio Utziga0bc9b52017-06-28 09:19:55 -030030#include <stddef.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080031
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include "sysflash/sysflash.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020033#include "flash_map_backend/flash_map_backend.h"
34
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include "bootutil/image.h"
36#include "bootutil/bootutil.h"
37#include "bootutil_priv.h"
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030038#include "bootutil/bootutil_log.h"
Raef Colese8fe6cf2020-05-26 13:07:40 +010039#include "bootutil/fault_injection_hardening.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
Raef Colese8fe6cf2020-05-26 13:07:40 +0100110/**
111 * @brief Determine if the data at two memory addresses is equal
112 *
113 * @param s1 The first memory region to compare.
114 * @param s2 The second memory region to compare.
115 * @param n The amount of bytes to compare.
116 *
117 * @note This function does not comply with the specification of memcmp,
118 * so should not be considered a drop-in replacement. It has no
119 * constant time execution. The point is to make sure that all the
120 * bytes are compared and detect if loop was abused and some cycles
121 * was skipped due to fault injection.
122 *
123 * @return FIH_SUCCESS if memory regions are equal, otherwise FIH_FAILURE
124 */
125#ifdef MCUBOOT_FIH_PROFILE_OFF
126inline
127fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n)
128{
129 return memcmp(s1, s2, n);
130}
131#else
132fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n)
133{
134 size_t i;
135 uint8_t *s1_p = (uint8_t*) s1;
136 uint8_t *s2_p = (uint8_t*) s2;
137 fih_int ret = FIH_FAILURE;
138
139 for (i = 0; i < n; i++) {
140 if (s1_p[i] != s2_p[i]) {
141 goto out;
142 }
143 }
144 if (i == n) {
145 ret = FIH_SUCCESS;
146 }
147
148out:
149 FIH_RET(ret);
150}
151#endif
152
Fabio Utzig39000012018-07-30 12:40:20 -0300153static int
Fabio Utzig178be542018-09-19 08:12:56 -0300154boot_magic_decode(const uint32_t *magic)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800155{
Fabio Utzig24a273d2017-04-20 08:21:31 -0300156 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800157 return BOOT_MAGIC_GOOD;
158 }
Fabio Utzig178be542018-09-19 08:12:56 -0300159 return BOOT_MAGIC_BAD;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800160}
161
Fabio Utzig39000012018-07-30 12:40:20 -0300162static int
Fabio Utzig178be542018-09-19 08:12:56 -0300163boot_flag_decode(uint8_t flag)
Fabio Utzig39000012018-07-30 12:40:20 -0300164{
Fabio Utzig39000012018-07-30 12:40:20 -0300165 if (flag != BOOT_FLAG_SET) {
166 return BOOT_FLAG_BAD;
167 }
168 return BOOT_FLAG_SET;
169}
170
Christopher Collinsa1c12042019-05-23 14:00:28 -0700171/**
172 * Determines if a status source table is satisfied by the specified magic
173 * code.
174 *
175 * @param tbl_val A magic field from a status source table.
176 * @param val The magic value in a trailer, encoded as a
177 * BOOT_MAGIC_[...].
178 *
179 * @return 1 if the two values are compatible;
180 * 0 otherwise.
181 */
182int
183boot_magic_compatible_check(uint8_t tbl_val, uint8_t val)
184{
185 switch (tbl_val) {
186 case BOOT_MAGIC_ANY:
187 return 1;
188
189 case BOOT_MAGIC_NOTGOOD:
190 return val != BOOT_MAGIC_GOOD;
191
192 default:
193 return tbl_val == val;
194 }
195}
196
Christopher Collins92ea77f2016-12-12 15:59:26 -0800197uint32_t
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300198boot_status_sz(uint32_t min_write_sz)
199{
200 return /* state for all sectors */
201 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz;
202}
203
204uint32_t
David Brownab449182019-11-15 09:32:52 -0700205boot_trailer_sz(uint32_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800206{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300207 return /* state for all sectors */
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300208 boot_status_sz(min_write_sz) +
Fabio Utzigba829042018-09-18 08:29:34 -0300209#ifdef MCUBOOT_ENC_IMAGES
210 /* encryption keys */
Fabio Utzig4741c452019-12-19 15:32:41 -0300211# if MCUBOOT_SWAP_SAVE_ENCTLV
212 BOOT_ENC_TLV_ALIGN_SIZE * 2 +
213# else
Fabio Utzigba829042018-09-18 08:29:34 -0300214 BOOT_ENC_KEY_SIZE * 2 +
Fabio Utzig4741c452019-12-19 15:32:41 -0300215# endif
Fabio Utzigba829042018-09-18 08:29:34 -0300216#endif
Christopher Collins2adef702019-05-22 14:37:31 -0700217 /* swap_type + copy_done + image_ok + swap_size */
218 BOOT_MAX_ALIGN * 4 +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300219 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800220}
221
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400222int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300223boot_status_entries(int image_index, const struct flash_area *fap)
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400224{
Fabio Utzig12d59162019-11-28 10:01:59 -0300225#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeb75c12a2019-03-22 14:58:33 +0100226 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400227 return BOOT_STATUS_STATE_COUNT;
Fabio Utzig12d59162019-11-28 10:01:59 -0300228 } else
229#endif
230 if (fap->fa_id == FLASH_AREA_IMAGE_PRIMARY(image_index) ||
Fabio Utzigb0f04732019-07-31 09:49:19 -0300231 fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vinczeb75c12a2019-03-22 14:58:33 +0100232 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400233 }
Fabio Utzig9d160092019-08-09 07:46:34 -0300234 return -1;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400235}
236
Christopher Collins92ea77f2016-12-12 15:59:26 -0800237uint32_t
238boot_status_off(const struct flash_area *fap)
239{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300240 uint32_t off_from_end;
241 uint8_t elem_sz;
242
243 elem_sz = flash_area_align(fap);
244
Christopher Collins2adef702019-05-22 14:37:31 -0700245 off_from_end = boot_trailer_sz(elem_sz);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300246
247 assert(off_from_end <= fap->fa_size);
248 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800249}
250
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300251static inline uint32_t
252boot_magic_off(const struct flash_area *fap)
253{
254 return fap->fa_size - BOOT_MAGIC_SZ;
255}
256
257static inline uint32_t
258boot_image_ok_off(const struct flash_area *fap)
259{
260 return boot_magic_off(fap) - BOOT_MAX_ALIGN;
261}
262
263static inline uint32_t
264boot_copy_done_off(const struct flash_area *fap)
265{
266 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
267}
268
Christopher Collinsa1c12042019-05-23 14:00:28 -0700269uint32_t
David Vinczee2453472019-06-17 12:31:59 +0200270boot_swap_info_off(const struct flash_area *fap)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700271{
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300272 return boot_copy_done_off(fap) - BOOT_MAX_ALIGN;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700273}
274
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300275static inline uint32_t
Fabio Utzig46490722017-09-04 15:34:32 -0300276boot_swap_size_off(const struct flash_area *fap)
277{
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300278 return boot_swap_info_off(fap) - BOOT_MAX_ALIGN;
Fabio Utzig46490722017-09-04 15:34:32 -0300279}
280
Fabio Utzigba829042018-09-18 08:29:34 -0300281#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300282static inline uint32_t
Fabio Utzigba829042018-09-18 08:29:34 -0300283boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
284{
Fabio Utzig4741c452019-12-19 15:32:41 -0300285#if MCUBOOT_SWAP_SAVE_ENCTLV
286 return boot_swap_size_off(fap) - ((slot + 1) *
287 ((((BOOT_ENC_TLV_SIZE - 1) / BOOT_MAX_ALIGN) + 1) * BOOT_MAX_ALIGN));
288#else
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300289 return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_KEY_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300290#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300291}
292#endif
293
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300294bool bootutil_buffer_is_erased(const struct flash_area *area,
295 const void *buffer, size_t len)
296{
297 size_t i;
298 uint8_t *u8b;
299 uint8_t erased_val;
300
301 if (buffer == NULL || len == 0) {
302 return false;
303 }
304
305 erased_val = flash_area_erased_val(area);
306 for (i = 0, u8b = (uint8_t *)buffer; i < len; i++) {
307 if (u8b[i] != erased_val) {
308 return false;
309 }
310 }
311
312 return true;
313}
314
Christopher Collins92ea77f2016-12-12 15:59:26 -0800315int
316boot_read_swap_state(const struct flash_area *fap,
317 struct boot_swap_state *state)
318{
Hovland, Sigvart1d96f362018-09-25 13:23:42 +0200319 uint32_t magic[BOOT_MAGIC_ARR_SZ];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800320 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200321 uint8_t swap_info;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800322 int rc;
323
324 off = boot_magic_off(fap);
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300325 rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
Fabio Utzig178be542018-09-19 08:12:56 -0300326 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800327 return BOOT_EFLASH;
328 }
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300329 if (bootutil_buffer_is_erased(fap, magic, BOOT_MAGIC_SZ)) {
Fabio Utzig178be542018-09-19 08:12:56 -0300330 state->magic = BOOT_MAGIC_UNSET;
331 } else {
332 state->magic = boot_magic_decode(magic);
333 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800334
David Vinczee2453472019-06-17 12:31:59 +0200335 off = boot_swap_info_off(fap);
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300336 rc = flash_area_read(fap, off, &swap_info, sizeof swap_info);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700337 if (rc < 0) {
338 return BOOT_EFLASH;
339 }
David Vinczee2453472019-06-17 12:31:59 +0200340
341 /* Extract the swap type and image number */
342 state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
343 state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
344
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300345 if (bootutil_buffer_is_erased(fap, &swap_info, sizeof swap_info) ||
346 state->swap_type > BOOT_SWAP_TYPE_REVERT) {
Christopher Collinsa1c12042019-05-23 14:00:28 -0700347 state->swap_type = BOOT_SWAP_TYPE_NONE;
David Vinczee2453472019-06-17 12:31:59 +0200348 state->image_num = 0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700349 }
350
Christopher Collins2adef702019-05-22 14:37:31 -0700351 off = boot_copy_done_off(fap);
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300352 rc = flash_area_read(fap, off, &state->copy_done, sizeof state->copy_done);
Christopher Collins2adef702019-05-22 14:37:31 -0700353 if (rc < 0) {
354 return BOOT_EFLASH;
355 }
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300356 if (bootutil_buffer_is_erased(fap, &state->copy_done,
357 sizeof state->copy_done)) {
Christopher Collins2adef702019-05-22 14:37:31 -0700358 state->copy_done = BOOT_FLAG_UNSET;
359 } else {
360 state->copy_done = boot_flag_decode(state->copy_done);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800361 }
362
363 off = boot_image_ok_off(fap);
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300364 rc = flash_area_read(fap, off, &state->image_ok, sizeof state->image_ok);
Fabio Utzig178be542018-09-19 08:12:56 -0300365 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800366 return BOOT_EFLASH;
367 }
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300368 if (bootutil_buffer_is_erased(fap, &state->image_ok,
369 sizeof state->image_ok)) {
Fabio Utzig178be542018-09-19 08:12:56 -0300370 state->image_ok = BOOT_FLAG_UNSET;
371 } else {
372 state->image_ok = boot_flag_decode(state->image_ok);
373 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800374
375 return 0;
376}
377
378/**
379 * Reads the image trailer from the scratch area.
380 */
381int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300382boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800383{
384 const struct flash_area *fap;
385 int rc;
386
Fabio Utzigb0f04732019-07-31 09:49:19 -0300387 rc = flash_area_open(flash_area_id, &fap);
388 if (rc != 0) {
389 return BOOT_EFLASH;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300390 }
391
392 rc = boot_read_swap_state(fap, state);
Fabio Utzigacfba2e2017-05-22 11:06:29 -0400393 flash_area_close(fap);
394 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800395}
396
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300397/**
398 * This functions tries to locate the status area after an aborted swap,
399 * by looking for the magic in the possible locations.
400 *
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300401 * If the magic is successfully found, a flash_area * is returned and it
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300402 * is the responsibility of the called to close it.
403 *
404 * @returns 0 on success, -1 on errors
405 */
406static int
407boot_find_status(int image_index, const struct flash_area **fap)
408{
409 uint32_t magic[BOOT_MAGIC_ARR_SZ];
410 uint32_t off;
411 uint8_t areas[2] = {
Fabio Utzig12d59162019-11-28 10:01:59 -0300412#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300413 FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig12d59162019-11-28 10:01:59 -0300414#endif
Fabio Utzig3c446072019-11-22 12:48:26 -0300415 FLASH_AREA_IMAGE_PRIMARY(image_index),
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300416 };
417 unsigned int i;
418 int rc;
419
420 /*
421 * In the middle a swap, tries to locate the area that is currently
422 * storing a valid magic, first on the primary slot, then on scratch.
423 * Both "slots" can end up being temporary storage for a swap and it
424 * is assumed that if magic is valid then other metadata is too,
425 * because magic is always written in the last step.
426 */
427
428 for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) {
429 rc = flash_area_open(areas[i], fap);
430 if (rc != 0) {
431 return rc;
432 }
433
434 off = boot_magic_off(*fap);
435 rc = flash_area_read(*fap, off, magic, BOOT_MAGIC_SZ);
436 if (rc != 0) {
437 flash_area_close(*fap);
438 return rc;
439 }
440
441 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
442 return 0;
443 }
444
445 flash_area_close(*fap);
446 }
447
448 /* If we got here, no magic was found */
449 return -1;
450}
451
Christopher Collins92ea77f2016-12-12 15:59:26 -0800452int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300453boot_read_swap_size(int image_index, uint32_t *swap_size)
Fabio Utzig46490722017-09-04 15:34:32 -0300454{
Fabio Utzig46490722017-09-04 15:34:32 -0300455 uint32_t off;
456 const struct flash_area *fap;
457 int rc;
458
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300459 rc = boot_find_status(image_index, &fap);
460 if (rc == 0) {
461 off = boot_swap_size_off(fap);
462 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -0300463 flash_area_close(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300464 }
465
Fabio Utzig46490722017-09-04 15:34:32 -0300466 return rc;
467}
468
Fabio Utzigba829042018-09-18 08:29:34 -0300469#ifdef MCUBOOT_ENC_IMAGES
470int
Fabio Utzig4741c452019-12-19 15:32:41 -0300471boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs)
Fabio Utzigba829042018-09-18 08:29:34 -0300472{
Fabio Utzigba829042018-09-18 08:29:34 -0300473 uint32_t off;
474 const struct flash_area *fap;
Fabio Utzig4741c452019-12-19 15:32:41 -0300475#if MCUBOOT_SWAP_SAVE_ENCTLV
476 int i;
477#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300478 int rc;
479
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300480 rc = boot_find_status(image_index, &fap);
481 if (rc == 0) {
482 off = boot_enc_key_off(fap, slot);
Fabio Utzig4741c452019-12-19 15:32:41 -0300483#if MCUBOOT_SWAP_SAVE_ENCTLV
484 rc = flash_area_read(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
485 if (rc == 0) {
486 for (i = 0; i < BOOT_ENC_TLV_ALIGN_SIZE; i++) {
487 if (bs->enctlv[slot][i] != 0xff) {
488 break;
489 }
490 }
491 /* Only try to decrypt non-erased TLV metadata */
492 if (i != BOOT_ENC_TLV_ALIGN_SIZE) {
493 rc = boot_enc_decrypt(bs->enctlv[slot], bs->enckey[slot]);
494 }
495 }
496#else
497 rc = flash_area_read(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE);
498#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300499 flash_area_close(fap);
Fabio Utzigba829042018-09-18 08:29:34 -0300500 }
501
Fabio Utzigba829042018-09-18 08:29:34 -0300502 return rc;
503}
504#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300505
506int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800507boot_write_magic(const struct flash_area *fap)
508{
509 uint32_t off;
510 int rc;
511
512 off = boot_magic_off(fap);
513
Ben McCrea4c0ee952019-08-28 09:13:24 -0700514 BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600515 fap->fa_id, (unsigned long)off,
516 (unsigned long)(fap->fa_off + off));
Fabio Utzig24a273d2017-04-20 08:21:31 -0300517 rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800518 if (rc != 0) {
519 return BOOT_EFLASH;
520 }
521
522 return 0;
523}
524
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300525/**
526 * Write trailer data; status bytes, swap_size, etc
527 *
528 * @returns 0 on success, != 0 on error.
529 */
Fabio Utzig2473ac02017-05-02 12:45:02 -0300530static int
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300531boot_write_trailer(const struct flash_area *fap, uint32_t off,
532 const uint8_t *inbuf, uint8_t inlen)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800533{
Fabio Utzig644b8d42017-04-20 07:56:05 -0300534 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700535 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300536 uint8_t erased_val;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700537 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800538
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200539 align = flash_area_align(fap);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300540 if (inlen > BOOT_MAX_ALIGN || align > BOOT_MAX_ALIGN) {
541 return -1;
542 }
Fabio Utzig39000012018-07-30 12:40:20 -0300543 erased_val = flash_area_erased_val(fap);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300544 if (align < inlen) {
545 align = inlen;
546 }
547 memcpy(buf, inbuf, inlen);
548 memset(&buf[inlen], erased_val, align - inlen);
David Brown9d725462017-01-23 15:50:58 -0700549
550 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800551 if (rc != 0) {
552 return BOOT_EFLASH;
553 }
554
555 return 0;
556}
557
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300558static int
559boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
560 uint8_t flag_val)
561{
562 const uint8_t buf[1] = { flag_val };
563 return boot_write_trailer(fap, off, buf, 1);
564}
565
Christopher Collins92ea77f2016-12-12 15:59:26 -0800566int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300567boot_write_copy_done(const struct flash_area *fap)
568{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700569 uint32_t off;
570
571 off = boot_copy_done_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700572 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600573 fap->fa_id, (unsigned long)off,
574 (unsigned long)(fap->fa_off + off));
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300575 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300576}
577
578int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800579boot_write_image_ok(const struct flash_area *fap)
580{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700581 uint32_t off;
582
583 off = boot_image_ok_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700584 BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600585 fap->fa_id, (unsigned long)off,
586 (unsigned long)(fap->fa_off + off));
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300587 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700588}
589
590/**
591 * Writes the specified value to the `swap-type` field of an image trailer.
592 * This value is persisted so that the boot loader knows what swap operation to
593 * resume in case of an unexpected reset.
594 */
595int
David Vinczee2453472019-06-17 12:31:59 +0200596boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
597 uint8_t image_num)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700598{
599 uint32_t off;
David Vinczee2453472019-06-17 12:31:59 +0200600 uint8_t swap_info;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700601
David Vinczee2453472019-06-17 12:31:59 +0200602 BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type);
603 off = boot_swap_info_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700604 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 +0200605 " image_num=0x%x",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600606 fap->fa_id, (unsigned long)off,
607 (unsigned long)(fap->fa_off + off), swap_type, image_num);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300608 return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800609}
610
611int
Fabio Utzig46490722017-09-04 15:34:32 -0300612boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
613{
614 uint32_t off;
Fabio Utzig46490722017-09-04 15:34:32 -0300615
616 off = boot_swap_size_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700617 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600618 fap->fa_id, (unsigned long)off,
619 (unsigned long)fap->fa_off + off);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300620 return boot_write_trailer(fap, off, (const uint8_t *) &swap_size, 4);
Fabio Utzig46490722017-09-04 15:34:32 -0300621}
622
Fabio Utzigba829042018-09-18 08:29:34 -0300623#ifdef MCUBOOT_ENC_IMAGES
624int
Fabio Utzig4741c452019-12-19 15:32:41 -0300625boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
626 const struct boot_status *bs)
Fabio Utzigba829042018-09-18 08:29:34 -0300627{
628 uint32_t off;
629 int rc;
630
631 off = boot_enc_key_off(fap, slot);
Fabio Utzig5e6ea222019-12-10 09:49:59 -0300632 BOOT_LOG_DBG("writing enc_key; fa_id=%d off=0x%lx (0x%lx)",
633 fap->fa_id, (unsigned long)off,
634 (unsigned long)fap->fa_off + off);
Fabio Utzig4741c452019-12-19 15:32:41 -0300635#if MCUBOOT_SWAP_SAVE_ENCTLV
636 rc = flash_area_write(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
637#else
638 rc = flash_area_write(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE);
639#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300640 if (rc != 0) {
641 return BOOT_EFLASH;
642 }
643
644 return 0;
645}
646#endif
647
Fabio Utzig46490722017-09-04 15:34:32 -0300648int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300649boot_swap_type_multi(int image_index)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800650{
651 const struct boot_swap_table *table;
David Vincze2d736ad2019-02-18 11:50:22 +0100652 struct boot_swap_state primary_slot;
653 struct boot_swap_state secondary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800654 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200655 size_t i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800656
Fabio Utzigb0f04732019-07-31 09:49:19 -0300657 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
658 &primary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300659 if (rc) {
660 return BOOT_SWAP_TYPE_PANIC;
661 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800662
Fabio Utzigb0f04732019-07-31 09:49:19 -0300663 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
David Vincze2d736ad2019-02-18 11:50:22 +0100664 &secondary_slot);
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300665 if (rc) {
666 return BOOT_SWAP_TYPE_PANIC;
667 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800668
669 for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
670 table = boot_swap_tables + i;
671
Christopher Collinsa1c12042019-05-23 14:00:28 -0700672 if (boot_magic_compatible_check(table->magic_primary_slot,
673 primary_slot.magic) &&
674 boot_magic_compatible_check(table->magic_secondary_slot,
675 secondary_slot.magic) &&
David Vincze2d736ad2019-02-18 11:50:22 +0100676 (table->image_ok_primary_slot == BOOT_FLAG_ANY ||
677 table->image_ok_primary_slot == primary_slot.image_ok) &&
678 (table->image_ok_secondary_slot == BOOT_FLAG_ANY ||
679 table->image_ok_secondary_slot == secondary_slot.image_ok) &&
680 (table->copy_done_primary_slot == BOOT_FLAG_ANY ||
681 table->copy_done_primary_slot == primary_slot.copy_done)) {
Fabio Utzig34e393e2017-05-22 11:07:07 -0400682 BOOT_LOG_INF("Swap type: %s",
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300683 table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" :
684 table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" :
685 table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" :
686 "BUG; can't happen");
Fabio Utzigf0dbd422019-08-09 10:22:05 -0300687 if (table->swap_type != BOOT_SWAP_TYPE_TEST &&
688 table->swap_type != BOOT_SWAP_TYPE_PERM &&
689 table->swap_type != BOOT_SWAP_TYPE_REVERT) {
690 return BOOT_SWAP_TYPE_PANIC;
691 }
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300692 return table->swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800693 }
694 }
695
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300696 BOOT_LOG_INF("Swap type: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800697 return BOOT_SWAP_TYPE_NONE;
698}
699
Fabio Utzig75e9a592019-08-27 09:10:36 -0300700/*
701 * This function is not used by the bootloader itself, but its required API
702 * by external tooling like mcumgr.
703 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300704int
705boot_swap_type(void)
706{
707 return boot_swap_type_multi(0);
708}
709
Christopher Collins92ea77f2016-12-12 15:59:26 -0800710/**
David Vincze2d736ad2019-02-18 11:50:22 +0100711 * Marks the image in the secondary slot as pending. On the next reboot,
712 * the system will perform a one-time boot of the the secondary slot image.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800713 *
Christopher Collins7835c1e2016-12-21 10:10:51 -0800714 * @param permanent Whether the image should be used permanently or
715 * only tested once:
716 * 0=run image once, then confirm or revert.
717 * 1=run image forever.
718 *
Christopher Collins92ea77f2016-12-12 15:59:26 -0800719 * @return 0 on success; nonzero on failure.
720 */
721int
Christopher Collins7835c1e2016-12-21 10:10:51 -0800722boot_set_pending(int permanent)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800723{
724 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100725 struct boot_swap_state state_secondary_slot;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700726 uint8_t swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800727 int rc;
728
Fabio Utzigb0f04732019-07-31 09:49:19 -0300729 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(0),
David Vincze2d736ad2019-02-18 11:50:22 +0100730 &state_secondary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800731 if (rc != 0) {
732 return rc;
733 }
734
David Vincze2d736ad2019-02-18 11:50:22 +0100735 switch (state_secondary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800736 case BOOT_MAGIC_GOOD:
737 /* Swap already scheduled. */
738 return 0;
739
740 case BOOT_MAGIC_UNSET:
Fabio Utzigb0f04732019-07-31 09:49:19 -0300741 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800742 if (rc != 0) {
743 rc = BOOT_EFLASH;
744 } else {
745 rc = boot_write_magic(fap);
746 }
747
Christopher Collins7835c1e2016-12-21 10:10:51 -0800748 if (rc == 0 && permanent) {
749 rc = boot_write_image_ok(fap);
750 }
751
Christopher Collinsa1c12042019-05-23 14:00:28 -0700752 if (rc == 0) {
753 if (permanent) {
754 swap_type = BOOT_SWAP_TYPE_PERM;
755 } else {
756 swap_type = BOOT_SWAP_TYPE_TEST;
757 }
David Vinczee2453472019-06-17 12:31:59 +0200758 rc = boot_write_swap_info(fap, swap_type, 0);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700759 }
760
Christopher Collins92ea77f2016-12-12 15:59:26 -0800761 flash_area_close(fap);
762 return rc;
763
Christopher Collinsae01f152019-01-30 09:56:37 -0800764 case BOOT_MAGIC_BAD:
765 /* The image slot is corrupt. There is no way to recover, so erase the
766 * slot to allow future upgrades.
767 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300768 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap);
Christopher Collinsae01f152019-01-30 09:56:37 -0800769 if (rc != 0) {
770 return BOOT_EFLASH;
771 }
772
773 flash_area_erase(fap, 0, fap->fa_size);
774 flash_area_close(fap);
775 return BOOT_EBADIMAGE;
776
Christopher Collins92ea77f2016-12-12 15:59:26 -0800777 default:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800778 assert(0);
Christopher Collinsae01f152019-01-30 09:56:37 -0800779 return BOOT_EBADIMAGE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800780 }
781}
782
783/**
David Vincze2d736ad2019-02-18 11:50:22 +0100784 * Marks the image in the primary slot as confirmed. The system will continue
785 * booting into the image in the primary slot until told to boot from a
786 * different slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800787 *
788 * @return 0 on success; nonzero on failure.
789 */
790int
791boot_set_confirmed(void)
792{
793 const struct flash_area *fap;
David Vincze2d736ad2019-02-18 11:50:22 +0100794 struct boot_swap_state state_primary_slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800795 int rc;
796
Fabio Utzigb0f04732019-07-31 09:49:19 -0300797 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(0),
David Vincze2d736ad2019-02-18 11:50:22 +0100798 &state_primary_slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800799 if (rc != 0) {
800 return rc;
801 }
802
David Vincze2d736ad2019-02-18 11:50:22 +0100803 switch (state_primary_slot.magic) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800804 case BOOT_MAGIC_GOOD:
805 /* Confirm needed; proceed. */
806 break;
807
808 case BOOT_MAGIC_UNSET:
809 /* Already confirmed. */
810 return 0;
811
812 case BOOT_MAGIC_BAD:
813 /* Unexpected state. */
814 return BOOT_EBADVECT;
815 }
816
Fabio Utzigb0f04732019-07-31 09:49:19 -0300817 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800818 if (rc) {
819 rc = BOOT_EFLASH;
820 goto done;
821 }
822
David Vincze2d736ad2019-02-18 11:50:22 +0100823 if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300824 /* Swap never completed. This is unexpected. */
825 rc = BOOT_EBADVECT;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800826 goto done;
827 }
828
David Vincze2d736ad2019-02-18 11:50:22 +0100829 if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) {
Fabio Utzig39000012018-07-30 12:40:20 -0300830 /* Already confirmed. */
831 goto done;
832 }
833
834 rc = boot_write_image_ok(fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800835
836done:
837 flash_area_close(fap);
838 return rc;
839}