blob: 75e53d35600694aeb3166cf0d89e914dbe1b5a6c [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
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300132 if (flash_area_get_id(fap) == 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
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300136 if (flash_area_get_id(fap) == FLASH_AREA_IMAGE_PRIMARY(image_index) ||
137 flash_area_get_id(fap) == 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;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300148 size_t elem_sz;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300149
150 elem_sz = flash_area_align(fap);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300151 assert(elem_sz != 0u);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300152
Christopher Collins2adef702019-05-22 14:37:31 -0700153 off_from_end = boot_trailer_sz(elem_sz);
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300154
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300155 assert(off_from_end <= flash_area_get_size(fap));
156 return flash_area_get_size(fap) - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800157}
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200158#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800159
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300160static inline uint32_t
161boot_magic_off(const struct flash_area *fap)
162{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300163 return flash_area_get_size(fap) - BOOT_MAGIC_SZ;
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300164}
165
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200166#ifndef MCUBOOT_SWAP_USING_STATUS
167
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300168static inline uint32_t
169boot_image_ok_off(const struct flash_area *fap)
170{
171 return boot_magic_off(fap) - BOOT_MAX_ALIGN;
172}
173
174static inline uint32_t
175boot_copy_done_off(const struct flash_area *fap)
176{
177 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
178}
179
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300180static inline uint32_t
Fabio Utzig46490722017-09-04 15:34:32 -0300181boot_swap_size_off(const struct flash_area *fap)
182{
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300183 return boot_swap_info_off(fap) - BOOT_MAX_ALIGN;
Fabio Utzig46490722017-09-04 15:34:32 -0300184}
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200185#endif /* !MCUBOOT_SWAP_USING_STATUS */
Fabio Utzig46490722017-09-04 15:34:32 -0300186
Fabio Utzigba829042018-09-18 08:29:34 -0300187#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300188static inline uint32_t
Fabio Utzigba829042018-09-18 08:29:34 -0300189boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
190{
Fabio Utzig4741c452019-12-19 15:32:41 -0300191#if MCUBOOT_SWAP_SAVE_ENCTLV
192 return boot_swap_size_off(fap) - ((slot + 1) *
193 ((((BOOT_ENC_TLV_SIZE - 1) / BOOT_MAX_ALIGN) + 1) * BOOT_MAX_ALIGN));
194#else
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300195 return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_KEY_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300196#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300197}
198#endif
199
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200200#ifndef MCUBOOT_SWAP_USING_STATUS
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300201/**
202 * This functions tries to locate the status area after an aborted swap,
203 * by looking for the magic in the possible locations.
204 *
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300205 * If the magic is successfully found, a flash_area * is returned and it
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300206 * is the responsibility of the called to close it.
207 *
208 * @returns 0 on success, -1 on errors
209 */
210static int
211boot_find_status(int image_index, const struct flash_area **fap)
212{
213 uint32_t magic[BOOT_MAGIC_ARR_SZ];
214 uint32_t off;
215 uint8_t areas[2] = {
Fabio Utzig12d59162019-11-28 10:01:59 -0300216#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300217 FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig12d59162019-11-28 10:01:59 -0300218#endif
Fabio Utzig3c446072019-11-22 12:48:26 -0300219 FLASH_AREA_IMAGE_PRIMARY(image_index),
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300220 };
221 unsigned int i;
222 int rc;
223
224 /*
225 * In the middle a swap, tries to locate the area that is currently
226 * storing a valid magic, first on the primary slot, then on scratch.
227 * Both "slots" can end up being temporary storage for a swap and it
228 * is assumed that if magic is valid then other metadata is too,
229 * because magic is always written in the last step.
230 */
231
232 for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) {
233 rc = flash_area_open(areas[i], fap);
234 if (rc != 0) {
235 return rc;
236 }
237
238 off = boot_magic_off(*fap);
239 rc = flash_area_read(*fap, off, magic, BOOT_MAGIC_SZ);
240 if (rc != 0) {
241 flash_area_close(*fap);
242 return rc;
243 }
244
245 if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) {
246 return 0;
247 }
248
249 flash_area_close(*fap);
250 }
251
252 /* If we got here, no magic was found */
253 return -1;
254}
255
Christopher Collins92ea77f2016-12-12 15:59:26 -0800256int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300257boot_read_swap_size(int image_index, uint32_t *swap_size)
Fabio Utzig46490722017-09-04 15:34:32 -0300258{
Fabio Utzig46490722017-09-04 15:34:32 -0300259 uint32_t off;
260 const struct flash_area *fap;
261 int rc;
262
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300263 rc = boot_find_status(image_index, &fap);
264 if (rc == 0) {
265 off = boot_swap_size_off(fap);
266 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -0300267 flash_area_close(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300268 }
269
Fabio Utzig46490722017-09-04 15:34:32 -0300270 return rc;
271}
272
Fabio Utzigba829042018-09-18 08:29:34 -0300273#ifdef MCUBOOT_ENC_IMAGES
274int
Fabio Utzig4741c452019-12-19 15:32:41 -0300275boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs)
Fabio Utzigba829042018-09-18 08:29:34 -0300276{
Fabio Utzigba829042018-09-18 08:29:34 -0300277 uint32_t off;
278 const struct flash_area *fap;
279 int rc;
280
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300281 rc = boot_find_status(image_index, &fap);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300282 if (0 == rc) {
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300283 off = boot_enc_key_off(fap, slot);
Fabio Utzig4741c452019-12-19 15:32:41 -0300284#if MCUBOOT_SWAP_SAVE_ENCTLV
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300285 uint8_t aes_iv[BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE];
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200286
Fabio Utzig4741c452019-12-19 15:32:41 -0300287 rc = flash_area_read(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300288 if (0 == rc) {
289 /* Only try to decrypt initialized TLV metadata */
290 if (!bootutil_buffer_is_filled(bs->enctlv[slot],
291 BOOT_UNINITIALIZED_TLV_FILL,
292 BOOT_ENC_TLV_ALIGN_SIZE)) {
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200293 rc = boot_enc_decrypt(bs->enctlv[slot], bs->enckey[slot], 0, aes_iv);
Fabio Utzig4741c452019-12-19 15:32:41 -0300294 }
295 }
296#else
297 rc = flash_area_read(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE);
298#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300299 flash_area_close(fap);
Fabio Utzigba829042018-09-18 08:29:34 -0300300 }
301
Fabio Utzigba829042018-09-18 08:29:34 -0300302 return rc;
303}
304#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300305
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200306#endif /* !MCUBOOT_SWAP_USING_STATUS */
307
Christopher Collins92ea77f2016-12-12 15:59:26 -0800308int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300309boot_write_copy_done(const struct flash_area *fap)
310{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700311 uint32_t off;
312
313 off = boot_copy_done_off(fap);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300314 BOOT_LOG_DBG("writing copy_done; fa_id=%u off=0x%" PRIx32
315 " (0x%" PRIx32 ")", (unsigned)flash_area_get_id(fap),
316 off, flash_area_get_off(fap) + off);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300317 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300318}
319
320int
Fabio Utzig46490722017-09-04 15:34:32 -0300321boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
322{
323 uint32_t off;
Fabio Utzig46490722017-09-04 15:34:32 -0300324
325 off = boot_swap_size_off(fap);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300326 BOOT_LOG_DBG("writing swap_size; fa_id=%u off=0x%" PRIx32
327 " (0x%" PRIx32 ")", (unsigned)flash_area_get_id(fap),
328 off, flash_area_get_off(fap) + off);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300329 return boot_write_trailer(fap, off, (const uint8_t *) &swap_size, 4);
Fabio Utzig46490722017-09-04 15:34:32 -0300330}
331
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200332#ifndef MCUBOOT_SWAP_USING_STATUS
333
Fabio Utzigba829042018-09-18 08:29:34 -0300334#ifdef MCUBOOT_ENC_IMAGES
335int
Fabio Utzig4741c452019-12-19 15:32:41 -0300336boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
337 const struct boot_status *bs)
Fabio Utzigba829042018-09-18 08:29:34 -0300338{
339 uint32_t off;
340 int rc;
341
342 off = boot_enc_key_off(fap, slot);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300343 BOOT_LOG_DBG("writing enc_key; fa_id=%u off=0x%" PRIx32
344 " (0x%" PRIx32 ")", (unsigned)flash_area_get_id(fap),
345 off, flash_area_get_off(fap) + off);
Fabio Utzig4741c452019-12-19 15:32:41 -0300346#if MCUBOOT_SWAP_SAVE_ENCTLV
347 rc = flash_area_write(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
348#else
349 rc = flash_area_write(fap, off, bs->enckey[slot], BOOT_ENC_KEY_SIZE);
350#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300351 if (rc != 0) {
352 return BOOT_EFLASH;
353 }
354
355 return 0;
356}
357#endif
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200358
359#endif /* !MCUBOOT_SWAP_USING_STATUS */