blob: ac40a6ed91d9f5f0d85e6e04f1dc46babff1a48a [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) + \
61 defined(MCUBOOT_RAM_LOAD)) > 1
62#error "Please enable only one of MCUBOOT_OVERWRITE_ONLY, MCUBOOT_SWAP_USING_MOVE, MCUBOOT_DIRECT_XIP or MCUBOOT_RAM_LOAD"
Fabio Utzig74aef312019-11-28 11:05:34 -030063#endif
64
David Vinczee574f2d2020-07-10 11:42:03 +020065#if !defined(MCUBOOT_OVERWRITE_ONLY) && \
66 !defined(MCUBOOT_SWAP_USING_MOVE) && \
Tamas Banfe031092020-09-10 17:32:39 +020067 !defined(MCUBOOT_DIRECT_XIP) && \
68 !defined(MCUBOOT_RAM_LOAD)
Fabio Utzig12d59162019-11-28 10:01:59 -030069#define MCUBOOT_SWAP_USING_SCRATCH 1
70#endif
71
Fabio Utzig74aef312019-11-28 11:05:34 -030072#define BOOT_STATUS_OP_MOVE 1
73#define BOOT_STATUS_OP_SWAP 2
74
Christopher Collins92ea77f2016-12-12 15:59:26 -080075/*
76 * Maintain state of copy progress.
77 */
78struct boot_status {
Fabio Utzig2473ac02017-05-02 12:45:02 -030079 uint32_t idx; /* Which area we're operating on */
80 uint8_t state; /* Which part of the swapping process are we at */
Fabio Utzig74aef312019-11-28 11:05:34 -030081 uint8_t op; /* What operation are we performing? */
Fabio Utzig2473ac02017-05-02 12:45:02 -030082 uint8_t use_scratch; /* Are status bytes ever written to scratch? */
Christopher Collinsa1c12042019-05-23 14:00:28 -070083 uint8_t swap_type; /* The type of swap in effect */
Fabio Utzig46490722017-09-04 15:34:32 -030084 uint32_t swap_size; /* Total size of swapped image */
Fabio Utzigba829042018-09-18 08:29:34 -030085#ifdef MCUBOOT_ENC_IMAGES
Roman Okhrimenko977b3752022-03-31 14:40:48 +030086#define BOOT_UNINITIALIZED_KEY_FILL 0xFF
Fabio Utzig1e4284b2019-08-23 11:55:27 -030087 uint8_t enckey[BOOT_NUM_SLOTS][BOOT_ENC_KEY_SIZE];
Roman Okhrimenko977b3752022-03-31 14:40:48 +030088#ifdef MCUBOOT_SWAP_SAVE_ENCTLV
89#define BOOT_UNINITIALIZED_TLV_FILL 0xFF
Fabio Utzig4741c452019-12-19 15:32:41 -030090 uint8_t enctlv[BOOT_NUM_SLOTS][BOOT_ENC_TLV_ALIGN_SIZE];
91#endif
Fabio Utzigba829042018-09-18 08:29:34 -030092#endif
Fabio Utzigb86e6882019-12-10 09:51:17 -030093 int source; /* Which slot contains swap status metadata */
Christopher Collins92ea77f2016-12-12 15:59:26 -080094};
95
Roman Okhrimenko977b3752022-03-31 14:40:48 +030096#define BOOT_STATUS_IDX_0 1U
Fabio Utzig39000012018-07-30 12:40:20 -030097
98#define BOOT_STATUS_STATE_0 1
99#define BOOT_STATUS_STATE_1 2
100#define BOOT_STATUS_STATE_2 3
Christopher Collins92ea77f2016-12-12 15:59:26 -0800101
102/**
103 * End-of-image slot structure.
104 *
Christopher Collinsa1c12042019-05-23 14:00:28 -0700105 * 0 1 2 3
106 * 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
107 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108 * ~ ~
109 * ~ Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3) ~
110 * ~ ~
111 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112 * | Encryption key 0 (16 octets) [*] |
113 * | |
114 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115 * | Encryption key 1 (16 octets) [*] |
116 * | |
117 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
118 * | Swap size (4 octets) |
119 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Vinczee2453472019-06-17 12:31:59 +0200120 * | Swap info | 0xff padding (7 octets) |
Christopher Collinsa1c12042019-05-23 14:00:28 -0700121 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
122 * | Copy done | 0xff padding (7 octets) |
123 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
124 * | Image OK | 0xff padding (7 octets) |
125 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
126 * | MAGIC (16 octets) |
127 * | |
128 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
129 *
130 * [*]: Only present if the encryption option is enabled
131 * (`MCUBOOT_ENC_IMAGES`).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800132 */
133
134extern const uint32_t boot_img_magic[4];
135
David Vinczeb75c12a2019-03-22 14:58:33 +0100136#ifdef MCUBOOT_IMAGE_NUMBER
137#define BOOT_IMAGE_NUMBER MCUBOOT_IMAGE_NUMBER
138#else
139#define BOOT_IMAGE_NUMBER 1
140#endif
141
Fabio Utzigabec0732019-07-31 08:40:22 -0300142_Static_assert(BOOT_IMAGE_NUMBER > 0, "Invalid value for BOOT_IMAGE_NUMBER");
143
Tamas Banfe031092020-09-10 17:32:39 +0200144#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
145#define ARE_SLOTS_EQUIVALENT() 0
David Vinczee574f2d2020-07-10 11:42:03 +0200146#else
Tamas Banfe031092020-09-10 17:32:39 +0200147#define ARE_SLOTS_EQUIVALENT() 1
David Vinczee574f2d2020-07-10 11:42:03 +0200148
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300149#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_ENC_IMAGES)
150#error "Image encryption (MCUBOOT_ENC_IMAGES) is not supported when MCUBOOT_DIRECT_XIP is selected."
151#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_ENC_IMAGES */
Tamas Banfe031092020-09-10 17:32:39 +0200152#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200153
Marti Bolivarf9bfddd2018-04-24 14:28:33 -0400154#define BOOT_MAX_IMG_SECTORS MCUBOOT_MAX_IMG_SECTORS
Fabio Utziga1fae672018-03-30 10:52:38 -0300155
David Vinczee574f2d2020-07-10 11:42:03 +0200156#define BOOT_LOG_IMAGE_INFO(slot, hdr) \
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300157 BOOT_LOG_INF("%-9s slot: " \
158 "version=%u.%u.%" PRIu16 "+%" PRIu32, \
David Vinczee574f2d2020-07-10 11:42:03 +0200159 ((slot) == BOOT_PRIMARY_SLOT) ? "Primary" : "Secondary", \
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300160 (unsigned)(hdr)->ih_ver.iv_major, \
161 (unsigned)(hdr)->ih_ver.iv_minor, \
David Vinczee574f2d2020-07-10 11:42:03 +0200162 (hdr)->ih_ver.iv_revision, \
163 (hdr)->ih_ver.iv_build_num)
164
David Vinczee2453472019-06-17 12:31:59 +0200165/*
Fabio Utziga1fae672018-03-30 10:52:38 -0300166 * The current flashmap API does not check the amount of space allocated when
167 * loading sector data from the flash device, allowing for smaller counts here
168 * would most surely incur in overruns.
169 *
170 * TODO: make flashmap API receive the current sector array size.
171 */
172#if BOOT_MAX_IMG_SECTORS < 32
173#error "Too few sectors, please increase BOOT_MAX_IMG_SECTORS to at least 32"
174#endif
175
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300176#if defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzig74aef312019-11-28 11:05:34 -0300177#define BOOT_STATUS_MOVE_STATE_COUNT 1
178#define BOOT_STATUS_SWAP_STATE_COUNT 2
179#define BOOT_STATUS_STATE_COUNT (BOOT_STATUS_MOVE_STATE_COUNT + BOOT_STATUS_SWAP_STATE_COUNT)
180#else
David Vincze2d736ad2019-02-18 11:50:22 +0100181#define BOOT_STATUS_STATE_COUNT 3
Fabio Utzig74aef312019-11-28 11:05:34 -0300182#endif
183
184/** Maximum number of image sectors supported by the bootloader. */
David Vincze2d736ad2019-02-18 11:50:22 +0100185#define BOOT_STATUS_MAX_ENTRIES BOOT_MAX_IMG_SECTORS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800186
David Vincze2d736ad2019-02-18 11:50:22 +0100187#define BOOT_PRIMARY_SLOT 0
188#define BOOT_SECONDARY_SLOT 1
Christopher Collins92ea77f2016-12-12 15:59:26 -0800189
David Vincze2d736ad2019-02-18 11:50:22 +0100190#define BOOT_STATUS_SOURCE_NONE 0
191#define BOOT_STATUS_SOURCE_SCRATCH 1
192#define BOOT_STATUS_SOURCE_PRIMARY_SLOT 2
193
Marti Bolivarc50926f2017-06-14 09:35:40 -0400194/**
195 * Compatibility shim for flash sector type.
196 *
197 * This can be deleted when flash_area_to_sectors() is removed.
198 */
199#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
200typedef struct flash_sector boot_sector_t;
201#else
202typedef struct flash_area boot_sector_t;
203#endif
204
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400205/** Private state maintained during boot. */
206struct boot_loader_state {
207 struct {
208 struct image_header hdr;
Marti Bolivarc0b47912017-06-13 17:18:09 -0400209 const struct flash_area *area;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400210 boot_sector_t *sectors;
Marti Bolivar84898652017-06-13 17:20:22 -0400211 size_t num_sectors;
David Vinczeba3bd602019-06-17 16:01:43 +0200212 } imgs[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS];
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400213
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300214#if defined(MCUBOOT_SWAP_USING_SCRATCH)
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200215 struct {
216 const struct flash_area *area;
217 boot_sector_t *sectors;
218 size_t num_sectors;
219 } scratch;
Fabio Utzig12d59162019-11-28 10:01:59 -0300220#endif
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300221#if defined(MCUBOOT_SWAP_USING_STATUS)
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200222 struct {
223 const struct flash_area *area;
224 boot_sector_t *sectors;
225 size_t num_sectors;
226 } status;
227#endif
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400228
David Vinczeba3bd602019-06-17 16:01:43 +0200229 uint8_t swap_type[BOOT_IMAGE_NUMBER];
David Brownab449182019-11-15 09:32:52 -0700230 uint32_t write_sz;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300231
232#if defined(MCUBOOT_ENC_IMAGES)
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300233 struct enc_key_data enc[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS];
Fabio Utzig10ee6482019-08-01 12:04:52 -0300234#endif
235
Fabio Utzigb0f04732019-07-31 09:49:19 -0300236#if (BOOT_IMAGE_NUMBER > 1)
237 uint8_t curr_img_idx;
238#endif
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400239};
240
Raef Colese8fe6cf2020-05-26 13:07:40 +0100241fih_int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
242 size_t slen, uint8_t key_id);
243
244fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800245
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300246bool boot_magic_compatible_check(uint8_t tbl_val, uint8_t val);
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300247uint32_t boot_status_sz(uint32_t min_write_sz);
David Brownab449182019-11-15 09:32:52 -0700248uint32_t boot_trailer_sz(uint32_t min_write_sz);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300249int boot_status_entries(int image_index, const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800250uint32_t boot_status_off(const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800251int boot_write_magic(const struct flash_area *fap);
Fabio Utzig12d59162019-11-28 10:01:59 -0300252int boot_write_status(const struct boot_loader_state *state, struct boot_status *bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800253int boot_write_copy_done(const struct flash_area *fap);
254int boot_write_image_ok(const struct flash_area *fap);
David Vinczee2453472019-06-17 12:31:59 +0200255int boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
256 uint8_t image_num);
Fabio Utzig46490722017-09-04 15:34:32 -0300257int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000258int boot_write_trailer(const struct flash_area *fap, uint32_t off,
259 const uint8_t *inbuf, uint8_t inlen);
260int boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
261 uint8_t flag_val);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300262int boot_read_swap_size(int image_index, uint32_t *swap_size);
Fabio Utzig12d59162019-11-28 10:01:59 -0300263int boot_slots_compatible(struct boot_loader_state *state);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300264uint32_t boot_status_internal_off(const struct boot_status *bs, uint32_t elem_sz);
Fabio Utzig12d59162019-11-28 10:01:59 -0300265int boot_read_image_header(struct boot_loader_state *state, int slot,
266 struct image_header *out_hdr, struct boot_status *bs);
267int boot_copy_region(struct boot_loader_state *state,
268 const struct flash_area *fap_src,
269 const struct flash_area *fap_dst,
270 uint32_t off_src, uint32_t off_dst, uint32_t sz);
271int boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz);
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300272bool boot_status_is_reset(const struct boot_status *bs);
Fabio Utzig12d59162019-11-28 10:01:59 -0300273
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200274#ifdef MCUBOOT_SWAP_USING_STATUS
275uint32_t boot_copy_done_off(const struct flash_area *fap);
276uint32_t boot_image_ok_off(const struct flash_area *fap);
277uint32_t boot_swap_size_off(const struct flash_area *fap);
278#endif
279
280
Fabio Utzigba829042018-09-18 08:29:34 -0300281#ifdef MCUBOOT_ENC_IMAGES
282int boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
Fabio Utzig4741c452019-12-19 15:32:41 -0300283 const struct boot_status *bs);
284int boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300285#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800286
David Browneffb06e2019-10-10 14:45:32 -0600287/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300288 * Checks that a buffer is filled by the specified value.
289 *
290 * @returns true if all bytes in the buffer are equal to `fill`; false if any
291 * of the bytes does not match, or when buffer is NULL, or when len == 0.
292 */
293bool bootutil_buffer_is_filled(const void *buffer, uint8_t fill, size_t len);
294
295/**
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300296 * Checks that a buffer is erased according to what the erase value for the
297 * flash device provided in `flash_area` is.
298 *
299 * @returns true if the buffer is erased; false if any of the bytes is not
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300300 * erased, or when area is NULL, or when buffer is NULL, or when len == 0.
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300301 */
302bool bootutil_buffer_is_erased(const struct flash_area *area,
303 const void *buffer, size_t len);
304
305/**
David Browneffb06e2019-10-10 14:45:32 -0600306 * Safe (non-overflowing) uint32_t addition. Returns true, and stores
307 * the result in *dest if it can be done without overflow. Otherwise,
308 * returns false.
309 */
310static inline bool boot_u32_safe_add(uint32_t *dest, uint32_t a, uint32_t b)
311{
312 /*
313 * "a + b <= UINT32_MAX", subtract 'b' from both sides to avoid
314 * the overflow.
315 */
316 if (a > UINT32_MAX - b) {
317 return false;
318 } else {
319 *dest = a + b;
320 return true;
321 }
322}
323
324/**
325 * Safe (non-overflowing) uint16_t addition. Returns true, and stores
326 * the result in *dest if it can be done without overflow. Otherwise,
327 * returns false.
328 */
329static inline bool boot_u16_safe_add(uint16_t *dest, uint16_t a, uint16_t b)
330{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300331 uint32_t tmp = (uint32_t)a + b;
David Browneffb06e2019-10-10 14:45:32 -0600332 if (tmp > UINT16_MAX) {
333 return false;
334 } else {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300335 *dest = (uint16_t) tmp;
David Browneffb06e2019-10-10 14:45:32 -0600336 return true;
337 }
338}
339
Marti Bolivarf804f622017-06-12 15:41:48 -0400340/*
341 * Accessors for the contents of struct boot_loader_state.
342 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300343#if (BOOT_IMAGE_NUMBER > 1)
344#define BOOT_CURR_IMG(state) ((state)->curr_img_idx)
345#else
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300346#define BOOT_CURR_IMG(state) 0u
Fabio Utzigbc077932019-08-26 11:16:34 -0300347#endif
348#ifdef MCUBOOT_ENC_IMAGES
349#define BOOT_CURR_ENC(state) ((state)->enc[BOOT_CURR_IMG(state)])
350#else
351#define BOOT_CURR_ENC(state) NULL
Fabio Utzigb0f04732019-07-31 09:49:19 -0300352#endif
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300353/* These are macros so they can be used as lvalues. */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300354#define BOOT_IMG(state, slot) ((state)->imgs[BOOT_CURR_IMG(state)][(slot)])
David Vinczeba3bd602019-06-17 16:01:43 +0200355#define BOOT_IMG_AREA(state, slot) (BOOT_IMG(state, slot).area)
Marti Bolivare10a7392017-06-14 16:20:07 -0400356#define BOOT_WRITE_SZ(state) ((state)->write_sz)
Fabio Utzigb0f04732019-07-31 09:49:19 -0300357#define BOOT_SWAP_TYPE(state) ((state)->swap_type[BOOT_CURR_IMG(state)])
Fabio Utzigc9621352019-08-08 12:15:51 -0300358#define BOOT_TLV_OFF(hdr) ((hdr)->ih_hdr_size + (hdr)->ih_img_size)
Marti Bolivarc0b47912017-06-13 17:18:09 -0400359
Fabio Utzige575b0b2019-09-11 12:34:23 -0300360#define BOOT_IS_UPGRADE(swap_type) \
361 (((swap_type) == BOOT_SWAP_TYPE_TEST) || \
362 ((swap_type) == BOOT_SWAP_TYPE_REVERT) || \
363 ((swap_type) == BOOT_SWAP_TYPE_PERM))
364
Marti Bolivarf804f622017-06-12 15:41:48 -0400365static inline struct image_header*
366boot_img_hdr(struct boot_loader_state *state, size_t slot)
367{
David Vinczeba3bd602019-06-17 16:01:43 +0200368 return &BOOT_IMG(state, slot).hdr;
Marti Bolivarf804f622017-06-12 15:41:48 -0400369}
370
Marti Bolivard3269fd2017-06-12 16:31:12 -0400371static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300372boot_img_num_sectors(const struct boot_loader_state *state, size_t slot)
Marti Bolivard3269fd2017-06-12 16:31:12 -0400373{
David Vinczeba3bd602019-06-17 16:01:43 +0200374 return BOOT_IMG(state, slot).num_sectors;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400375}
376
Marti Bolivar135b8f62017-06-13 17:22:13 -0400377/*
378 * Offset of the slot from the beginning of the flash device.
379 */
380static inline uint32_t
381boot_img_slot_off(struct boot_loader_state *state, size_t slot)
382{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300383 return flash_area_get_off(BOOT_IMG(state, slot).area);
Marti Bolivar135b8f62017-06-13 17:22:13 -0400384}
385
Marti Bolivarc50926f2017-06-14 09:35:40 -0400386#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
387
Marti Bolivard3269fd2017-06-12 16:31:12 -0400388static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300389boot_img_sector_size(const struct boot_loader_state *state,
Marti Bolivard3269fd2017-06-12 16:31:12 -0400390 size_t slot, size_t sector)
391{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300392 return flash_area_get_size(&BOOT_IMG(state, slot).sectors[sector]);
Marti Bolivard3269fd2017-06-12 16:31:12 -0400393}
394
Marti Bolivarea088872017-06-12 17:10:49 -0400395/*
396 * Offset of the sector from the beginning of the image, NOT the flash
397 * device.
398 */
399static inline uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300400boot_img_sector_off(const struct boot_loader_state *state, size_t slot,
Marti Bolivarea088872017-06-12 17:10:49 -0400401 size_t sector)
402{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300403 return flash_area_get_off(&BOOT_IMG(state, slot).sectors[sector]) -
404 flash_area_get_off(&BOOT_IMG(state, slot).sectors[0]);
Marti Bolivarea088872017-06-12 17:10:49 -0400405}
406
Marti Bolivarc50926f2017-06-14 09:35:40 -0400407#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
408
409static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300410boot_img_sector_size(const struct boot_loader_state *state,
Marti Bolivarc50926f2017-06-14 09:35:40 -0400411 size_t slot, size_t sector)
412{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300413 return flash_sector_get_size(&BOOT_IMG(state, slot).sectors[sector]);
Marti Bolivarc50926f2017-06-14 09:35:40 -0400414}
415
416static inline uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300417boot_img_sector_off(const struct boot_loader_state *state, size_t slot,
Marti Bolivarc50926f2017-06-14 09:35:40 -0400418 size_t sector)
419{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300420 return flash_sector_get_off(&BOOT_IMG(state, slot).sectors[sector]) -
421 flash_sector_get_off(&BOOT_IMG(state, slot).sectors[0]);
Marti Bolivarc50926f2017-06-14 09:35:40 -0400422}
423
Marti Bolivarc50926f2017-06-14 09:35:40 -0400424#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
425
Tamas Banfe031092020-09-10 17:32:39 +0200426#ifdef MCUBOOT_RAM_LOAD
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300427# ifdef __BOOTSIM__
428
429/* Query for the layout of a RAM buffer appropriate for holding the
430 * image. This will be per-test-thread, and therefore must be queried
431 * through this call. */
432struct bootsim_ram_info {
433 uint32_t start;
434 uint32_t size;
435 uintptr_t base;
436};
437struct bootsim_ram_info *bootsim_get_ram_info(void);
438
439#define IMAGE_GET_FIELD(field) (bootsim_get_ram_info()->field)
440#define IMAGE_RAM_BASE IMAGE_GET_FIELD(base)
441#define IMAGE_EXECUTABLE_RAM_START IMAGE_GET_FIELD(start)
442#define IMAGE_EXECUTABLE_RAM_SIZE IMAGE_GET_FIELD(size)
443
444# else
445# define IMAGE_RAM_BASE ((uintptr_t)0)
446# endif
447
Tamas Banfe031092020-09-10 17:32:39 +0200448#define LOAD_IMAGE_DATA(hdr, fap, start, output, size) \
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300449 (memcpy((output),(void*)(IMAGE_RAM_BASE + (hdr)->ih_load_addr + (start)), \
Tamas Bane4885a62020-11-12 13:50:07 +0000450 (size)), 0)
Tamas Banfe031092020-09-10 17:32:39 +0200451#else
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300452#define IMAGE_RAM_BASE ((uintptr_t)0)
453
Tamas Banfe031092020-09-10 17:32:39 +0200454#define LOAD_IMAGE_DATA(hdr, fap, start, output, size) \
455 (flash_area_read((fap), (start), (output), (size)))
456#endif /* MCUBOOT_RAM_LOAD */
457
Christopher Collins92ea77f2016-12-12 15:59:26 -0800458#ifdef __cplusplus
459}
460#endif
461
462#endif