blob: df49453e674d67209007b76efdf5107f92c839a1 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Vinczee2453472019-06-17 12:31:59 +020020/*
21 * Modifications are Copyright (c) 2019 Arm Limited.
22 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024#ifndef H_BOOTUTIL_PRIV_
25#define H_BOOTUTIL_PRIV_
26
Marti Bolivarcca28a92017-06-12 16:52:22 -040027#include "sysflash/sysflash.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020028
29#include <flash_map_backend/flash_map_backend.h>
30
Christopher Collins01dfbb62019-03-21 07:28:37 -070031#include "bootutil/bootutil.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include "bootutil/image.h"
Marti Bolivarf91bca52018-04-12 12:40:46 -040033#include "mcuboot_config/mcuboot_config.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080034
Fabio Utzigba829042018-09-18 08:29:34 -030035#ifdef MCUBOOT_ENC_IMAGES
36#include "bootutil/enc_key.h"
37#endif
38
Christopher Collins92ea77f2016-12-12 15:59:26 -080039#ifdef __cplusplus
40extern "C" {
41#endif
42
Marti Bolivar248da082018-04-24 15:11:39 -040043#ifdef MCUBOOT_HAVE_ASSERT_H
44#include "mcuboot_config/mcuboot_assert.h"
Fabio Utzig57c40f72017-12-12 21:48:30 -020045#else
46#define ASSERT assert
47#endif
48
Christopher Collins92ea77f2016-12-12 15:59:26 -080049struct flash_area;
50
David Vinczee32483f2019-06-13 10:46:24 +020051#define BOOT_EFLASH 1
52#define BOOT_EFILE 2
53#define BOOT_EBADIMAGE 3
54#define BOOT_EBADVECT 4
55#define BOOT_EBADSTATUS 5
56#define BOOT_ENOMEM 6
57#define BOOT_EBADARGS 7
58#define BOOT_EBADVERSION 8
Christopher Collins92ea77f2016-12-12 15:59:26 -080059
60#define BOOT_TMPBUF_SZ 256
61
Fabio Utzig1e4284b2019-08-23 11:55:27 -030062/** Number of image slots in flash; currently limited to two. */
63#define BOOT_NUM_SLOTS 2
64
Fabio Utzig74aef312019-11-28 11:05:34 -030065#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_SWAP_USING_MOVE)
66#error "Please enable only one of MCUBOOT_OVERWRITE_ONLY or MCUBOOT_SWAP_USING_MOVE"
67#endif
68
69#if !defined(MCUBOOT_OVERWRITE_ONLY) && !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzig12d59162019-11-28 10:01:59 -030070#define MCUBOOT_SWAP_USING_SCRATCH 1
71#endif
72
Fabio Utzig74aef312019-11-28 11:05:34 -030073#define BOOT_STATUS_OP_MOVE 1
74#define BOOT_STATUS_OP_SWAP 2
75
Christopher Collins92ea77f2016-12-12 15:59:26 -080076/*
77 * Maintain state of copy progress.
78 */
79struct boot_status {
Fabio Utzig2473ac02017-05-02 12:45:02 -030080 uint32_t idx; /* Which area we're operating on */
81 uint8_t state; /* Which part of the swapping process are we at */
Fabio Utzig74aef312019-11-28 11:05:34 -030082 uint8_t op; /* What operation are we performing? */
Fabio Utzig2473ac02017-05-02 12:45:02 -030083 uint8_t use_scratch; /* Are status bytes ever written to scratch? */
Christopher Collinsa1c12042019-05-23 14:00:28 -070084 uint8_t swap_type; /* The type of swap in effect */
Fabio Utzig46490722017-09-04 15:34:32 -030085 uint32_t swap_size; /* Total size of swapped image */
Fabio Utzigba829042018-09-18 08:29:34 -030086#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig1e4284b2019-08-23 11:55:27 -030087 uint8_t enckey[BOOT_NUM_SLOTS][BOOT_ENC_KEY_SIZE];
Fabio Utzigba829042018-09-18 08:29:34 -030088#endif
Fabio Utzigb86e6882019-12-10 09:51:17 -030089 int source; /* Which slot contains swap status metadata */
Christopher Collins92ea77f2016-12-12 15:59:26 -080090};
91
Fabio Utzig39000012018-07-30 12:40:20 -030092#define BOOT_MAGIC_GOOD 1
93#define BOOT_MAGIC_BAD 2
94#define BOOT_MAGIC_UNSET 3
95#define BOOT_MAGIC_ANY 4 /* NOTE: control only, not dependent on sector */
Christopher Collinsa1c12042019-05-23 14:00:28 -070096#define BOOT_MAGIC_NOTGOOD 5 /* NOTE: control only, not dependent on sector */
Fabio Utzig39000012018-07-30 12:40:20 -030097
98/*
99 * NOTE: leave BOOT_FLAG_SET equal to one, this is written to flash!
100 */
101#define BOOT_FLAG_SET 1
102#define BOOT_FLAG_BAD 2
103#define BOOT_FLAG_UNSET 3
104#define BOOT_FLAG_ANY 4 /* NOTE: control only, not dependent on sector */
105
106#define BOOT_STATUS_IDX_0 1
107
108#define BOOT_STATUS_STATE_0 1
109#define BOOT_STATUS_STATE_1 2
110#define BOOT_STATUS_STATE_2 3
Christopher Collins92ea77f2016-12-12 15:59:26 -0800111
112/**
113 * End-of-image slot structure.
114 *
Christopher Collinsa1c12042019-05-23 14:00:28 -0700115 * 0 1 2 3
116 * 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
117 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
118 * ~ ~
119 * ~ Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3) ~
120 * ~ ~
121 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
122 * | Encryption key 0 (16 octets) [*] |
123 * | |
124 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
125 * | Encryption key 1 (16 octets) [*] |
126 * | |
127 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
128 * | Swap size (4 octets) |
129 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Vinczee2453472019-06-17 12:31:59 +0200130 * | Swap info | 0xff padding (7 octets) |
Christopher Collinsa1c12042019-05-23 14:00:28 -0700131 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
132 * | Copy done | 0xff padding (7 octets) |
133 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
134 * | Image OK | 0xff padding (7 octets) |
135 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
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
144extern const uint32_t boot_img_magic[4];
145
146struct boot_swap_state {
Christopher Collinsa1c12042019-05-23 14:00:28 -0700147 uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */
148 uint8_t swap_type; /* One of the BOOT_SWAP_TYPE_[...] values. */
149 uint8_t copy_done; /* One of the BOOT_FLAG_[...] values. */
150 uint8_t image_ok; /* One of the BOOT_FLAG_[...] values. */
David Vinczee2453472019-06-17 12:31:59 +0200151 uint8_t image_num; /* Boot status belongs to this image */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800152};
153
David Vinczeb75c12a2019-03-22 14:58:33 +0100154#ifdef MCUBOOT_IMAGE_NUMBER
155#define BOOT_IMAGE_NUMBER MCUBOOT_IMAGE_NUMBER
156#else
157#define BOOT_IMAGE_NUMBER 1
158#endif
159
Fabio Utzigabec0732019-07-31 08:40:22 -0300160_Static_assert(BOOT_IMAGE_NUMBER > 0, "Invalid value for BOOT_IMAGE_NUMBER");
161
Marti Bolivarf9bfddd2018-04-24 14:28:33 -0400162#define BOOT_MAX_IMG_SECTORS MCUBOOT_MAX_IMG_SECTORS
Fabio Utziga1fae672018-03-30 10:52:38 -0300163
164/*
David Vinczee2453472019-06-17 12:31:59 +0200165 * Extract the swap type and image number from image trailers's swap_info
166 * filed.
167 */
168#define BOOT_GET_SWAP_TYPE(swap_info) ((swap_info) & 0x0F)
169#define BOOT_GET_IMAGE_NUM(swap_info) ((swap_info) >> 4)
170
171/* Construct the swap_info field from swap type and image number */
172#define BOOT_SET_SWAP_INFO(swap_info, image, type) { \
173 assert((image) < 0xF); \
174 assert((type) < 0xF); \
175 (swap_info) = (image) << 4 \
176 | (type); \
177 }
178
179/*
Fabio Utziga1fae672018-03-30 10:52:38 -0300180 * The current flashmap API does not check the amount of space allocated when
181 * loading sector data from the flash device, allowing for smaller counts here
182 * would most surely incur in overruns.
183 *
184 * TODO: make flashmap API receive the current sector array size.
185 */
186#if BOOT_MAX_IMG_SECTORS < 32
187#error "Too few sectors, please increase BOOT_MAX_IMG_SECTORS to at least 32"
188#endif
189
Fabio Utzig74aef312019-11-28 11:05:34 -0300190#if MCUBOOT_SWAP_USING_MOVE
191#define BOOT_STATUS_MOVE_STATE_COUNT 1
192#define BOOT_STATUS_SWAP_STATE_COUNT 2
193#define BOOT_STATUS_STATE_COUNT (BOOT_STATUS_MOVE_STATE_COUNT + BOOT_STATUS_SWAP_STATE_COUNT)
194#else
David Vincze2d736ad2019-02-18 11:50:22 +0100195#define BOOT_STATUS_STATE_COUNT 3
Fabio Utzig74aef312019-11-28 11:05:34 -0300196#endif
197
198/** Maximum number of image sectors supported by the bootloader. */
David Vincze2d736ad2019-02-18 11:50:22 +0100199#define BOOT_STATUS_MAX_ENTRIES BOOT_MAX_IMG_SECTORS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800200
David Vincze2d736ad2019-02-18 11:50:22 +0100201#define BOOT_PRIMARY_SLOT 0
202#define BOOT_SECONDARY_SLOT 1
Christopher Collins92ea77f2016-12-12 15:59:26 -0800203
David Vincze2d736ad2019-02-18 11:50:22 +0100204#define BOOT_STATUS_SOURCE_NONE 0
205#define BOOT_STATUS_SOURCE_SCRATCH 1
206#define BOOT_STATUS_SOURCE_PRIMARY_SLOT 2
207
David Brown2b8a6952019-10-01 16:14:53 -0600208#define BOOT_MAGIC_SZ (sizeof boot_img_magic)
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300209
Marti Bolivarc50926f2017-06-14 09:35:40 -0400210/**
211 * Compatibility shim for flash sector type.
212 *
213 * This can be deleted when flash_area_to_sectors() is removed.
214 */
215#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
216typedef struct flash_sector boot_sector_t;
217#else
218typedef struct flash_area boot_sector_t;
219#endif
220
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400221/** Private state maintained during boot. */
222struct boot_loader_state {
223 struct {
224 struct image_header hdr;
Marti Bolivarc0b47912017-06-13 17:18:09 -0400225 const struct flash_area *area;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400226 boot_sector_t *sectors;
Marti Bolivar84898652017-06-13 17:20:22 -0400227 size_t num_sectors;
David Vinczeba3bd602019-06-17 16:01:43 +0200228 } imgs[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS];
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400229
Fabio Utzig12d59162019-11-28 10:01:59 -0300230#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200231 struct {
232 const struct flash_area *area;
233 boot_sector_t *sectors;
234 size_t num_sectors;
235 } scratch;
Fabio Utzig12d59162019-11-28 10:01:59 -0300236#endif
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400237
David Vinczeba3bd602019-06-17 16:01:43 +0200238 uint8_t swap_type[BOOT_IMAGE_NUMBER];
David Brownab449182019-11-15 09:32:52 -0700239 uint32_t write_sz;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300240
241#if defined(MCUBOOT_ENC_IMAGES)
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300242 struct enc_key_data enc[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS];
Fabio Utzig10ee6482019-08-01 12:04:52 -0300243#endif
244
Fabio Utzigb0f04732019-07-31 09:49:19 -0300245#if (BOOT_IMAGE_NUMBER > 1)
246 uint8_t curr_img_idx;
247#endif
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400248};
249
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200250int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
251 size_t slen, uint8_t key_id);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800252
Christopher Collinsa1c12042019-05-23 14:00:28 -0700253int boot_magic_compatible_check(uint8_t tbl_val, uint8_t val);
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300254uint32_t boot_status_sz(uint32_t min_write_sz);
David Brownab449182019-11-15 09:32:52 -0700255uint32_t boot_trailer_sz(uint32_t min_write_sz);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300256int boot_status_entries(int image_index, const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800257uint32_t boot_status_off(const struct flash_area *fap);
David Vinczee2453472019-06-17 12:31:59 +0200258uint32_t boot_swap_info_off(const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800259int boot_read_swap_state(const struct flash_area *fap,
260 struct boot_swap_state *state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300261int boot_read_swap_state_by_id(int flash_area_id,
262 struct boot_swap_state *state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800263int boot_write_magic(const struct flash_area *fap);
Fabio Utzig12d59162019-11-28 10:01:59 -0300264int boot_write_status(const struct boot_loader_state *state, struct boot_status *bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800265int boot_write_copy_done(const struct flash_area *fap);
266int boot_write_image_ok(const struct flash_area *fap);
David Vinczee2453472019-06-17 12:31:59 +0200267int boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
268 uint8_t image_num);
Fabio Utzig46490722017-09-04 15:34:32 -0300269int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300270int boot_read_swap_size(int image_index, uint32_t *swap_size);
Fabio Utzig12d59162019-11-28 10:01:59 -0300271int boot_slots_compatible(struct boot_loader_state *state);
272uint32_t boot_status_internal_off(const struct boot_status *bs, int elem_sz);
273int boot_read_image_header(struct boot_loader_state *state, int slot,
274 struct image_header *out_hdr, struct boot_status *bs);
275int boot_copy_region(struct boot_loader_state *state,
276 const struct flash_area *fap_src,
277 const struct flash_area *fap_dst,
278 uint32_t off_src, uint32_t off_dst, uint32_t sz);
279int boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz);
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,
283 const uint8_t *enckey);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300284int boot_read_enc_key(int image_index, uint8_t slot, uint8_t *enckey);
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/**
288 * Safe (non-overflowing) uint32_t addition. Returns true, and stores
289 * the result in *dest if it can be done without overflow. Otherwise,
290 * returns false.
291 */
292static inline bool boot_u32_safe_add(uint32_t *dest, uint32_t a, uint32_t b)
293{
294 /*
295 * "a + b <= UINT32_MAX", subtract 'b' from both sides to avoid
296 * the overflow.
297 */
298 if (a > UINT32_MAX - b) {
299 return false;
300 } else {
301 *dest = a + b;
302 return true;
303 }
304}
305
306/**
307 * Safe (non-overflowing) uint16_t addition. Returns true, and stores
308 * the result in *dest if it can be done without overflow. Otherwise,
309 * returns false.
310 */
311static inline bool boot_u16_safe_add(uint16_t *dest, uint16_t a, uint16_t b)
312{
313 uint32_t tmp = a + b;
314 if (tmp > UINT16_MAX) {
315 return false;
316 } else {
317 *dest = tmp;
318 return true;
319 }
320}
321
Marti Bolivarf804f622017-06-12 15:41:48 -0400322/*
323 * Accessors for the contents of struct boot_loader_state.
324 */
325
Marti Bolivarc0b47912017-06-13 17:18:09 -0400326/* These are macros so they can be used as lvalues. */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300327#if (BOOT_IMAGE_NUMBER > 1)
328#define BOOT_CURR_IMG(state) ((state)->curr_img_idx)
329#else
330#define BOOT_CURR_IMG(state) 0
Fabio Utzigbc077932019-08-26 11:16:34 -0300331#endif
332#ifdef MCUBOOT_ENC_IMAGES
333#define BOOT_CURR_ENC(state) ((state)->enc[BOOT_CURR_IMG(state)])
334#else
335#define BOOT_CURR_ENC(state) NULL
Fabio Utzigb0f04732019-07-31 09:49:19 -0300336#endif
337#define BOOT_IMG(state, slot) ((state)->imgs[BOOT_CURR_IMG(state)][(slot)])
David Vinczeba3bd602019-06-17 16:01:43 +0200338#define BOOT_IMG_AREA(state, slot) (BOOT_IMG(state, slot).area)
Marti Bolivare10a7392017-06-14 16:20:07 -0400339#define BOOT_WRITE_SZ(state) ((state)->write_sz)
Fabio Utzigb0f04732019-07-31 09:49:19 -0300340#define BOOT_SWAP_TYPE(state) ((state)->swap_type[BOOT_CURR_IMG(state)])
Fabio Utzigc9621352019-08-08 12:15:51 -0300341#define BOOT_TLV_OFF(hdr) ((hdr)->ih_hdr_size + (hdr)->ih_img_size)
Marti Bolivarc0b47912017-06-13 17:18:09 -0400342
Fabio Utzige575b0b2019-09-11 12:34:23 -0300343#define BOOT_IS_UPGRADE(swap_type) \
344 (((swap_type) == BOOT_SWAP_TYPE_TEST) || \
345 ((swap_type) == BOOT_SWAP_TYPE_REVERT) || \
346 ((swap_type) == BOOT_SWAP_TYPE_PERM))
347
Marti Bolivarf804f622017-06-12 15:41:48 -0400348static inline struct image_header*
349boot_img_hdr(struct boot_loader_state *state, size_t slot)
350{
David Vinczeba3bd602019-06-17 16:01:43 +0200351 return &BOOT_IMG(state, slot).hdr;
Marti Bolivarf804f622017-06-12 15:41:48 -0400352}
353
Marti Bolivard3269fd2017-06-12 16:31:12 -0400354static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300355boot_img_num_sectors(const struct boot_loader_state *state, size_t slot)
Marti Bolivard3269fd2017-06-12 16:31:12 -0400356{
David Vinczeba3bd602019-06-17 16:01:43 +0200357 return BOOT_IMG(state, slot).num_sectors;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400358}
359
Marti Bolivar135b8f62017-06-13 17:22:13 -0400360/*
361 * Offset of the slot from the beginning of the flash device.
362 */
363static inline uint32_t
364boot_img_slot_off(struct boot_loader_state *state, size_t slot)
365{
David Vinczeba3bd602019-06-17 16:01:43 +0200366 return BOOT_IMG(state, slot).area->fa_off;
Marti Bolivar135b8f62017-06-13 17:22:13 -0400367}
368
Marti Bolivarc50926f2017-06-14 09:35:40 -0400369#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
370
Marti Bolivard3269fd2017-06-12 16:31:12 -0400371static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300372boot_img_sector_size(const struct boot_loader_state *state,
Marti Bolivard3269fd2017-06-12 16:31:12 -0400373 size_t slot, size_t sector)
374{
David Vinczeba3bd602019-06-17 16:01:43 +0200375 return BOOT_IMG(state, slot).sectors[sector].fa_size;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400376}
377
Marti Bolivarea088872017-06-12 17:10:49 -0400378/*
379 * Offset of the sector from the beginning of the image, NOT the flash
380 * device.
381 */
382static inline uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300383boot_img_sector_off(const struct boot_loader_state *state, size_t slot,
Marti Bolivarea088872017-06-12 17:10:49 -0400384 size_t sector)
385{
David Vinczeba3bd602019-06-17 16:01:43 +0200386 return BOOT_IMG(state, slot).sectors[sector].fa_off -
387 BOOT_IMG(state, slot).sectors[0].fa_off;
Marti Bolivarea088872017-06-12 17:10:49 -0400388}
389
Marti Bolivarc50926f2017-06-14 09:35:40 -0400390#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
391
392static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300393boot_img_sector_size(const struct boot_loader_state *state,
Marti Bolivarc50926f2017-06-14 09:35:40 -0400394 size_t slot, size_t sector)
395{
David Vinczeba3bd602019-06-17 16:01:43 +0200396 return BOOT_IMG(state, slot).sectors[sector].fs_size;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400397}
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 Bolivarc50926f2017-06-14 09:35:40 -0400401 size_t sector)
402{
David Vinczeba3bd602019-06-17 16:01:43 +0200403 return BOOT_IMG(state, slot).sectors[sector].fs_off -
404 BOOT_IMG(state, slot).sectors[0].fs_off;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400405}
406
Marti Bolivarc50926f2017-06-14 09:35:40 -0400407#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
408
Christopher Collins92ea77f2016-12-12 15:59:26 -0800409#ifdef __cplusplus
410}
411#endif
412
413#endif