blob: f7ca5c9255a2f5e8e3460d510c0e018bacfe796a [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
Mark Horvathccaf7f82021-01-04 18:16:42 +01006 * 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
Tamas Banfe031092020-09-10 17:32:39 +020031#include <string.h>
32
Marti Bolivarcca28a92017-06-12 16:52:22 -040033#include "sysflash/sysflash.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020034
35#include <flash_map_backend/flash_map_backend.h>
36
Christopher Collins01dfbb62019-03-21 07:28:37 -070037#include "bootutil/bootutil.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080038#include "bootutil/image.h"
Raef Colese8fe6cf2020-05-26 13:07:40 +010039#include "bootutil/fault_injection_hardening.h"
Marti Bolivarf91bca52018-04-12 12:40:46 -040040#include "mcuboot_config/mcuboot_config.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080041
Fabio Utzigba829042018-09-18 08:29:34 -030042#ifdef MCUBOOT_ENC_IMAGES
43#include "bootutil/enc_key.h"
44#endif
45
Christopher Collins92ea77f2016-12-12 15:59:26 -080046#ifdef __cplusplus
47extern "C" {
48#endif
49
50struct flash_area;
51
Christopher Collins92ea77f2016-12-12 15:59:26 -080052#define BOOT_TMPBUF_SZ 256
53
Fabio Utzig1e4284b2019-08-23 11:55:27 -030054/** Number of image slots in flash; currently limited to two. */
55#define BOOT_NUM_SLOTS 2
56
David Vinczee574f2d2020-07-10 11:42:03 +020057#if (defined(MCUBOOT_OVERWRITE_ONLY) + \
58 defined(MCUBOOT_SWAP_USING_MOVE) + \
Tamas Banfe031092020-09-10 17:32:39 +020059 defined(MCUBOOT_DIRECT_XIP) + \
60 defined(MCUBOOT_RAM_LOAD)) > 1
61#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 -030062#endif
63
David Vinczee574f2d2020-07-10 11:42:03 +020064#if !defined(MCUBOOT_OVERWRITE_ONLY) && \
65 !defined(MCUBOOT_SWAP_USING_MOVE) && \
Tamas Banfe031092020-09-10 17:32:39 +020066 !defined(MCUBOOT_DIRECT_XIP) && \
67 !defined(MCUBOOT_RAM_LOAD)
Fabio Utzig12d59162019-11-28 10:01:59 -030068#define MCUBOOT_SWAP_USING_SCRATCH 1
69#endif
70
Fabio Utzig74aef312019-11-28 11:05:34 -030071#define BOOT_STATUS_OP_MOVE 1
72#define BOOT_STATUS_OP_SWAP 2
73
Christopher Collins92ea77f2016-12-12 15:59:26 -080074/*
75 * Maintain state of copy progress.
76 */
77struct boot_status {
Fabio Utzig2473ac02017-05-02 12:45:02 -030078 uint32_t idx; /* Which area we're operating on */
79 uint8_t state; /* Which part of the swapping process are we at */
Fabio Utzig74aef312019-11-28 11:05:34 -030080 uint8_t op; /* What operation are we performing? */
Fabio Utzig2473ac02017-05-02 12:45:02 -030081 uint8_t use_scratch; /* Are status bytes ever written to scratch? */
Christopher Collinsa1c12042019-05-23 14:00:28 -070082 uint8_t swap_type; /* The type of swap in effect */
Fabio Utzig46490722017-09-04 15:34:32 -030083 uint32_t swap_size; /* Total size of swapped image */
Fabio Utzigba829042018-09-18 08:29:34 -030084#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig1e4284b2019-08-23 11:55:27 -030085 uint8_t enckey[BOOT_NUM_SLOTS][BOOT_ENC_KEY_SIZE];
Fabio Utzig4741c452019-12-19 15:32:41 -030086#if MCUBOOT_SWAP_SAVE_ENCTLV
87 uint8_t enctlv[BOOT_NUM_SLOTS][BOOT_ENC_TLV_ALIGN_SIZE];
88#endif
Fabio Utzigba829042018-09-18 08:29:34 -030089#endif
Fabio Utzigb86e6882019-12-10 09:51:17 -030090 int source; /* Which slot contains swap status metadata */
Christopher Collins92ea77f2016-12-12 15:59:26 -080091};
92
Fabio Utzig39000012018-07-30 12:40:20 -030093#define BOOT_STATUS_IDX_0 1
94
95#define BOOT_STATUS_STATE_0 1
96#define BOOT_STATUS_STATE_1 2
97#define BOOT_STATUS_STATE_2 3
Christopher Collins92ea77f2016-12-12 15:59:26 -080098
99/**
100 * End-of-image slot structure.
101 *
Christopher Collinsa1c12042019-05-23 14:00:28 -0700102 * 0 1 2 3
103 * 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
104 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 * ~ ~
106 * ~ Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3) ~
107 * ~ ~
108 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 * | Encryption key 0 (16 octets) [*] |
110 * | |
111 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112 * | Encryption key 1 (16 octets) [*] |
113 * | |
114 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115 * | Swap size (4 octets) |
116 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Vinczee2453472019-06-17 12:31:59 +0200117 * | Swap info | 0xff padding (7 octets) |
Christopher Collinsa1c12042019-05-23 14:00:28 -0700118 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
119 * | Copy done | 0xff padding (7 octets) |
120 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
121 * | Image OK | 0xff padding (7 octets) |
122 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
123 * | MAGIC (16 octets) |
124 * | |
125 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
126 *
127 * [*]: Only present if the encryption option is enabled
128 * (`MCUBOOT_ENC_IMAGES`).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800129 */
130
131extern const uint32_t boot_img_magic[4];
132
David Vinczeb75c12a2019-03-22 14:58:33 +0100133#ifdef MCUBOOT_IMAGE_NUMBER
134#define BOOT_IMAGE_NUMBER MCUBOOT_IMAGE_NUMBER
135#else
136#define BOOT_IMAGE_NUMBER 1
137#endif
138
Fabio Utzigabec0732019-07-31 08:40:22 -0300139_Static_assert(BOOT_IMAGE_NUMBER > 0, "Invalid value for BOOT_IMAGE_NUMBER");
140
Tamas Banfe031092020-09-10 17:32:39 +0200141#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
142#define ARE_SLOTS_EQUIVALENT() 0
David Vinczee574f2d2020-07-10 11:42:03 +0200143#else
Tamas Banfe031092020-09-10 17:32:39 +0200144#define ARE_SLOTS_EQUIVALENT() 1
David Vinczee574f2d2020-07-10 11:42:03 +0200145
David Vinczee574f2d2020-07-10 11:42:03 +0200146#ifdef MCUBOOT_ENC_IMAGES
Tamas Banfe031092020-09-10 17:32:39 +0200147#error "Image encryption (MCUBOOT_ENC_IMAGES) is not supported when MCUBOOT_DIRECT_XIP or MCUBOOT_RAM_LOAD mode is selected."
David Vinczee574f2d2020-07-10 11:42:03 +0200148#endif
Tamas Banfe031092020-09-10 17:32:39 +0200149#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200150
Marti Bolivarf9bfddd2018-04-24 14:28:33 -0400151#define BOOT_MAX_IMG_SECTORS MCUBOOT_MAX_IMG_SECTORS
Fabio Utziga1fae672018-03-30 10:52:38 -0300152
David Vinczee574f2d2020-07-10 11:42:03 +0200153#define BOOT_LOG_IMAGE_INFO(slot, hdr) \
154 BOOT_LOG_INF("%-9s slot: version=%u.%u.%u+%u", \
155 ((slot) == BOOT_PRIMARY_SLOT) ? "Primary" : "Secondary", \
156 (hdr)->ih_ver.iv_major, \
157 (hdr)->ih_ver.iv_minor, \
158 (hdr)->ih_ver.iv_revision, \
159 (hdr)->ih_ver.iv_build_num)
160
David Vinczee2453472019-06-17 12:31:59 +0200161/*
Fabio Utziga1fae672018-03-30 10:52:38 -0300162 * The current flashmap API does not check the amount of space allocated when
163 * loading sector data from the flash device, allowing for smaller counts here
164 * would most surely incur in overruns.
165 *
166 * TODO: make flashmap API receive the current sector array size.
167 */
168#if BOOT_MAX_IMG_SECTORS < 32
169#error "Too few sectors, please increase BOOT_MAX_IMG_SECTORS to at least 32"
170#endif
171
Fabio Utzig74aef312019-11-28 11:05:34 -0300172#if MCUBOOT_SWAP_USING_MOVE
173#define BOOT_STATUS_MOVE_STATE_COUNT 1
174#define BOOT_STATUS_SWAP_STATE_COUNT 2
175#define BOOT_STATUS_STATE_COUNT (BOOT_STATUS_MOVE_STATE_COUNT + BOOT_STATUS_SWAP_STATE_COUNT)
176#else
David Vincze2d736ad2019-02-18 11:50:22 +0100177#define BOOT_STATUS_STATE_COUNT 3
Fabio Utzig74aef312019-11-28 11:05:34 -0300178#endif
179
180/** Maximum number of image sectors supported by the bootloader. */
David Vincze2d736ad2019-02-18 11:50:22 +0100181#define BOOT_STATUS_MAX_ENTRIES BOOT_MAX_IMG_SECTORS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182
David Vincze2d736ad2019-02-18 11:50:22 +0100183#define BOOT_PRIMARY_SLOT 0
184#define BOOT_SECONDARY_SLOT 1
Christopher Collins92ea77f2016-12-12 15:59:26 -0800185
David Vincze2d736ad2019-02-18 11:50:22 +0100186#define BOOT_STATUS_SOURCE_NONE 0
187#define BOOT_STATUS_SOURCE_SCRATCH 1
188#define BOOT_STATUS_SOURCE_PRIMARY_SLOT 2
189
Marti Bolivarc50926f2017-06-14 09:35:40 -0400190/**
191 * Compatibility shim for flash sector type.
192 *
193 * This can be deleted when flash_area_to_sectors() is removed.
194 */
195#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
196typedef struct flash_sector boot_sector_t;
197#else
198typedef struct flash_area boot_sector_t;
199#endif
200
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400201/** Private state maintained during boot. */
202struct boot_loader_state {
203 struct {
204 struct image_header hdr;
Marti Bolivarc0b47912017-06-13 17:18:09 -0400205 const struct flash_area *area;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400206 boot_sector_t *sectors;
Dominik Ermel7d747232021-05-24 17:10:49 +0000207 uint32_t num_sectors;
David Vinczeba3bd602019-06-17 16:01:43 +0200208 } imgs[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS];
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400209
Fabio Utzig12d59162019-11-28 10:01:59 -0300210#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200211 struct {
212 const struct flash_area *area;
213 boot_sector_t *sectors;
Dominik Ermel7d747232021-05-24 17:10:49 +0000214 uint32_t num_sectors;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200215 } scratch;
Fabio Utzig12d59162019-11-28 10:01:59 -0300216#endif
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400217
David Vinczeba3bd602019-06-17 16:01:43 +0200218 uint8_t swap_type[BOOT_IMAGE_NUMBER];
David Brownab449182019-11-15 09:32:52 -0700219 uint32_t write_sz;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300220
221#if defined(MCUBOOT_ENC_IMAGES)
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300222 struct enc_key_data enc[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS];
Fabio Utzig10ee6482019-08-01 12:04:52 -0300223#endif
224
Fabio Utzigb0f04732019-07-31 09:49:19 -0300225#if (BOOT_IMAGE_NUMBER > 1)
226 uint8_t curr_img_idx;
227#endif
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400228};
229
Raef Colese8fe6cf2020-05-26 13:07:40 +0100230fih_int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
231 size_t slen, uint8_t key_id);
232
233fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800234
Christopher Collinsa1c12042019-05-23 14:00:28 -0700235int boot_magic_compatible_check(uint8_t tbl_val, uint8_t val);
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300236uint32_t boot_status_sz(uint32_t min_write_sz);
David Brownab449182019-11-15 09:32:52 -0700237uint32_t boot_trailer_sz(uint32_t min_write_sz);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300238int boot_status_entries(int image_index, const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800239uint32_t boot_status_off(const struct flash_area *fap);
240int boot_read_swap_state(const struct flash_area *fap,
241 struct boot_swap_state *state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300242int boot_read_swap_state_by_id(int flash_area_id,
243 struct boot_swap_state *state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800244int boot_write_magic(const struct flash_area *fap);
Fabio Utzig12d59162019-11-28 10:01:59 -0300245int boot_write_status(const struct boot_loader_state *state, struct boot_status *bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800246int boot_write_copy_done(const struct flash_area *fap);
247int boot_write_image_ok(const struct flash_area *fap);
David Vinczee2453472019-06-17 12:31:59 +0200248int boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
249 uint8_t image_num);
Fabio Utzig46490722017-09-04 15:34:32 -0300250int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
Dominik Ermela7f9e9f2021-03-24 17:31:57 +0000251int boot_write_trailer(const struct flash_area *fap, uint32_t off,
252 const uint8_t *inbuf, uint8_t inlen);
253int boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
254 uint8_t flag_val);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300255int boot_read_swap_size(int image_index, uint32_t *swap_size);
Fabio Utzig12d59162019-11-28 10:01:59 -0300256int boot_slots_compatible(struct boot_loader_state *state);
257uint32_t boot_status_internal_off(const struct boot_status *bs, int elem_sz);
258int boot_read_image_header(struct boot_loader_state *state, int slot,
259 struct image_header *out_hdr, struct boot_status *bs);
260int boot_copy_region(struct boot_loader_state *state,
261 const struct flash_area *fap_src,
262 const struct flash_area *fap_dst,
263 uint32_t off_src, uint32_t off_dst, uint32_t sz);
264int boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz);
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300265bool boot_status_is_reset(const struct boot_status *bs);
Fabio Utzig12d59162019-11-28 10:01:59 -0300266
Fabio Utzigba829042018-09-18 08:29:34 -0300267#ifdef MCUBOOT_ENC_IMAGES
268int boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
Fabio Utzig4741c452019-12-19 15:32:41 -0300269 const struct boot_status *bs);
270int boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300271#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800272
David Browneffb06e2019-10-10 14:45:32 -0600273/**
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300274 * Checks that a buffer is erased according to what the erase value for the
275 * flash device provided in `flash_area` is.
276 *
277 * @returns true if the buffer is erased; false if any of the bytes is not
278 * erased, or when buffer is NULL, or when len == 0.
279 */
280bool bootutil_buffer_is_erased(const struct flash_area *area,
281 const void *buffer, size_t len);
282
283/**
David Browneffb06e2019-10-10 14:45:32 -0600284 * Safe (non-overflowing) uint32_t addition. Returns true, and stores
285 * the result in *dest if it can be done without overflow. Otherwise,
286 * returns false.
287 */
288static inline bool boot_u32_safe_add(uint32_t *dest, uint32_t a, uint32_t b)
289{
290 /*
291 * "a + b <= UINT32_MAX", subtract 'b' from both sides to avoid
292 * the overflow.
293 */
294 if (a > UINT32_MAX - b) {
295 return false;
296 } else {
297 *dest = a + b;
298 return true;
299 }
300}
301
302/**
303 * Safe (non-overflowing) uint16_t addition. Returns true, and stores
304 * the result in *dest if it can be done without overflow. Otherwise,
305 * returns false.
306 */
307static inline bool boot_u16_safe_add(uint16_t *dest, uint16_t a, uint16_t b)
308{
309 uint32_t tmp = a + b;
310 if (tmp > UINT16_MAX) {
311 return false;
312 } else {
313 *dest = tmp;
314 return true;
315 }
316}
317
Marti Bolivarf804f622017-06-12 15:41:48 -0400318/*
319 * Accessors for the contents of struct boot_loader_state.
320 */
321
Marti Bolivarc0b47912017-06-13 17:18:09 -0400322/* These are macros so they can be used as lvalues. */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300323#if (BOOT_IMAGE_NUMBER > 1)
324#define BOOT_CURR_IMG(state) ((state)->curr_img_idx)
325#else
326#define BOOT_CURR_IMG(state) 0
Fabio Utzigbc077932019-08-26 11:16:34 -0300327#endif
328#ifdef MCUBOOT_ENC_IMAGES
329#define BOOT_CURR_ENC(state) ((state)->enc[BOOT_CURR_IMG(state)])
330#else
331#define BOOT_CURR_ENC(state) NULL
Fabio Utzigb0f04732019-07-31 09:49:19 -0300332#endif
333#define BOOT_IMG(state, slot) ((state)->imgs[BOOT_CURR_IMG(state)][(slot)])
David Vinczeba3bd602019-06-17 16:01:43 +0200334#define BOOT_IMG_AREA(state, slot) (BOOT_IMG(state, slot).area)
Marti Bolivare10a7392017-06-14 16:20:07 -0400335#define BOOT_WRITE_SZ(state) ((state)->write_sz)
Fabio Utzigb0f04732019-07-31 09:49:19 -0300336#define BOOT_SWAP_TYPE(state) ((state)->swap_type[BOOT_CURR_IMG(state)])
Fabio Utzigc9621352019-08-08 12:15:51 -0300337#define BOOT_TLV_OFF(hdr) ((hdr)->ih_hdr_size + (hdr)->ih_img_size)
Marti Bolivarc0b47912017-06-13 17:18:09 -0400338
Fabio Utzige575b0b2019-09-11 12:34:23 -0300339#define BOOT_IS_UPGRADE(swap_type) \
340 (((swap_type) == BOOT_SWAP_TYPE_TEST) || \
341 ((swap_type) == BOOT_SWAP_TYPE_REVERT) || \
342 ((swap_type) == BOOT_SWAP_TYPE_PERM))
343
Marti Bolivarf804f622017-06-12 15:41:48 -0400344static inline struct image_header*
345boot_img_hdr(struct boot_loader_state *state, size_t slot)
346{
David Vinczeba3bd602019-06-17 16:01:43 +0200347 return &BOOT_IMG(state, slot).hdr;
Marti Bolivarf804f622017-06-12 15:41:48 -0400348}
349
Marti Bolivard3269fd2017-06-12 16:31:12 -0400350static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300351boot_img_num_sectors(const struct boot_loader_state *state, size_t slot)
Marti Bolivard3269fd2017-06-12 16:31:12 -0400352{
David Vinczeba3bd602019-06-17 16:01:43 +0200353 return BOOT_IMG(state, slot).num_sectors;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400354}
355
Marti Bolivar135b8f62017-06-13 17:22:13 -0400356/*
357 * Offset of the slot from the beginning of the flash device.
358 */
359static inline uint32_t
360boot_img_slot_off(struct boot_loader_state *state, size_t slot)
361{
David Vinczeba3bd602019-06-17 16:01:43 +0200362 return BOOT_IMG(state, slot).area->fa_off;
Marti Bolivar135b8f62017-06-13 17:22:13 -0400363}
364
Marti Bolivarc50926f2017-06-14 09:35:40 -0400365#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
366
Marti Bolivard3269fd2017-06-12 16:31:12 -0400367static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300368boot_img_sector_size(const struct boot_loader_state *state,
Marti Bolivard3269fd2017-06-12 16:31:12 -0400369 size_t slot, size_t sector)
370{
David Vinczeba3bd602019-06-17 16:01:43 +0200371 return BOOT_IMG(state, slot).sectors[sector].fa_size;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400372}
373
Marti Bolivarea088872017-06-12 17:10:49 -0400374/*
375 * Offset of the sector from the beginning of the image, NOT the flash
376 * device.
377 */
378static inline uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300379boot_img_sector_off(const struct boot_loader_state *state, size_t slot,
Marti Bolivarea088872017-06-12 17:10:49 -0400380 size_t sector)
381{
David Vinczeba3bd602019-06-17 16:01:43 +0200382 return BOOT_IMG(state, slot).sectors[sector].fa_off -
383 BOOT_IMG(state, slot).sectors[0].fa_off;
Marti Bolivarea088872017-06-12 17:10:49 -0400384}
385
Marti Bolivarc50926f2017-06-14 09:35:40 -0400386#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
387
388static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300389boot_img_sector_size(const struct boot_loader_state *state,
Marti Bolivarc50926f2017-06-14 09:35:40 -0400390 size_t slot, size_t sector)
391{
David Vinczeba3bd602019-06-17 16:01:43 +0200392 return BOOT_IMG(state, slot).sectors[sector].fs_size;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400393}
394
395static inline uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300396boot_img_sector_off(const struct boot_loader_state *state, size_t slot,
Marti Bolivarc50926f2017-06-14 09:35:40 -0400397 size_t sector)
398{
David Vinczeba3bd602019-06-17 16:01:43 +0200399 return BOOT_IMG(state, slot).sectors[sector].fs_off -
400 BOOT_IMG(state, slot).sectors[0].fs_off;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400401}
402
Marti Bolivarc50926f2017-06-14 09:35:40 -0400403#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
404
Tamas Banfe031092020-09-10 17:32:39 +0200405#ifdef MCUBOOT_RAM_LOAD
406#define LOAD_IMAGE_DATA(hdr, fap, start, output, size) \
407 (memcpy((output),(void*)((hdr)->ih_load_addr + (start)), \
Tamas Bane4885a62020-11-12 13:50:07 +0000408 (size)), 0)
Tamas Banfe031092020-09-10 17:32:39 +0200409#else
410#define LOAD_IMAGE_DATA(hdr, fap, start, output, size) \
411 (flash_area_read((fap), (start), (output), (size)))
412#endif /* MCUBOOT_RAM_LOAD */
413
Christopher Collins92ea77f2016-12-12 15:59:26 -0800414#ifdef __cplusplus
415}
416#endif
417
418#endif