blob: 221d8a6a234db376562d951dff1c21d6c853afd1 [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
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +020044#ifdef MCUBOOT_SWAP_USING_STATUS
45#include "swap_status.h"
46#endif
47
48#include "mcuboot_config/mcuboot_config.h"
49
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010050MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
51
Fabio Utzig10ee6482019-08-01 12:04:52 -030052/* Currently only used by imgmgr */
Christopher Collins92ea77f2016-12-12 15:59:26 -080053int boot_current_slot;
54
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +010055extern const uint32_t boot_img_magic[];
Christopher Collins92ea77f2016-12-12 15:59:26 -080056
Hovland, Sigvart1d96f362018-09-25 13:23:42 +020057#define BOOT_MAGIC_ARR_SZ \
58 (sizeof boot_img_magic / sizeof boot_img_magic[0])
59
Raef Colese8fe6cf2020-05-26 13:07:40 +010060/**
61 * @brief Determine if the data at two memory addresses is equal
62 *
63 * @param s1 The first memory region to compare.
64 * @param s2 The second memory region to compare.
65 * @param n The amount of bytes to compare.
66 *
67 * @note This function does not comply with the specification of memcmp,
68 * so should not be considered a drop-in replacement. It has no
69 * constant time execution. The point is to make sure that all the
70 * bytes are compared and detect if loop was abused and some cycles
71 * was skipped due to fault injection.
72 *
73 * @return FIH_SUCCESS if memory regions are equal, otherwise FIH_FAILURE
74 */
75#ifdef MCUBOOT_FIH_PROFILE_OFF
76inline
77fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n)
78{
79 return memcmp(s1, s2, n);
80}
81#else
82fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n)
83{
84 size_t i;
85 uint8_t *s1_p = (uint8_t*) s1;
86 uint8_t *s2_p = (uint8_t*) s2;
87 fih_int ret = FIH_FAILURE;
88
89 for (i = 0; i < n; i++) {
90 if (s1_p[i] != s2_p[i]) {
91 goto out;
92 }
93 }
94 if (i == n) {
95 ret = FIH_SUCCESS;
96 }
97
98out:
99 FIH_RET(ret);
100}
101#endif
102
Christopher Collins92ea77f2016-12-12 15:59:26 -0800103uint32_t
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300104boot_status_sz(uint32_t min_write_sz)
105{
106 return /* state for all sectors */
107 BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz;
108}
109
110uint32_t
David Brownab449182019-11-15 09:32:52 -0700111boot_trailer_sz(uint32_t min_write_sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800112{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300113 return /* state for all sectors */
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300114 boot_status_sz(min_write_sz) +
Fabio Utzigba829042018-09-18 08:29:34 -0300115#ifdef MCUBOOT_ENC_IMAGES
116 /* encryption keys */
Fabio Utzig4741c452019-12-19 15:32:41 -0300117# if MCUBOOT_SWAP_SAVE_ENCTLV
118 BOOT_ENC_TLV_ALIGN_SIZE * 2 +
119# else
Fabio Utzigba829042018-09-18 08:29:34 -0300120 BOOT_ENC_KEY_SIZE * 2 +
Fabio Utzig4741c452019-12-19 15:32:41 -0300121# endif
Fabio Utzigba829042018-09-18 08:29:34 -0300122#endif
Christopher Collins2adef702019-05-22 14:37:31 -0700123 /* swap_type + copy_done + image_ok + swap_size */
124 BOOT_MAX_ALIGN * 4 +
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300125 BOOT_MAGIC_SZ;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800126}
127
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400128int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300129boot_status_entries(int image_index, const struct flash_area *fap)
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400130{
Fabio Utzig12d59162019-11-28 10:01:59 -0300131#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeb75c12a2019-03-22 14:58:33 +0100132 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400133 return BOOT_STATUS_STATE_COUNT;
Fabio Utzig12d59162019-11-28 10:01:59 -0300134 } else
135#endif
136 if (fap->fa_id == FLASH_AREA_IMAGE_PRIMARY(image_index) ||
Fabio Utzigb0f04732019-07-31 09:49:19 -0300137 fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vinczeb75c12a2019-03-22 14:58:33 +0100138 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400139 }
Fabio Utzig9d160092019-08-09 07:46:34 -0300140 return -1;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400141}
142
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200143#ifndef MCUBOOT_SWAP_USING_STATUS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800144uint32_t
145boot_status_off(const struct flash_area *fap)
146{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300147 uint32_t off_from_end;
148 uint8_t elem_sz;
149
150 elem_sz = flash_area_align(fap);
151
Christopher Collins2adef702019-05-22 14:37:31 -0700152 off_from_end = boot_trailer_sz(elem_sz);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300153
154 assert(off_from_end <= fap->fa_size);
155 return fap->fa_size - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800156}
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200157#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800158
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300159static inline uint32_t
160boot_magic_off(const struct flash_area *fap)
161{
162 return fap->fa_size - BOOT_MAGIC_SZ;
163}
164
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200165#ifndef MCUBOOT_SWAP_USING_STATUS
166
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300167static inline uint32_t
168boot_image_ok_off(const struct flash_area *fap)
169{
170 return boot_magic_off(fap) - BOOT_MAX_ALIGN;
171}
172
173static inline uint32_t
174boot_copy_done_off(const struct flash_area *fap)
175{
176 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
177}
178
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300179static inline uint32_t
Fabio Utzig46490722017-09-04 15:34:32 -0300180boot_swap_size_off(const struct flash_area *fap)
181{
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300182 return boot_swap_info_off(fap) - BOOT_MAX_ALIGN;
Fabio Utzig46490722017-09-04 15:34:32 -0300183}
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200184#endif /* !MCUBOOT_SWAP_USING_STATUS */
Fabio Utzig46490722017-09-04 15:34:32 -0300185
Fabio Utzigba829042018-09-18 08:29:34 -0300186#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300187static inline uint32_t
Fabio Utzigba829042018-09-18 08:29:34 -0300188boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
189{
Fabio Utzig4741c452019-12-19 15:32:41 -0300190#if MCUBOOT_SWAP_SAVE_ENCTLV
191 return boot_swap_size_off(fap) - ((slot + 1) *
192 ((((BOOT_ENC_TLV_SIZE - 1) / BOOT_MAX_ALIGN) + 1) * BOOT_MAX_ALIGN));
193#else
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300194 return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_KEY_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300195#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300196}
197#endif
198
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200199#ifndef MCUBOOT_SWAP_USING_STATUS
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300200/**
201 * This functions tries to locate the status area after an aborted swap,
202 * by looking for the magic in the possible locations.
203 *
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300204 * If the magic is successfully found, a flash_area * is returned and it
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300205 * is the responsibility of the called to close it.
206 *
207 * @returns 0 on success, -1 on errors
208 */
209static int
210boot_find_status(int image_index, const struct flash_area **fap)
211{
212 uint32_t magic[BOOT_MAGIC_ARR_SZ];
213 uint32_t off;
214 uint8_t areas[2] = {
Fabio Utzig12d59162019-11-28 10:01:59 -0300215#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300216 FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig12d59162019-11-28 10:01:59 -0300217#endif
Fabio Utzig3c446072019-11-22 12:48:26 -0300218 FLASH_AREA_IMAGE_PRIMARY(image_index),
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300219 };
220 unsigned int i;
221 int rc;
222
223 /*
224 * In the middle a swap, tries to locate the area that is currently
225 * storing a valid magic, first on the primary slot, then on scratch.
226 * Both "slots" can end up being temporary storage for a swap and it
227 * is assumed that if magic is valid then other metadata is too,
228 * because magic is always written in the last step.
229 */
230
231 for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) {
232 rc = flash_area_open(areas[i], fap);
233 if (rc != 0) {
234 return rc;
235 }
236
237 off = boot_magic_off(*fap);
238 rc = flash_area_read(*fap, off, magic, BOOT_MAGIC_SZ);
239 if (rc != 0) {
240 flash_area_close(*fap);
241 return rc;
242 }
243
244 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
245 return 0;
246 }
247
248 flash_area_close(*fap);
249 }
250
251 /* If we got here, no magic was found */
252 return -1;
253}
254
Christopher Collins92ea77f2016-12-12 15:59:26 -0800255int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300256boot_read_swap_size(int image_index, uint32_t *swap_size)
Fabio Utzig46490722017-09-04 15:34:32 -0300257{
Fabio Utzig46490722017-09-04 15:34:32 -0300258 uint32_t off;
259 const struct flash_area *fap;
260 int rc;
261
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300262 rc = boot_find_status(image_index, &fap);
263 if (rc == 0) {
264 off = boot_swap_size_off(fap);
265 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -0300266 flash_area_close(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300267 }
268
Fabio Utzig46490722017-09-04 15:34:32 -0300269 return rc;
270}
271
Fabio Utzigba829042018-09-18 08:29:34 -0300272#ifdef MCUBOOT_ENC_IMAGES
273int
Fabio Utzig4741c452019-12-19 15:32:41 -0300274boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs)
Fabio Utzigba829042018-09-18 08:29:34 -0300275{
Fabio Utzigba829042018-09-18 08:29:34 -0300276 uint32_t off;
277 const struct flash_area *fap;
Fabio Utzig4741c452019-12-19 15:32:41 -0300278#if MCUBOOT_SWAP_SAVE_ENCTLV
279 int i;
280#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300281 int rc;
282
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300283 rc = boot_find_status(image_index, &fap);
284 if (rc == 0) {
285 off = boot_enc_key_off(fap, slot);
Fabio Utzig4741c452019-12-19 15:32:41 -0300286#if MCUBOOT_SWAP_SAVE_ENCTLV
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200287 uint8_t aes_iv[BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE];
288
Fabio Utzig4741c452019-12-19 15:32:41 -0300289 rc = flash_area_read(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
290 if (rc == 0) {
291 for (i = 0; i < BOOT_ENC_TLV_ALIGN_SIZE; i++) {
292 if (bs->enctlv[slot][i] != 0xff) {
293 break;
294 }
295 }
296 /* Only try to decrypt non-erased TLV metadata */
297 if (i != BOOT_ENC_TLV_ALIGN_SIZE) {
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200298 rc = boot_enc_decrypt(bs->enctlv[slot], bs->enckey[slot], 0, aes_iv);
Fabio Utzig4741c452019-12-19 15:32:41 -0300299 }
300 }
301#else
302 rc = flash_area_read(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE);
303#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300304 flash_area_close(fap);
Fabio Utzigba829042018-09-18 08:29:34 -0300305 }
306
Fabio Utzigba829042018-09-18 08:29:34 -0300307 return rc;
308}
309#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300310
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200311#endif /* !MCUBOOT_SWAP_USING_STATUS */
312
Christopher Collins92ea77f2016-12-12 15:59:26 -0800313int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300314boot_write_copy_done(const struct flash_area *fap)
315{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700316 uint32_t off;
317
318 off = boot_copy_done_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700319 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600320 fap->fa_id, (unsigned long)off,
321 (unsigned long)(fap->fa_off + off));
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300322 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300323}
324
325int
Fabio Utzig46490722017-09-04 15:34:32 -0300326boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
327{
328 uint32_t off;
Fabio Utzig46490722017-09-04 15:34:32 -0300329
330 off = boot_swap_size_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700331 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%lx (0x%lx)",
Marti Bolivar99ec3832019-09-12 21:21:26 -0600332 fap->fa_id, (unsigned long)off,
333 (unsigned long)fap->fa_off + off);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300334 return boot_write_trailer(fap, off, (const uint8_t *) &swap_size, 4);
Fabio Utzig46490722017-09-04 15:34:32 -0300335}
336
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200337#ifndef MCUBOOT_SWAP_USING_STATUS
338
Fabio Utzigba829042018-09-18 08:29:34 -0300339#ifdef MCUBOOT_ENC_IMAGES
340int
Fabio Utzig4741c452019-12-19 15:32:41 -0300341boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
342 const struct boot_status *bs)
Fabio Utzigba829042018-09-18 08:29:34 -0300343{
344 uint32_t off;
345 int rc;
346
347 off = boot_enc_key_off(fap, slot);
Fabio Utzig5e6ea222019-12-10 09:49:59 -0300348 BOOT_LOG_DBG("writing enc_key; fa_id=%d off=0x%lx (0x%lx)",
349 fap->fa_id, (unsigned long)off,
350 (unsigned long)fap->fa_off + off);
Fabio Utzig4741c452019-12-19 15:32:41 -0300351#if MCUBOOT_SWAP_SAVE_ENCTLV
352 rc = flash_area_write(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
353#else
354 rc = flash_area_write(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE);
355#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300356 if (rc != 0) {
357 return BOOT_EFLASH;
358 }
359
360 return 0;
361}
362#endif
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200363
364#endif /* !MCUBOOT_SWAP_USING_STATUS */