blob: 171b409c26381872307a41a33aaa2264be6ddc8f [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
David Vinczee574f2d2020-07-10 11:42:03 +02006 * 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
28#ifndef H_BOOTUTIL_PRIV_
29#define H_BOOTUTIL_PRIV_
30
Marti Bolivarcca28a92017-06-12 16:52:22 -040031#include "sysflash/sysflash.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020032
33#include <flash_map_backend/flash_map_backend.h>
34
Christopher Collins01dfbb62019-03-21 07:28:37 -070035#include "bootutil/bootutil.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080036#include "bootutil/image.h"
Marti Bolivarf91bca52018-04-12 12:40:46 -040037#include "mcuboot_config/mcuboot_config.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080038
Fabio Utzigba829042018-09-18 08:29:34 -030039#ifdef MCUBOOT_ENC_IMAGES
40#include "bootutil/enc_key.h"
41#endif
42
Christopher Collins92ea77f2016-12-12 15:59:26 -080043#ifdef __cplusplus
44extern "C" {
45#endif
46
Marti Bolivar248da082018-04-24 15:11:39 -040047#ifdef MCUBOOT_HAVE_ASSERT_H
48#include "mcuboot_config/mcuboot_assert.h"
Fabio Utzig57c40f72017-12-12 21:48:30 -020049#else
50#define ASSERT assert
51#endif
52
Christopher Collins92ea77f2016-12-12 15:59:26 -080053struct flash_area;
54
David Vinczee32483f2019-06-13 10:46:24 +020055#define BOOT_EFLASH 1
56#define BOOT_EFILE 2
57#define BOOT_EBADIMAGE 3
58#define BOOT_EBADVECT 4
59#define BOOT_EBADSTATUS 5
60#define BOOT_ENOMEM 6
61#define BOOT_EBADARGS 7
62#define BOOT_EBADVERSION 8
Christopher Collins92ea77f2016-12-12 15:59:26 -080063
64#define BOOT_TMPBUF_SZ 256
65
Fabio Utzig1e4284b2019-08-23 11:55:27 -030066/** Number of image slots in flash; currently limited to two. */
67#define BOOT_NUM_SLOTS 2
68
David Vinczee574f2d2020-07-10 11:42:03 +020069#if (defined(MCUBOOT_OVERWRITE_ONLY) + \
70 defined(MCUBOOT_SWAP_USING_MOVE) + \
71 defined(MCUBOOT_DIRECT_XIP)) > 1
72#error "Please enable only one of MCUBOOT_OVERWRITE_ONLY, MCUBOOT_SWAP_USING_MOVE or MCUBOOT_DIRECT_XIP"
Fabio Utzig74aef312019-11-28 11:05:34 -030073#endif
74
David Vinczee574f2d2020-07-10 11:42:03 +020075#if !defined(MCUBOOT_OVERWRITE_ONLY) && \
76 !defined(MCUBOOT_SWAP_USING_MOVE) && \
77 !defined(MCUBOOT_DIRECT_XIP)
Fabio Utzig12d59162019-11-28 10:01:59 -030078#define MCUBOOT_SWAP_USING_SCRATCH 1
79#endif
80
Fabio Utzig74aef312019-11-28 11:05:34 -030081#define BOOT_STATUS_OP_MOVE 1
82#define BOOT_STATUS_OP_SWAP 2
83
Christopher Collins92ea77f2016-12-12 15:59:26 -080084/*
85 * Maintain state of copy progress.
86 */
87struct boot_status {
Fabio Utzig2473ac02017-05-02 12:45:02 -030088 uint32_t idx; /* Which area we're operating on */
89 uint8_t state; /* Which part of the swapping process are we at */
Fabio Utzig74aef312019-11-28 11:05:34 -030090 uint8_t op; /* What operation are we performing? */
Fabio Utzig2473ac02017-05-02 12:45:02 -030091 uint8_t use_scratch; /* Are status bytes ever written to scratch? */
Christopher Collinsa1c12042019-05-23 14:00:28 -070092 uint8_t swap_type; /* The type of swap in effect */
Fabio Utzig46490722017-09-04 15:34:32 -030093 uint32_t swap_size; /* Total size of swapped image */
Fabio Utzigba829042018-09-18 08:29:34 -030094#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig1e4284b2019-08-23 11:55:27 -030095 uint8_t enckey[BOOT_NUM_SLOTS][BOOT_ENC_KEY_SIZE];
Fabio Utzig4741c452019-12-19 15:32:41 -030096#if MCUBOOT_SWAP_SAVE_ENCTLV
97 uint8_t enctlv[BOOT_NUM_SLOTS][BOOT_ENC_TLV_ALIGN_SIZE];
98#endif
Fabio Utzigba829042018-09-18 08:29:34 -030099#endif
Fabio Utzigb86e6882019-12-10 09:51:17 -0300100 int source; /* Which slot contains swap status metadata */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800101};
102
Fabio Utzig39000012018-07-30 12:40:20 -0300103#define BOOT_MAGIC_GOOD 1
104#define BOOT_MAGIC_BAD 2
105#define BOOT_MAGIC_UNSET 3
106#define BOOT_MAGIC_ANY 4 /* NOTE: control only, not dependent on sector */
Christopher Collinsa1c12042019-05-23 14:00:28 -0700107#define BOOT_MAGIC_NOTGOOD 5 /* NOTE: control only, not dependent on sector */
Fabio Utzig39000012018-07-30 12:40:20 -0300108
109/*
110 * NOTE: leave BOOT_FLAG_SET equal to one, this is written to flash!
111 */
112#define BOOT_FLAG_SET 1
113#define BOOT_FLAG_BAD 2
114#define BOOT_FLAG_UNSET 3
115#define BOOT_FLAG_ANY 4 /* NOTE: control only, not dependent on sector */
116
117#define BOOT_STATUS_IDX_0 1
118
119#define BOOT_STATUS_STATE_0 1
120#define BOOT_STATUS_STATE_1 2
121#define BOOT_STATUS_STATE_2 3
Christopher Collins92ea77f2016-12-12 15:59:26 -0800122
123/**
124 * End-of-image slot structure.
125 *
Christopher Collinsa1c12042019-05-23 14:00:28 -0700126 * 0 1 2 3
127 * 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
128 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
129 * ~ ~
130 * ~ Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3) ~
131 * ~ ~
132 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
133 * | Encryption key 0 (16 octets) [*] |
134 * | |
135 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
136 * | Encryption key 1 (16 octets) [*] |
137 * | |
138 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139 * | Swap size (4 octets) |
140 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Vinczee2453472019-06-17 12:31:59 +0200141 * | Swap info | 0xff padding (7 octets) |
Christopher Collinsa1c12042019-05-23 14:00:28 -0700142 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143 * | Copy done | 0xff padding (7 octets) |
144 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
145 * | Image OK | 0xff padding (7 octets) |
146 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
147 * | MAGIC (16 octets) |
148 * | |
149 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
150 *
151 * [*]: Only present if the encryption option is enabled
152 * (`MCUBOOT_ENC_IMAGES`).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800153 */
154
155extern const uint32_t boot_img_magic[4];
156
157struct boot_swap_state {
Christopher Collinsa1c12042019-05-23 14:00:28 -0700158 uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */
159 uint8_t swap_type; /* One of the BOOT_SWAP_TYPE_[...] values. */
160 uint8_t copy_done; /* One of the BOOT_FLAG_[...] values. */
161 uint8_t image_ok; /* One of the BOOT_FLAG_[...] values. */
David Vinczee2453472019-06-17 12:31:59 +0200162 uint8_t image_num; /* Boot status belongs to this image */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800163};
164
David Vinczeb75c12a2019-03-22 14:58:33 +0100165#ifdef MCUBOOT_IMAGE_NUMBER
166#define BOOT_IMAGE_NUMBER MCUBOOT_IMAGE_NUMBER
167#else
168#define BOOT_IMAGE_NUMBER 1
169#endif
170
Fabio Utzigabec0732019-07-31 08:40:22 -0300171_Static_assert(BOOT_IMAGE_NUMBER > 0, "Invalid value for BOOT_IMAGE_NUMBER");
172
David Vinczee574f2d2020-07-10 11:42:03 +0200173#if !defined(MCUBOOT_DIRECT_XIP)
174#define IS_IN_XIP_MODE() 0
175#else
176#define IS_IN_XIP_MODE() 1
177
178#if (BOOT_IMAGE_NUMBER != 1)
179#error "The MCUBOOT_DIRECT_XIP mode only supports single-image boot (MCUBOOT_IMAGE_NUMBER=1)."
180#endif
181#ifdef MCUBOOT_ENC_IMAGES
182#error "Image encryption (MCUBOOT_ENC_IMAGES) is not supported when MCUBOOT_DIRECT_XIP mode is selected."
183#endif
184#endif /* MCUBOOT_DIRECT_XIP */
185
Marti Bolivarf9bfddd2018-04-24 14:28:33 -0400186#define BOOT_MAX_IMG_SECTORS MCUBOOT_MAX_IMG_SECTORS
Fabio Utziga1fae672018-03-30 10:52:38 -0300187
188/*
David Vinczee2453472019-06-17 12:31:59 +0200189 * Extract the swap type and image number from image trailers's swap_info
190 * filed.
191 */
192#define BOOT_GET_SWAP_TYPE(swap_info) ((swap_info) & 0x0F)
193#define BOOT_GET_IMAGE_NUM(swap_info) ((swap_info) >> 4)
194
195/* Construct the swap_info field from swap type and image number */
196#define BOOT_SET_SWAP_INFO(swap_info, image, type) { \
197 assert((image) < 0xF); \
198 assert((type) < 0xF); \
199 (swap_info) = (image) << 4 \
200 | (type); \
201 }
202
David Vinczee574f2d2020-07-10 11:42:03 +0200203#define BOOT_LOG_IMAGE_INFO(slot, hdr) \
204 BOOT_LOG_INF("%-9s slot: version=%u.%u.%u+%u", \
205 ((slot) == BOOT_PRIMARY_SLOT) ? "Primary" : "Secondary", \
206 (hdr)->ih_ver.iv_major, \
207 (hdr)->ih_ver.iv_minor, \
208 (hdr)->ih_ver.iv_revision, \
209 (hdr)->ih_ver.iv_build_num)
210
David Vinczee2453472019-06-17 12:31:59 +0200211/*
Fabio Utziga1fae672018-03-30 10:52:38 -0300212 * The current flashmap API does not check the amount of space allocated when
213 * loading sector data from the flash device, allowing for smaller counts here
214 * would most surely incur in overruns.
215 *
216 * TODO: make flashmap API receive the current sector array size.
217 */
218#if BOOT_MAX_IMG_SECTORS < 32
219#error "Too few sectors, please increase BOOT_MAX_IMG_SECTORS to at least 32"
220#endif
221
Fabio Utzig74aef312019-11-28 11:05:34 -0300222#if MCUBOOT_SWAP_USING_MOVE
223#define BOOT_STATUS_MOVE_STATE_COUNT 1
224#define BOOT_STATUS_SWAP_STATE_COUNT 2
225#define BOOT_STATUS_STATE_COUNT (BOOT_STATUS_MOVE_STATE_COUNT + BOOT_STATUS_SWAP_STATE_COUNT)
226#else
David Vincze2d736ad2019-02-18 11:50:22 +0100227#define BOOT_STATUS_STATE_COUNT 3
Fabio Utzig74aef312019-11-28 11:05:34 -0300228#endif
229
230/** Maximum number of image sectors supported by the bootloader. */
David Vincze2d736ad2019-02-18 11:50:22 +0100231#define BOOT_STATUS_MAX_ENTRIES BOOT_MAX_IMG_SECTORS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800232
David Vincze2d736ad2019-02-18 11:50:22 +0100233#define BOOT_PRIMARY_SLOT 0
234#define BOOT_SECONDARY_SLOT 1
Christopher Collins92ea77f2016-12-12 15:59:26 -0800235
David Vincze2d736ad2019-02-18 11:50:22 +0100236#define BOOT_STATUS_SOURCE_NONE 0
237#define BOOT_STATUS_SOURCE_SCRATCH 1
238#define BOOT_STATUS_SOURCE_PRIMARY_SLOT 2
239
David Brown2b8a6952019-10-01 16:14:53 -0600240#define BOOT_MAGIC_SZ (sizeof boot_img_magic)
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300241
Marti Bolivarc50926f2017-06-14 09:35:40 -0400242/**
243 * Compatibility shim for flash sector type.
244 *
245 * This can be deleted when flash_area_to_sectors() is removed.
246 */
247#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
248typedef struct flash_sector boot_sector_t;
249#else
250typedef struct flash_area boot_sector_t;
251#endif
252
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400253/** Private state maintained during boot. */
254struct boot_loader_state {
255 struct {
256 struct image_header hdr;
Marti Bolivarc0b47912017-06-13 17:18:09 -0400257 const struct flash_area *area;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400258 boot_sector_t *sectors;
Marti Bolivar84898652017-06-13 17:20:22 -0400259 size_t num_sectors;
David Vinczeba3bd602019-06-17 16:01:43 +0200260 } imgs[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS];
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400261
Fabio Utzig12d59162019-11-28 10:01:59 -0300262#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200263 struct {
264 const struct flash_area *area;
265 boot_sector_t *sectors;
266 size_t num_sectors;
267 } scratch;
Fabio Utzig12d59162019-11-28 10:01:59 -0300268#endif
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400269
David Vinczeba3bd602019-06-17 16:01:43 +0200270 uint8_t swap_type[BOOT_IMAGE_NUMBER];
David Brownab449182019-11-15 09:32:52 -0700271 uint32_t write_sz;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300272
273#if defined(MCUBOOT_ENC_IMAGES)
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300274 struct enc_key_data enc[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS];
Fabio Utzig10ee6482019-08-01 12:04:52 -0300275#endif
276
Fabio Utzigb0f04732019-07-31 09:49:19 -0300277#if (BOOT_IMAGE_NUMBER > 1)
278 uint8_t curr_img_idx;
279#endif
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400280};
281
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200282int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
283 size_t slen, uint8_t key_id);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800284
Christopher Collinsa1c12042019-05-23 14:00:28 -0700285int boot_magic_compatible_check(uint8_t tbl_val, uint8_t val);
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300286uint32_t boot_status_sz(uint32_t min_write_sz);
David Brownab449182019-11-15 09:32:52 -0700287uint32_t boot_trailer_sz(uint32_t min_write_sz);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300288int boot_status_entries(int image_index, const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800289uint32_t boot_status_off(const struct flash_area *fap);
David Vinczee2453472019-06-17 12:31:59 +0200290uint32_t boot_swap_info_off(const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800291int boot_read_swap_state(const struct flash_area *fap,
292 struct boot_swap_state *state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300293int boot_read_swap_state_by_id(int flash_area_id,
294 struct boot_swap_state *state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800295int boot_write_magic(const struct flash_area *fap);
Fabio Utzig12d59162019-11-28 10:01:59 -0300296int boot_write_status(const struct boot_loader_state *state, struct boot_status *bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800297int boot_write_copy_done(const struct flash_area *fap);
298int boot_write_image_ok(const struct flash_area *fap);
David Vinczee2453472019-06-17 12:31:59 +0200299int boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
300 uint8_t image_num);
Fabio Utzig46490722017-09-04 15:34:32 -0300301int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300302int boot_read_swap_size(int image_index, uint32_t *swap_size);
Fabio Utzig12d59162019-11-28 10:01:59 -0300303int boot_slots_compatible(struct boot_loader_state *state);
304uint32_t boot_status_internal_off(const struct boot_status *bs, int elem_sz);
305int boot_read_image_header(struct boot_loader_state *state, int slot,
306 struct image_header *out_hdr, struct boot_status *bs);
307int boot_copy_region(struct boot_loader_state *state,
308 const struct flash_area *fap_src,
309 const struct flash_area *fap_dst,
310 uint32_t off_src, uint32_t off_dst, uint32_t sz);
311int boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz);
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300312bool boot_status_is_reset(const struct boot_status *bs);
Fabio Utzig12d59162019-11-28 10:01:59 -0300313
Fabio Utzigba829042018-09-18 08:29:34 -0300314#ifdef MCUBOOT_ENC_IMAGES
315int boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
Fabio Utzig4741c452019-12-19 15:32:41 -0300316 const struct boot_status *bs);
317int boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300318#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800319
David Browneffb06e2019-10-10 14:45:32 -0600320/**
321 * Safe (non-overflowing) uint32_t addition. Returns true, and stores
322 * the result in *dest if it can be done without overflow. Otherwise,
323 * returns false.
324 */
325static inline bool boot_u32_safe_add(uint32_t *dest, uint32_t a, uint32_t b)
326{
327 /*
328 * "a + b <= UINT32_MAX", subtract 'b' from both sides to avoid
329 * the overflow.
330 */
331 if (a > UINT32_MAX - b) {
332 return false;
333 } else {
334 *dest = a + b;
335 return true;
336 }
337}
338
339/**
340 * Safe (non-overflowing) uint16_t addition. Returns true, and stores
341 * the result in *dest if it can be done without overflow. Otherwise,
342 * returns false.
343 */
344static inline bool boot_u16_safe_add(uint16_t *dest, uint16_t a, uint16_t b)
345{
346 uint32_t tmp = a + b;
347 if (tmp > UINT16_MAX) {
348 return false;
349 } else {
350 *dest = tmp;
351 return true;
352 }
353}
354
Marti Bolivarf804f622017-06-12 15:41:48 -0400355/*
356 * Accessors for the contents of struct boot_loader_state.
357 */
358
Marti Bolivarc0b47912017-06-13 17:18:09 -0400359/* These are macros so they can be used as lvalues. */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300360#if (BOOT_IMAGE_NUMBER > 1)
361#define BOOT_CURR_IMG(state) ((state)->curr_img_idx)
362#else
363#define BOOT_CURR_IMG(state) 0
Fabio Utzigbc077932019-08-26 11:16:34 -0300364#endif
365#ifdef MCUBOOT_ENC_IMAGES
366#define BOOT_CURR_ENC(state) ((state)->enc[BOOT_CURR_IMG(state)])
367#else
368#define BOOT_CURR_ENC(state) NULL
Fabio Utzigb0f04732019-07-31 09:49:19 -0300369#endif
370#define BOOT_IMG(state, slot) ((state)->imgs[BOOT_CURR_IMG(state)][(slot)])
David Vinczeba3bd602019-06-17 16:01:43 +0200371#define BOOT_IMG_AREA(state, slot) (BOOT_IMG(state, slot).area)
Marti Bolivare10a7392017-06-14 16:20:07 -0400372#define BOOT_WRITE_SZ(state) ((state)->write_sz)
Fabio Utzigb0f04732019-07-31 09:49:19 -0300373#define BOOT_SWAP_TYPE(state) ((state)->swap_type[BOOT_CURR_IMG(state)])
Fabio Utzigc9621352019-08-08 12:15:51 -0300374#define BOOT_TLV_OFF(hdr) ((hdr)->ih_hdr_size + (hdr)->ih_img_size)
Marti Bolivarc0b47912017-06-13 17:18:09 -0400375
Fabio Utzige575b0b2019-09-11 12:34:23 -0300376#define BOOT_IS_UPGRADE(swap_type) \
377 (((swap_type) == BOOT_SWAP_TYPE_TEST) || \
378 ((swap_type) == BOOT_SWAP_TYPE_REVERT) || \
379 ((swap_type) == BOOT_SWAP_TYPE_PERM))
380
Marti Bolivarf804f622017-06-12 15:41:48 -0400381static inline struct image_header*
382boot_img_hdr(struct boot_loader_state *state, size_t slot)
383{
David Vinczeba3bd602019-06-17 16:01:43 +0200384 return &BOOT_IMG(state, slot).hdr;
Marti Bolivarf804f622017-06-12 15:41:48 -0400385}
386
Marti Bolivard3269fd2017-06-12 16:31:12 -0400387static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300388boot_img_num_sectors(const struct boot_loader_state *state, size_t slot)
Marti Bolivard3269fd2017-06-12 16:31:12 -0400389{
David Vinczeba3bd602019-06-17 16:01:43 +0200390 return BOOT_IMG(state, slot).num_sectors;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400391}
392
Marti Bolivar135b8f62017-06-13 17:22:13 -0400393/*
394 * Offset of the slot from the beginning of the flash device.
395 */
396static inline uint32_t
397boot_img_slot_off(struct boot_loader_state *state, size_t slot)
398{
David Vinczeba3bd602019-06-17 16:01:43 +0200399 return BOOT_IMG(state, slot).area->fa_off;
Marti Bolivar135b8f62017-06-13 17:22:13 -0400400}
401
Marti Bolivarc50926f2017-06-14 09:35:40 -0400402#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
403
Marti Bolivard3269fd2017-06-12 16:31:12 -0400404static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300405boot_img_sector_size(const struct boot_loader_state *state,
Marti Bolivard3269fd2017-06-12 16:31:12 -0400406 size_t slot, size_t sector)
407{
David Vinczeba3bd602019-06-17 16:01:43 +0200408 return BOOT_IMG(state, slot).sectors[sector].fa_size;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400409}
410
Marti Bolivarea088872017-06-12 17:10:49 -0400411/*
412 * Offset of the sector from the beginning of the image, NOT the flash
413 * device.
414 */
415static inline uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300416boot_img_sector_off(const struct boot_loader_state *state, size_t slot,
Marti Bolivarea088872017-06-12 17:10:49 -0400417 size_t sector)
418{
David Vinczeba3bd602019-06-17 16:01:43 +0200419 return BOOT_IMG(state, slot).sectors[sector].fa_off -
420 BOOT_IMG(state, slot).sectors[0].fa_off;
Marti Bolivarea088872017-06-12 17:10:49 -0400421}
422
Marti Bolivarc50926f2017-06-14 09:35:40 -0400423#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
424
425static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300426boot_img_sector_size(const struct boot_loader_state *state,
Marti Bolivarc50926f2017-06-14 09:35:40 -0400427 size_t slot, size_t sector)
428{
David Vinczeba3bd602019-06-17 16:01:43 +0200429 return BOOT_IMG(state, slot).sectors[sector].fs_size;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400430}
431
432static inline uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300433boot_img_sector_off(const struct boot_loader_state *state, size_t slot,
Marti Bolivarc50926f2017-06-14 09:35:40 -0400434 size_t sector)
435{
David Vinczeba3bd602019-06-17 16:01:43 +0200436 return BOOT_IMG(state, slot).sectors[sector].fs_off -
437 BOOT_IMG(state, slot).sectors[0].fs_off;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400438}
439
Marti Bolivarc50926f2017-06-14 09:35:40 -0400440#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
441
Christopher Collins92ea77f2016-12-12 15:59:26 -0800442#ifdef __cplusplus
443}
444#endif
445
446#endif