blob: 75367d6cbb16a63935d26e13d83514e352104fa0 [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-2020 Linaro LTD
5 * Copyright (c) 2017-2019 JUUL Labs
Roman Okhrimenko977b3752022-03-31 14:40:48 +03006 * Copyright (c) 2019-2021 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
28#ifndef H_BOOTUTIL_PRIV_
29#define H_BOOTUTIL_PRIV_
30
Roman Okhrimenko977b3752022-03-31 14:40:48 +030031#include <inttypes.h>
Tamas Banfe031092020-09-10 17:32:39 +020032#include <string.h>
33
Marti Bolivarcca28a92017-06-12 16:52:22 -040034#include "sysflash/sysflash.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020035
36#include <flash_map_backend/flash_map_backend.h>
37
Christopher Collins01dfbb62019-03-21 07:28:37 -070038#include "bootutil/bootutil.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080039#include "bootutil/image.h"
Raef Colese8fe6cf2020-05-26 13:07:40 +010040#include "bootutil/fault_injection_hardening.h"
Marti Bolivarf91bca52018-04-12 12:40:46 -040041#include "mcuboot_config/mcuboot_config.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080042
Fabio Utzigba829042018-09-18 08:29:34 -030043#ifdef MCUBOOT_ENC_IMAGES
44#include "bootutil/enc_key.h"
45#endif
46
Christopher Collins92ea77f2016-12-12 15:59:26 -080047#ifdef __cplusplus
48extern "C" {
49#endif
50
51struct flash_area;
52
Christopher Collins92ea77f2016-12-12 15:59:26 -080053#define BOOT_TMPBUF_SZ 256
54
Fabio Utzig1e4284b2019-08-23 11:55:27 -030055/** Number of image slots in flash; currently limited to two. */
56#define BOOT_NUM_SLOTS 2
57
David Vinczee574f2d2020-07-10 11:42:03 +020058#if !defined(MCUBOOT_OVERWRITE_ONLY) && \
59 !defined(MCUBOOT_SWAP_USING_MOVE) && \
Tamas Banfe031092020-09-10 17:32:39 +020060 !defined(MCUBOOT_DIRECT_XIP) && \
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030061 !(defined(MCUBOOT_RAM_LOAD) && !defined(MCUBOOT_MULTI_MEMORY_LOAD))
62#define MCUBOOT_SWAP_USING_SCRATCH 1
63#endif
64
65#if !defined(MCUBOOT_OVERWRITE_ONLY) && \
66 !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzig12d59162019-11-28 10:01:59 -030067#define MCUBOOT_SWAP_USING_SCRATCH 1
68#endif
69
Fabio Utzig74aef312019-11-28 11:05:34 -030070#define BOOT_STATUS_OP_MOVE 1
71#define BOOT_STATUS_OP_SWAP 2
72
Christopher Collins92ea77f2016-12-12 15:59:26 -080073/*
74 * Maintain state of copy progress.
75 */
76struct boot_status {
Fabio Utzig2473ac02017-05-02 12:45:02 -030077 uint32_t idx; /* Which area we're operating on */
78 uint8_t state; /* Which part of the swapping process are we at */
Fabio Utzig74aef312019-11-28 11:05:34 -030079 uint8_t op; /* What operation are we performing? */
Fabio Utzig2473ac02017-05-02 12:45:02 -030080 uint8_t use_scratch; /* Are status bytes ever written to scratch? */
Christopher Collinsa1c12042019-05-23 14:00:28 -070081 uint8_t swap_type; /* The type of swap in effect */
Fabio Utzig46490722017-09-04 15:34:32 -030082 uint32_t swap_size; /* Total size of swapped image */
Fabio Utzigba829042018-09-18 08:29:34 -030083#ifdef MCUBOOT_ENC_IMAGES
Roman Okhrimenko977b3752022-03-31 14:40:48 +030084#define BOOT_UNINITIALIZED_KEY_FILL 0xFF
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +030085 uint8_t enckey[BOOT_NUM_SLOTS][BOOT_ENC_KEY_ALIGN_SIZE];
86#if MCUBOOT_SWAP_SAVE_ENCTLV
Roman Okhrimenko977b3752022-03-31 14:40:48 +030087#define BOOT_UNINITIALIZED_TLV_FILL 0xFF
Fabio Utzig4741c452019-12-19 15:32:41 -030088 uint8_t enctlv[BOOT_NUM_SLOTS][BOOT_ENC_TLV_ALIGN_SIZE];
89#endif
Fabio Utzigba829042018-09-18 08:29:34 -030090#endif
Fabio Utzigb86e6882019-12-10 09:51:17 -030091 int source; /* Which slot contains swap status metadata */
Christopher Collins92ea77f2016-12-12 15:59:26 -080092};
93
Roman Okhrimenko977b3752022-03-31 14:40:48 +030094#define BOOT_STATUS_IDX_0 1U
Fabio Utzig39000012018-07-30 12:40:20 -030095
96#define BOOT_STATUS_STATE_0 1
97#define BOOT_STATUS_STATE_1 2
98#define BOOT_STATUS_STATE_2 3
Christopher Collins92ea77f2016-12-12 15:59:26 -080099
100/**
101 * End-of-image slot structure.
102 *
Christopher Collinsa1c12042019-05-23 14:00:28 -0700103 * 0 1 2 3
104 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
105 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106 * ~ ~
107 * ~ Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3) ~
108 * ~ ~
109 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110 * | Encryption key 0 (16 octets) [*] |
111 * | |
112 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300113 * | 0xff padding as needed |
114 * | (BOOT_MAX_ALIGN minus 16 octets from Encryption key 0) [*] |
115 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collinsa1c12042019-05-23 14:00:28 -0700116 * | Encryption key 1 (16 octets) [*] |
117 * | |
118 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300119 * | 0xff padding as needed |
120 * | (BOOT_MAX_ALIGN minus 16 octets from Encryption key 1) [*] |
121 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collinsa1c12042019-05-23 14:00:28 -0700122 * | Swap size (4 octets) |
123 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300124 * | 0xff padding as needed |
125 * | (BOOT_MAX_ALIGN minus 4 octets from Swap size) |
Christopher Collinsa1c12042019-05-23 14:00:28 -0700126 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300127 * | Swap info | 0xff padding (BOOT_MAX_ALIGN minus 1 octet) |
Christopher Collinsa1c12042019-05-23 14:00:28 -0700128 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300129 * | Copy done | 0xff padding (BOOT_MAX_ALIGN minus 1 octet) |
130 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
131 * | Image OK | 0xff padding (BOOT_MAX_ALIGN minus 1 octet) |
132 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
133 * | 0xff padding as needed |
134 * | (BOOT_MAX_ALIGN minus 16 octets from MAGIC) |
Christopher Collinsa1c12042019-05-23 14:00:28 -0700135 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
136 * | MAGIC (16 octets) |
137 * | |
138 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139 *
140 * [*]: Only present if the encryption option is enabled
141 * (`MCUBOOT_ENC_IMAGES`).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800142 */
143
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300144union boot_img_magic_t
145{
146 struct {
147 uint16_t align;
148 uint8_t magic[14];
149 };
150 uint8_t val[16];
151};
Christopher Collins92ea77f2016-12-12 15:59:26 -0800152
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300153extern const union boot_img_magic_t boot_img_magic;
154
155#define BOOT_IMG_MAGIC (boot_img_magic.val)
156
157#if BOOT_MAX_ALIGN == 8
158#define BOOT_IMG_ALIGN (BOOT_MAX_ALIGN)
David Vinczeb75c12a2019-03-22 14:58:33 +0100159#else
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300160#define BOOT_IMG_ALIGN (boot_img_magic.align)
David Vinczeb75c12a2019-03-22 14:58:33 +0100161#endif
162
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300163_Static_assert(sizeof(boot_img_magic) == BOOT_MAGIC_SZ, "Invalid size for image magic");
Fabio Utzigabec0732019-07-31 08:40:22 -0300164
Tamas Banfe031092020-09-10 17:32:39 +0200165#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
166#define ARE_SLOTS_EQUIVALENT() 0
David Vinczee574f2d2020-07-10 11:42:03 +0200167#else
Tamas Banfe031092020-09-10 17:32:39 +0200168#define ARE_SLOTS_EQUIVALENT() 1
David Vinczee574f2d2020-07-10 11:42:03 +0200169
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300170#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_ENC_IMAGES)
171#error "Image encryption (MCUBOOT_ENC_IMAGES) is not supported when MCUBOOT_DIRECT_XIP is selected."
172#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_ENC_IMAGES */
Tamas Banfe031092020-09-10 17:32:39 +0200173#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200174
Marti Bolivarf9bfddd2018-04-24 14:28:33 -0400175#define BOOT_MAX_IMG_SECTORS MCUBOOT_MAX_IMG_SECTORS
Fabio Utziga1fae672018-03-30 10:52:38 -0300176
David Vinczee574f2d2020-07-10 11:42:03 +0200177#define BOOT_LOG_IMAGE_INFO(slot, hdr) \
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300178 BOOT_LOG_INF("%-9s slot: " \
179 "version=%u.%u.%" PRIu16 "+%" PRIu32, \
David Vinczee574f2d2020-07-10 11:42:03 +0200180 ((slot) == BOOT_PRIMARY_SLOT) ? "Primary" : "Secondary", \
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300181 (unsigned)(hdr)->ih_ver.iv_major, \
182 (unsigned)(hdr)->ih_ver.iv_minor, \
David Vinczee574f2d2020-07-10 11:42:03 +0200183 (hdr)->ih_ver.iv_revision, \
184 (hdr)->ih_ver.iv_build_num)
185
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300186#if MCUBOOT_SWAP_USING_MOVE
Fabio Utzig74aef312019-11-28 11:05:34 -0300187#define BOOT_STATUS_MOVE_STATE_COUNT 1
188#define BOOT_STATUS_SWAP_STATE_COUNT 2
189#define BOOT_STATUS_STATE_COUNT (BOOT_STATUS_MOVE_STATE_COUNT + BOOT_STATUS_SWAP_STATE_COUNT)
190#else
David Vincze2d736ad2019-02-18 11:50:22 +0100191#define BOOT_STATUS_STATE_COUNT 3
Fabio Utzig74aef312019-11-28 11:05:34 -0300192#endif
193
194/** Maximum number of image sectors supported by the bootloader. */
David Vincze2d736ad2019-02-18 11:50:22 +0100195#define BOOT_STATUS_MAX_ENTRIES BOOT_MAX_IMG_SECTORS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800196
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300197#define NO_ACTIVE_SLOT UINT32_MAX
David Vincze2d736ad2019-02-18 11:50:22 +0100198#define BOOT_PRIMARY_SLOT 0
199#define BOOT_SECONDARY_SLOT 1
Christopher Collins92ea77f2016-12-12 15:59:26 -0800200
David Vincze2d736ad2019-02-18 11:50:22 +0100201#define BOOT_STATUS_SOURCE_NONE 0
202#define BOOT_STATUS_SOURCE_SCRATCH 1
203#define BOOT_STATUS_SOURCE_PRIMARY_SLOT 2
204
Marti Bolivarc50926f2017-06-14 09:35:40 -0400205/**
206 * Compatibility shim for flash sector type.
207 *
208 * This can be deleted when flash_area_to_sectors() is removed.
209 */
210#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
211typedef struct flash_sector boot_sector_t;
212#else
213typedef struct flash_area boot_sector_t;
214#endif
215
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400216/** Private state maintained during boot. */
217struct boot_loader_state {
218 struct {
219 struct image_header hdr;
Marti Bolivarc0b47912017-06-13 17:18:09 -0400220 const struct flash_area *area;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400221 boot_sector_t *sectors;
Marti Bolivar84898652017-06-13 17:20:22 -0400222 size_t num_sectors;
David Vinczeba3bd602019-06-17 16:01:43 +0200223 } imgs[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS];
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400224
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300225#if defined(MCUBOOT_SWAP_USING_SCRATCH)
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200226 struct {
227 const struct flash_area *area;
228 boot_sector_t *sectors;
229 size_t num_sectors;
230 } scratch;
Fabio Utzig12d59162019-11-28 10:01:59 -0300231#endif
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300232#if defined(MCUBOOT_SWAP_USING_STATUS)
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200233 struct {
234 const struct flash_area *area;
235 boot_sector_t *sectors;
236 size_t num_sectors;
237 } status;
238#endif
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400239
David Vinczeba3bd602019-06-17 16:01:43 +0200240 uint8_t swap_type[BOOT_IMAGE_NUMBER];
David Brownab449182019-11-15 09:32:52 -0700241 uint32_t write_sz;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300242
243#if defined(MCUBOOT_ENC_IMAGES)
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300244 struct enc_key_data enc[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS];
Fabio Utzig10ee6482019-08-01 12:04:52 -0300245#endif
246
Fabio Utzigb0f04732019-07-31 09:49:19 -0300247#if (BOOT_IMAGE_NUMBER > 1)
248 uint8_t curr_img_idx;
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300249 bool img_mask[BOOT_IMAGE_NUMBER];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300250#endif
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300251
252#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD) || defined(MCUBOOT_MULTI_MEMORY_BOOT)
253 struct slot_usage_t {
254 /* Index of the slot chosen to be loaded */
255 uint32_t active_slot;
256 bool slot_available[BOOT_NUM_SLOTS];
257#if defined(MCUBOOT_RAM_LOAD)
258 /* Image destination and size for the active slot */
259 uint32_t img_dst;
260 uint32_t img_sz;
261#elif defined(MCUBOOT_DIRECT_XIP_REVERT)
262 /* Swap status for the active slot */
263 struct boot_swap_state swap_state;
264#endif
265 } slot_usage[BOOT_IMAGE_NUMBER];
266#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400267};
268
Raef Colese8fe6cf2020-05-26 13:07:40 +0100269fih_int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
270 size_t slen, uint8_t key_id);
271
272fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800273
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300274bool boot_magic_compatible_check(uint8_t tbl_val, uint8_t val);
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300275uint32_t boot_status_sz(uint32_t min_write_sz);
David Brownab449182019-11-15 09:32:52 -0700276uint32_t boot_trailer_sz(uint32_t min_write_sz);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300277int boot_status_entries(int image_index, const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800278uint32_t boot_status_off(const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800279int boot_write_magic(const struct flash_area *fap);
Fabio Utzig12d59162019-11-28 10:01:59 -0300280int boot_write_status(const struct boot_loader_state *state, struct boot_status *bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800281int boot_write_copy_done(const struct flash_area *fap);
282int boot_write_image_ok(const struct flash_area *fap);
David Vinczee2453472019-06-17 12:31:59 +0200283int boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
284 uint8_t image_num);
Fabio Utzig46490722017-09-04 15:34:32 -0300285int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000286int boot_write_trailer(const struct flash_area *fap, uint32_t off,
287 const uint8_t *inbuf, uint8_t inlen);
288int boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
289 uint8_t flag_val);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300290int boot_read_swap_size(int image_index, uint32_t *swap_size);
Fabio Utzig12d59162019-11-28 10:01:59 -0300291int boot_slots_compatible(struct boot_loader_state *state);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300292uint32_t boot_status_internal_off(const struct boot_status *bs, uint32_t elem_sz);
Fabio Utzig12d59162019-11-28 10:01:59 -0300293int boot_read_image_header(struct boot_loader_state *state, int slot,
294 struct image_header *out_hdr, struct boot_status *bs);
295int boot_copy_region(struct boot_loader_state *state,
296 const struct flash_area *fap_src,
297 const struct flash_area *fap_dst,
298 uint32_t off_src, uint32_t off_dst, uint32_t sz);
299int boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz);
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300300bool boot_status_is_reset(const struct boot_status *bs);
Fabio Utzig12d59162019-11-28 10:01:59 -0300301
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200302#ifdef MCUBOOT_SWAP_USING_STATUS
303uint32_t boot_copy_done_off(const struct flash_area *fap);
304uint32_t boot_image_ok_off(const struct flash_area *fap);
305uint32_t boot_swap_size_off(const struct flash_area *fap);
306#endif
307
308
Fabio Utzigba829042018-09-18 08:29:34 -0300309#ifdef MCUBOOT_ENC_IMAGES
310int boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
Fabio Utzig4741c452019-12-19 15:32:41 -0300311 const struct boot_status *bs);
312int boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300313#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800314
David Browneffb06e2019-10-10 14:45:32 -0600315/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300316 * Checks that a buffer is filled by the specified value.
317 *
318 * @returns true if all bytes in the buffer are equal to `fill`; false if any
319 * of the bytes does not match, or when buffer is NULL, or when len == 0.
320 */
321bool bootutil_buffer_is_filled(const void *buffer, uint8_t fill, size_t len);
322
323/**
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300324 * Checks that a buffer is erased according to what the erase value for the
325 * flash device provided in `flash_area` is.
326 *
327 * @returns true if the buffer is erased; false if any of the bytes is not
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300328 * erased, or when area is NULL, or when buffer is NULL, or when len == 0.
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300329 */
330bool bootutil_buffer_is_erased(const struct flash_area *area,
331 const void *buffer, size_t len);
332
333/**
David Browneffb06e2019-10-10 14:45:32 -0600334 * Safe (non-overflowing) uint32_t addition. Returns true, and stores
335 * the result in *dest if it can be done without overflow. Otherwise,
336 * returns false.
337 */
338static inline bool boot_u32_safe_add(uint32_t *dest, uint32_t a, uint32_t b)
339{
340 /*
341 * "a + b <= UINT32_MAX", subtract 'b' from both sides to avoid
342 * the overflow.
343 */
344 if (a > UINT32_MAX - b) {
345 return false;
346 } else {
347 *dest = a + b;
348 return true;
349 }
350}
351
352/**
353 * Safe (non-overflowing) uint16_t addition. Returns true, and stores
354 * the result in *dest if it can be done without overflow. Otherwise,
355 * returns false.
356 */
357static inline bool boot_u16_safe_add(uint16_t *dest, uint16_t a, uint16_t b)
358{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300359 uint32_t tmp = (uint32_t)a + b;
David Browneffb06e2019-10-10 14:45:32 -0600360 if (tmp > UINT16_MAX) {
361 return false;
362 } else {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300363 *dest = (uint16_t) tmp;
David Browneffb06e2019-10-10 14:45:32 -0600364 return true;
365 }
366}
367
Marti Bolivarf804f622017-06-12 15:41:48 -0400368/*
369 * Accessors for the contents of struct boot_loader_state.
370 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300371#if (BOOT_IMAGE_NUMBER > 1)
372#define BOOT_CURR_IMG(state) ((state)->curr_img_idx)
373#else
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300374#define BOOT_CURR_IMG(state) 0u
Fabio Utzigbc077932019-08-26 11:16:34 -0300375#endif
376#ifdef MCUBOOT_ENC_IMAGES
377#define BOOT_CURR_ENC(state) ((state)->enc[BOOT_CURR_IMG(state)])
378#else
379#define BOOT_CURR_ENC(state) NULL
Fabio Utzigb0f04732019-07-31 09:49:19 -0300380#endif
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300381/* These are macros so they can be used as lvalues. */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300382#define BOOT_IMG(state, slot) ((state)->imgs[BOOT_CURR_IMG(state)][(slot)])
David Vinczeba3bd602019-06-17 16:01:43 +0200383#define BOOT_IMG_AREA(state, slot) (BOOT_IMG(state, slot).area)
Marti Bolivare10a7392017-06-14 16:20:07 -0400384#define BOOT_WRITE_SZ(state) ((state)->write_sz)
Fabio Utzigb0f04732019-07-31 09:49:19 -0300385#define BOOT_SWAP_TYPE(state) ((state)->swap_type[BOOT_CURR_IMG(state)])
Fabio Utzigc9621352019-08-08 12:15:51 -0300386#define BOOT_TLV_OFF(hdr) ((hdr)->ih_hdr_size + (hdr)->ih_img_size)
Marti Bolivarc0b47912017-06-13 17:18:09 -0400387
Fabio Utzige575b0b2019-09-11 12:34:23 -0300388#define BOOT_IS_UPGRADE(swap_type) \
389 (((swap_type) == BOOT_SWAP_TYPE_TEST) || \
390 ((swap_type) == BOOT_SWAP_TYPE_REVERT) || \
391 ((swap_type) == BOOT_SWAP_TYPE_PERM))
392
Marti Bolivarf804f622017-06-12 15:41:48 -0400393static inline struct image_header*
394boot_img_hdr(struct boot_loader_state *state, size_t slot)
395{
David Vinczeba3bd602019-06-17 16:01:43 +0200396 return &BOOT_IMG(state, slot).hdr;
Marti Bolivarf804f622017-06-12 15:41:48 -0400397}
398
Marti Bolivard3269fd2017-06-12 16:31:12 -0400399static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300400boot_img_num_sectors(const struct boot_loader_state *state, size_t slot)
Marti Bolivard3269fd2017-06-12 16:31:12 -0400401{
David Vinczeba3bd602019-06-17 16:01:43 +0200402 return BOOT_IMG(state, slot).num_sectors;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400403}
404
Marti Bolivar135b8f62017-06-13 17:22:13 -0400405/*
406 * Offset of the slot from the beginning of the flash device.
407 */
408static inline uint32_t
409boot_img_slot_off(struct boot_loader_state *state, size_t slot)
410{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300411 return flash_area_get_off(BOOT_IMG(state, slot).area);
Marti Bolivar135b8f62017-06-13 17:22:13 -0400412}
413
Marti Bolivarc50926f2017-06-14 09:35:40 -0400414#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
415
Marti Bolivard3269fd2017-06-12 16:31:12 -0400416static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300417boot_img_sector_size(const struct boot_loader_state *state,
Marti Bolivard3269fd2017-06-12 16:31:12 -0400418 size_t slot, size_t sector)
419{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300420 return flash_area_get_size(&BOOT_IMG(state, slot).sectors[sector]);
Marti Bolivard3269fd2017-06-12 16:31:12 -0400421}
422
Marti Bolivarea088872017-06-12 17:10:49 -0400423/*
424 * Offset of the sector from the beginning of the image, NOT the flash
425 * device.
426 */
427static inline uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300428boot_img_sector_off(const struct boot_loader_state *state, size_t slot,
Marti Bolivarea088872017-06-12 17:10:49 -0400429 size_t sector)
430{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300431 return flash_area_get_off(&BOOT_IMG(state, slot).sectors[sector]) -
432 flash_area_get_off(&BOOT_IMG(state, slot).sectors[0]);
Marti Bolivarea088872017-06-12 17:10:49 -0400433}
434
Marti Bolivarc50926f2017-06-14 09:35:40 -0400435#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
436
437static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300438boot_img_sector_size(const struct boot_loader_state *state,
Marti Bolivarc50926f2017-06-14 09:35:40 -0400439 size_t slot, size_t sector)
440{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300441 return flash_sector_get_size(&BOOT_IMG(state, slot).sectors[sector]);
Marti Bolivarc50926f2017-06-14 09:35:40 -0400442}
443
444static inline uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300445boot_img_sector_off(const struct boot_loader_state *state, size_t slot,
Marti Bolivarc50926f2017-06-14 09:35:40 -0400446 size_t sector)
447{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300448 return flash_sector_get_off(&BOOT_IMG(state, slot).sectors[sector]) -
449 flash_sector_get_off(&BOOT_IMG(state, slot).sectors[0]);
Marti Bolivarc50926f2017-06-14 09:35:40 -0400450}
451
Marti Bolivarc50926f2017-06-14 09:35:40 -0400452#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
453
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300454#if defined(MCUBOOT_RAM_LOAD)
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300455# ifdef __BOOTSIM__
456
457/* Query for the layout of a RAM buffer appropriate for holding the
458 * image. This will be per-test-thread, and therefore must be queried
459 * through this call. */
460struct bootsim_ram_info {
461 uint32_t start;
462 uint32_t size;
463 uintptr_t base;
464};
465struct bootsim_ram_info *bootsim_get_ram_info(void);
466
467#define IMAGE_GET_FIELD(field) (bootsim_get_ram_info()->field)
468#define IMAGE_RAM_BASE IMAGE_GET_FIELD(base)
469#define IMAGE_EXECUTABLE_RAM_START IMAGE_GET_FIELD(start)
470#define IMAGE_EXECUTABLE_RAM_SIZE IMAGE_GET_FIELD(size)
471
472# else
473# define IMAGE_RAM_BASE ((uintptr_t)0)
474# endif
475
Tamas Banfe031092020-09-10 17:32:39 +0200476#else
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300477#define IMAGE_RAM_BASE ((uintptr_t)0)
Tamas Banfe031092020-09-10 17:32:39 +0200478#endif /* MCUBOOT_RAM_LOAD */
479
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300480#define LOAD_IMAGE_DATA_RAM(hdr, fap, start, output, size) \
481 (memcpy((output),(void*)(IMAGE_RAM_BASE + (hdr)->ih_load_addr + (start)), \
482 (size)), 0)
483
484#define LOAD_IMAGE_DATA_FLASH(hdr, fap, start, output, size) \
485 (flash_area_read((fap), (start), (output), (size)))
486
487#if defined(MCUBOOT_MULTI_MEMORY_LOAD) && defined(MCUBOOT_RAM_LOAD)
488#define LOAD_IMAGE_DATA(hdr, fap, start, output, size) \
489 ({ \
490 int rc; \
491 if (IS_RAM_BOOTABLE(hdr)) { \
492 rc = LOAD_IMAGE_DATA_RAM((hdr), (fap), (start), (output), (size)); \
493 } else { \
494 rc = LOAD_IMAGE_DATA_FLASH((hdr), (fap), (start), (output), \
495 (size)); \
496 } \
497 rc; \
498 })
499#elif defined(MCUBOOT_RAM_LOAD)
500#define LOAD_IMAGE_DATA(hdr, fap, start, output, size) \
501 LOAD_IMAGE_DATA_RAM((hdr), (fap), (start), (output), (size))
502#else /* !defined(MCUBOOT_RAM_LOAD)*/
503#define LOAD_IMAGE_DATA(hdr, fap, start, output, size) \
504 LOAD_IMAGE_DATA_FLASH((hdr), (fap), (start), (output), (size))
505#endif /* MCUBOOT_MULTI_MEMORY_LOAD */
506
Christopher Collins92ea77f2016-12-12 15:59:26 -0800507#ifdef __cplusplus
508}
509#endif
510
511#endif