blob: 94521c465283a556bb45e70e15870162e2bc7ab1 [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
Carlos Falgueras GarcĂ­aa4b4b0f2021-06-22 10:00:22 +020044BOOT_LOG_MODULE_DECLARE(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010045
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
Raef Colese8fe6cf2020-05-26 13:07:40 +010049/**
50 * @brief Determine if the data at two memory addresses is equal
51 *
52 * @param s1 The first memory region to compare.
53 * @param s2 The second memory region to compare.
54 * @param n The amount of bytes to compare.
55 *
56 * @note This function does not comply with the specification of memcmp,
57 * so should not be considered a drop-in replacement. It has no
58 * constant time execution. The point is to make sure that all the
59 * bytes are compared and detect if loop was abused and some cycles
60 * was skipped due to fault injection.
61 *
62 * @return FIH_SUCCESS if memory regions are equal, otherwise FIH_FAILURE
63 */
64#ifdef MCUBOOT_FIH_PROFILE_OFF
65inline
66fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n)
67{
68 return memcmp(s1, s2, n);
69}
70#else
71fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n)
72{
73 size_t i;
74 uint8_t *s1_p = (uint8_t*) s1;
75 uint8_t *s2_p = (uint8_t*) s2;
76 fih_int ret = FIH_FAILURE;
77
78 for (i = 0; i < n; i++) {
79 if (s1_p[i] != s2_p[i]) {
80 goto out;
81 }
82 }
83 if (i == n) {
84 ret = FIH_SUCCESS;
85 }
86
87out:
88 FIH_RET(ret);
89}
90#endif
91
Fabio Utzig152cca02021-12-14 22:16:37 -030092/*
93 * Amount of space used to save information required when doing a swap,
94 * or while a swap is under progress, but not the status of sector swap
95 * progress itself.
96 */
97static inline uint32_t
98boot_trailer_info_sz(void)
Fabio Utzig3fbbdac2019-12-19 15:18:23 -030099{
Fabio Utzig152cca02021-12-14 22:16:37 -0300100 return (
Fabio Utzigba829042018-09-18 08:29:34 -0300101#ifdef MCUBOOT_ENC_IMAGES
102 /* encryption keys */
Fabio Utzig4741c452019-12-19 15:32:41 -0300103# if MCUBOOT_SWAP_SAVE_ENCTLV
104 BOOT_ENC_TLV_ALIGN_SIZE * 2 +
105# else
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000106 BOOT_ENC_KEY_ALIGN_SIZE * 2 +
Fabio Utzig4741c452019-12-19 15:32:41 -0300107# endif
Fabio Utzigba829042018-09-18 08:29:34 -0300108#endif
Christopher Collins2adef702019-05-22 14:37:31 -0700109 /* swap_type + copy_done + image_ok + swap_size */
110 BOOT_MAX_ALIGN * 4 +
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000111 BOOT_MAGIC_ALIGN_SIZE
Fabio Utzig152cca02021-12-14 22:16:37 -0300112 );
Christopher Collins92ea77f2016-12-12 15:59:26 -0800113}
114
Fabio Utzig152cca02021-12-14 22:16:37 -0300115/*
116 * Amount of space used to maintain progress information for a single swap
117 * operation.
118 */
119static inline uint32_t
120boot_status_entry_sz(uint32_t min_write_sz)
121{
122 return BOOT_STATUS_STATE_COUNT * min_write_sz;
123}
124
125uint32_t
126boot_status_sz(uint32_t min_write_sz)
127{
128 return BOOT_STATUS_MAX_ENTRIES * boot_status_entry_sz(min_write_sz);
129}
130
131uint32_t
132boot_trailer_sz(uint32_t min_write_sz)
133{
134 return boot_status_sz(min_write_sz) + boot_trailer_info_sz();
135}
136
137#if MCUBOOT_SWAP_USING_SCRATCH
138/*
139 * Similar to `boot_trailer_sz` but this function returns the space used to
140 * store status in the scratch partition. The scratch partition only stores
141 * status during the swap of the last sector from primary/secondary (which
142 * is the first swap operation) and thus only requires space for one swap.
143 */
144static uint32_t
145boot_scratch_trailer_sz(uint32_t min_write_sz)
146{
147 return boot_status_entry_sz(min_write_sz) + boot_trailer_info_sz();
148}
149#endif
150
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400151int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300152boot_status_entries(int image_index, const struct flash_area *fap)
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400153{
Fabio Utzig12d59162019-11-28 10:01:59 -0300154#if MCUBOOT_SWAP_USING_SCRATCH
Dominik Ermel260ae092021-04-23 05:38:45 +0000155 if (flash_area_get_id(fap) == FLASH_AREA_IMAGE_SCRATCH) {
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400156 return BOOT_STATUS_STATE_COUNT;
Fabio Utzig12d59162019-11-28 10:01:59 -0300157 } else
158#endif
Dominik Ermel260ae092021-04-23 05:38:45 +0000159 if (flash_area_get_id(fap) == FLASH_AREA_IMAGE_PRIMARY(image_index) ||
160 flash_area_get_id(fap) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vinczeb75c12a2019-03-22 14:58:33 +0100161 return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400162 }
Fabio Utzig9d160092019-08-09 07:46:34 -0300163 return -1;
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400164}
165
Christopher Collins92ea77f2016-12-12 15:59:26 -0800166uint32_t
167boot_status_off(const struct flash_area *fap)
168{
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300169 uint32_t off_from_end;
Gustavo Henrique Nihei4aa286d2021-11-24 14:54:56 -0300170 uint32_t elem_sz;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300171
172 elem_sz = flash_area_align(fap);
173
Fabio Utzig152cca02021-12-14 22:16:37 -0300174#if MCUBOOT_SWAP_USING_SCRATCH
175 if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) {
176 off_from_end = boot_scratch_trailer_sz(elem_sz);
177 } else {
178#endif
179 off_from_end = boot_trailer_sz(elem_sz);
180#if MCUBOOT_SWAP_USING_SCRATCH
181 }
182#endif
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300183
Dominik Ermel260ae092021-04-23 05:38:45 +0000184 assert(off_from_end <= flash_area_get_size(fap));
185 return flash_area_get_size(fap) - off_from_end;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800186}
187
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000188static int
Gustavo Henrique Niheicf120ba2021-11-22 18:44:11 -0300189boot_magic_decode(const uint8_t *magic)
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000190{
Gustavo Henrique Niheicf120ba2021-11-22 18:44:11 -0300191 if (memcmp(magic, BOOT_IMG_MAGIC, BOOT_MAGIC_SZ) == 0) {
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000192 return BOOT_MAGIC_GOOD;
193 }
194 return BOOT_MAGIC_BAD;
195}
196
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300197static inline uint32_t
198boot_magic_off(const struct flash_area *fap)
199{
Dominik Ermel260ae092021-04-23 05:38:45 +0000200 return flash_area_get_size(fap) - BOOT_MAGIC_SZ;
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300201}
202
203static inline uint32_t
204boot_image_ok_off(const struct flash_area *fap)
205{
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000206 return ALIGN_DOWN(boot_magic_off(fap) - BOOT_MAX_ALIGN, BOOT_MAX_ALIGN);
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300207}
208
209static inline uint32_t
210boot_copy_done_off(const struct flash_area *fap)
211{
212 return boot_image_ok_off(fap) - BOOT_MAX_ALIGN;
213}
214
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300215static inline uint32_t
Fabio Utzig46490722017-09-04 15:34:32 -0300216boot_swap_size_off(const struct flash_area *fap)
217{
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300218 return boot_swap_info_off(fap) - BOOT_MAX_ALIGN;
Fabio Utzig46490722017-09-04 15:34:32 -0300219}
220
Fabio Utzigba829042018-09-18 08:29:34 -0300221#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4e8113b2019-08-09 09:11:18 -0300222static inline uint32_t
Fabio Utzigba829042018-09-18 08:29:34 -0300223boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
224{
Fabio Utzig4741c452019-12-19 15:32:41 -0300225#if MCUBOOT_SWAP_SAVE_ENCTLV
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000226 return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_TLV_ALIGN_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300227#else
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000228 return boot_swap_size_off(fap) - ((slot + 1) * BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300229#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300230}
231#endif
232
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300233/**
234 * This functions tries to locate the status area after an aborted swap,
235 * by looking for the magic in the possible locations.
236 *
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300237 * If the magic is successfully found, a flash_area * is returned and it
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300238 * is the responsibility of the called to close it.
239 *
240 * @returns 0 on success, -1 on errors
241 */
242static int
243boot_find_status(int image_index, const struct flash_area **fap)
244{
Gustavo Henrique Niheicf120ba2021-11-22 18:44:11 -0300245 uint8_t magic[BOOT_MAGIC_SZ];
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300246 uint32_t off;
247 uint8_t areas[2] = {
Fabio Utzig12d59162019-11-28 10:01:59 -0300248#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300249 FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig12d59162019-11-28 10:01:59 -0300250#endif
Fabio Utzig3c446072019-11-22 12:48:26 -0300251 FLASH_AREA_IMAGE_PRIMARY(image_index),
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300252 };
253 unsigned int i;
254 int rc;
255
256 /*
257 * In the middle a swap, tries to locate the area that is currently
258 * storing a valid magic, first on the primary slot, then on scratch.
259 * Both "slots" can end up being temporary storage for a swap and it
260 * is assumed that if magic is valid then other metadata is too,
261 * because magic is always written in the last step.
262 */
263
264 for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) {
265 rc = flash_area_open(areas[i], fap);
266 if (rc != 0) {
267 return rc;
268 }
269
270 off = boot_magic_off(*fap);
271 rc = flash_area_read(*fap, off, magic, BOOT_MAGIC_SZ);
Dominik Ermelec6dac52021-10-22 14:55:37 +0000272 flash_area_close(*fap);
273
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300274 if (rc != 0) {
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300275 return rc;
276 }
277
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000278 if (BOOT_MAGIC_GOOD == boot_magic_decode(magic)) {
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300279 return 0;
280 }
281
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300282 }
283
284 /* If we got here, no magic was found */
285 return -1;
286}
287
Christopher Collins92ea77f2016-12-12 15:59:26 -0800288int
Fabio Utzigb0f04732019-07-31 09:49:19 -0300289boot_read_swap_size(int image_index, uint32_t *swap_size)
Fabio Utzig46490722017-09-04 15:34:32 -0300290{
Fabio Utzig46490722017-09-04 15:34:32 -0300291 uint32_t off;
292 const struct flash_area *fap;
293 int rc;
294
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300295 rc = boot_find_status(image_index, &fap);
296 if (rc == 0) {
297 off = boot_swap_size_off(fap);
298 rc = flash_area_read(fap, off, swap_size, sizeof *swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -0300299 flash_area_close(fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300300 }
301
Fabio Utzig46490722017-09-04 15:34:32 -0300302 return rc;
303}
304
Fabio Utzigba829042018-09-18 08:29:34 -0300305#ifdef MCUBOOT_ENC_IMAGES
306int
Fabio Utzig4741c452019-12-19 15:32:41 -0300307boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs)
Fabio Utzigba829042018-09-18 08:29:34 -0300308{
Fabio Utzigba829042018-09-18 08:29:34 -0300309 uint32_t off;
310 const struct flash_area *fap;
Fabio Utzig4741c452019-12-19 15:32:41 -0300311#if MCUBOOT_SWAP_SAVE_ENCTLV
312 int i;
313#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300314 int rc;
315
Fabio Utzigdf0cc502019-08-09 09:10:41 -0300316 rc = boot_find_status(image_index, &fap);
317 if (rc == 0) {
318 off = boot_enc_key_off(fap, slot);
Fabio Utzig4741c452019-12-19 15:32:41 -0300319#if MCUBOOT_SWAP_SAVE_ENCTLV
320 rc = flash_area_read(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
321 if (rc == 0) {
322 for (i = 0; i < BOOT_ENC_TLV_ALIGN_SIZE; i++) {
323 if (bs->enctlv[slot][i] != 0xff) {
324 break;
325 }
326 }
327 /* Only try to decrypt non-erased TLV metadata */
328 if (i != BOOT_ENC_TLV_ALIGN_SIZE) {
329 rc = boot_enc_decrypt(bs->enctlv[slot], bs->enckey[slot]);
330 }
331 }
332#else
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000333 rc = flash_area_read(fap, off, bs->enckey[slot], BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300334#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300335 flash_area_close(fap);
Fabio Utzigba829042018-09-18 08:29:34 -0300336 }
337
Fabio Utzigba829042018-09-18 08:29:34 -0300338 return rc;
339}
340#endif
Fabio Utzig46490722017-09-04 15:34:32 -0300341
Christopher Collins92ea77f2016-12-12 15:59:26 -0800342int
Fabio Utzig2473ac02017-05-02 12:45:02 -0300343boot_write_copy_done(const struct flash_area *fap)
344{
Christopher Collinsa1c12042019-05-23 14:00:28 -0700345 uint32_t off;
346
347 off = boot_copy_done_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700348 BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%lx (0x%lx)",
Dominik Ermel260ae092021-04-23 05:38:45 +0000349 flash_area_get_id(fap), (unsigned long)off,
350 (unsigned long)(flash_area_get_off(fap) + off));
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300351 return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300352}
353
354int
Fabio Utzig46490722017-09-04 15:34:32 -0300355boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
356{
357 uint32_t off;
Fabio Utzig46490722017-09-04 15:34:32 -0300358
359 off = boot_swap_size_off(fap);
Ben McCrea4c0ee952019-08-28 09:13:24 -0700360 BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%lx (0x%lx)",
Dominik Ermel260ae092021-04-23 05:38:45 +0000361 flash_area_get_id(fap), (unsigned long)off,
362 (unsigned long)flash_area_get_off(fap) + off);
Fabio Utzig1a1ec172019-08-09 10:21:43 -0300363 return boot_write_trailer(fap, off, (const uint8_t *) &swap_size, 4);
Fabio Utzig46490722017-09-04 15:34:32 -0300364}
365
Fabio Utzigba829042018-09-18 08:29:34 -0300366#ifdef MCUBOOT_ENC_IMAGES
367int
Fabio Utzig4741c452019-12-19 15:32:41 -0300368boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
369 const struct boot_status *bs)
Fabio Utzigba829042018-09-18 08:29:34 -0300370{
371 uint32_t off;
372 int rc;
373
374 off = boot_enc_key_off(fap, slot);
Fabio Utzig5e6ea222019-12-10 09:49:59 -0300375 BOOT_LOG_DBG("writing enc_key; fa_id=%d off=0x%lx (0x%lx)",
Dominik Ermel260ae092021-04-23 05:38:45 +0000376 flash_area_get_id(fap), (unsigned long)off,
377 (unsigned long)flash_area_get_off(fap) + off);
Fabio Utzig4741c452019-12-19 15:32:41 -0300378#if MCUBOOT_SWAP_SAVE_ENCTLV
379 rc = flash_area_write(fap, off, bs->enctlv[slot], BOOT_ENC_TLV_ALIGN_SIZE);
380#else
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000381 rc = flash_area_write(fap, off, bs->enckey[slot], BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300382#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300383 if (rc != 0) {
384 return BOOT_EFLASH;
385 }
386
387 return 0;
388}
389#endif
Andrzej Puzdrowski334b6a62022-01-21 16:59:18 +0100390
391uint32_t bootutil_max_image_size(const struct flash_area *fap)
392{
393#if defined(MCUBOOT_SWAP_USING_SCRATCH)
394 return boot_status_off(fap);
395#elif defined(MCUBOOT_SWAP_USING_MOVE)
396 struct flash_sector sector;
397 /* get the last sector offset */
398 int rc = flash_area_sector_from_off(boot_status_off(fap), &sector);
399 if (rc) {
400 BOOT_LOG_ERR("Unable to determine flash sector of the image trailer");
401 return 0; /* Returning of zero here should cause any check which uses
402 * this value to fail.
403 */
404 }
405 return flash_sector_get_off(&sector);
406#elif defined(MCUBOOT_OVERWRITE_ONLY)
407 return boot_swap_info_off(fap);
408#elif defined(MCUBOOT_DIRECT_XIP)
409 return boot_swap_info_off(fap);
410#elif defined(MCUBOOT_RAM_LOAD)
411 return boot_swap_info_off(fap);
412#endif
413}