blob: 69aa6caa143fd60315cc8998737c512e271d090c [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
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
Marti Bolivar248da082018-04-24 15:11:39 -040050#ifdef MCUBOOT_HAVE_ASSERT_H
51#include "mcuboot_config/mcuboot_assert.h"
Fabio Utzig57c40f72017-12-12 21:48:30 -020052#else
Blaž Hrastnikf62ea0c2020-06-12 13:30:53 +090053#include <assert.h>
Fabio Utzig57c40f72017-12-12 21:48:30 -020054#define ASSERT assert
55#endif
56
Christopher Collins92ea77f2016-12-12 15:59:26 -080057struct flash_area;
58
David Vinczee32483f2019-06-13 10:46:24 +020059#define BOOT_EFLASH 1
60#define BOOT_EFILE 2
61#define BOOT_EBADIMAGE 3
62#define BOOT_EBADVECT 4
63#define BOOT_EBADSTATUS 5
64#define BOOT_ENOMEM 6
65#define BOOT_EBADARGS 7
66#define BOOT_EBADVERSION 8
Christopher Collins92ea77f2016-12-12 15:59:26 -080067
68#define BOOT_TMPBUF_SZ 256
69
Fabio Utzig1e4284b2019-08-23 11:55:27 -030070/** Number of image slots in flash; currently limited to two. */
71#define BOOT_NUM_SLOTS 2
72
David Vinczee574f2d2020-07-10 11:42:03 +020073#if (defined(MCUBOOT_OVERWRITE_ONLY) + \
74 defined(MCUBOOT_SWAP_USING_MOVE) + \
Tamas Banfe031092020-09-10 17:32:39 +020075 defined(MCUBOOT_DIRECT_XIP) + \
76 defined(MCUBOOT_RAM_LOAD)) > 1
77#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 -030078#endif
79
David Vinczee574f2d2020-07-10 11:42:03 +020080#if !defined(MCUBOOT_OVERWRITE_ONLY) && \
81 !defined(MCUBOOT_SWAP_USING_MOVE) && \
Tamas Banfe031092020-09-10 17:32:39 +020082 !defined(MCUBOOT_DIRECT_XIP) && \
83 !defined(MCUBOOT_RAM_LOAD)
Fabio Utzig12d59162019-11-28 10:01:59 -030084#define MCUBOOT_SWAP_USING_SCRATCH 1
85#endif
86
Fabio Utzig74aef312019-11-28 11:05:34 -030087#define BOOT_STATUS_OP_MOVE 1
88#define BOOT_STATUS_OP_SWAP 2
89
Christopher Collins92ea77f2016-12-12 15:59:26 -080090/*
91 * Maintain state of copy progress.
92 */
93struct boot_status {
Fabio Utzig2473ac02017-05-02 12:45:02 -030094 uint32_t idx; /* Which area we're operating on */
95 uint8_t state; /* Which part of the swapping process are we at */
Fabio Utzig74aef312019-11-28 11:05:34 -030096 uint8_t op; /* What operation are we performing? */
Fabio Utzig2473ac02017-05-02 12:45:02 -030097 uint8_t use_scratch; /* Are status bytes ever written to scratch? */
Christopher Collinsa1c12042019-05-23 14:00:28 -070098 uint8_t swap_type; /* The type of swap in effect */
Fabio Utzig46490722017-09-04 15:34:32 -030099 uint32_t swap_size; /* Total size of swapped image */
Fabio Utzigba829042018-09-18 08:29:34 -0300100#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300101 uint8_t enckey[BOOT_NUM_SLOTS][BOOT_ENC_KEY_SIZE];
Fabio Utzig4741c452019-12-19 15:32:41 -0300102#if MCUBOOT_SWAP_SAVE_ENCTLV
103 uint8_t enctlv[BOOT_NUM_SLOTS][BOOT_ENC_TLV_ALIGN_SIZE];
104#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300105#endif
Fabio Utzigb86e6882019-12-10 09:51:17 -0300106 int source; /* Which slot contains swap status metadata */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800107};
108
Fabio Utzig39000012018-07-30 12:40:20 -0300109#define BOOT_STATUS_IDX_0 1
110
111#define BOOT_STATUS_STATE_0 1
112#define BOOT_STATUS_STATE_1 2
113#define BOOT_STATUS_STATE_2 3
Christopher Collins92ea77f2016-12-12 15:59:26 -0800114
115/**
116 * End-of-image slot structure.
117 *
Christopher Collinsa1c12042019-05-23 14:00:28 -0700118 * 0 1 2 3
119 * 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
120 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
121 * ~ ~
122 * ~ Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3) ~
123 * ~ ~
124 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
125 * | Encryption key 0 (16 octets) [*] |
126 * | |
127 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
128 * | Encryption key 1 (16 octets) [*] |
129 * | |
130 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
131 * | Swap size (4 octets) |
132 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Vinczee2453472019-06-17 12:31:59 +0200133 * | Swap info | 0xff padding (7 octets) |
Christopher Collinsa1c12042019-05-23 14:00:28 -0700134 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
135 * | Copy done | 0xff padding (7 octets) |
136 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137 * | Image OK | 0xff padding (7 octets) |
138 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139 * | MAGIC (16 octets) |
140 * | |
141 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
142 *
143 * [*]: Only present if the encryption option is enabled
144 * (`MCUBOOT_ENC_IMAGES`).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800145 */
146
147extern const uint32_t boot_img_magic[4];
148
David Vinczeb75c12a2019-03-22 14:58:33 +0100149#ifdef MCUBOOT_IMAGE_NUMBER
150#define BOOT_IMAGE_NUMBER MCUBOOT_IMAGE_NUMBER
151#else
152#define BOOT_IMAGE_NUMBER 1
153#endif
154
Fabio Utzigabec0732019-07-31 08:40:22 -0300155_Static_assert(BOOT_IMAGE_NUMBER > 0, "Invalid value for BOOT_IMAGE_NUMBER");
156
Tamas Banfe031092020-09-10 17:32:39 +0200157#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
158#define ARE_SLOTS_EQUIVALENT() 0
David Vinczee574f2d2020-07-10 11:42:03 +0200159#else
Tamas Banfe031092020-09-10 17:32:39 +0200160#define ARE_SLOTS_EQUIVALENT() 1
David Vinczee574f2d2020-07-10 11:42:03 +0200161
162#if (BOOT_IMAGE_NUMBER != 1)
Tamas Banfe031092020-09-10 17:32:39 +0200163#error "The MCUBOOT_DIRECT_XIP and MCUBOOT_RAM_LOAD mode only supports single-image boot (MCUBOOT_IMAGE_NUMBER=1)."
David Vinczee574f2d2020-07-10 11:42:03 +0200164#endif
165#ifdef MCUBOOT_ENC_IMAGES
Tamas Banfe031092020-09-10 17:32:39 +0200166#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 +0200167#endif
Tamas Banfe031092020-09-10 17:32:39 +0200168#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200169
Marti Bolivarf9bfddd2018-04-24 14:28:33 -0400170#define BOOT_MAX_IMG_SECTORS MCUBOOT_MAX_IMG_SECTORS
Fabio Utziga1fae672018-03-30 10:52:38 -0300171
172/*
David Vinczee2453472019-06-17 12:31:59 +0200173 * Extract the swap type and image number from image trailers's swap_info
174 * filed.
175 */
176#define BOOT_GET_SWAP_TYPE(swap_info) ((swap_info) & 0x0F)
177#define BOOT_GET_IMAGE_NUM(swap_info) ((swap_info) >> 4)
178
179/* Construct the swap_info field from swap type and image number */
180#define BOOT_SET_SWAP_INFO(swap_info, image, type) { \
181 assert((image) < 0xF); \
182 assert((type) < 0xF); \
183 (swap_info) = (image) << 4 \
184 | (type); \
185 }
186
David Vinczee574f2d2020-07-10 11:42:03 +0200187#define BOOT_LOG_IMAGE_INFO(slot, hdr) \
188 BOOT_LOG_INF("%-9s slot: version=%u.%u.%u+%u", \
189 ((slot) == BOOT_PRIMARY_SLOT) ? "Primary" : "Secondary", \
190 (hdr)->ih_ver.iv_major, \
191 (hdr)->ih_ver.iv_minor, \
192 (hdr)->ih_ver.iv_revision, \
193 (hdr)->ih_ver.iv_build_num)
194
David Vinczee2453472019-06-17 12:31:59 +0200195/*
Fabio Utziga1fae672018-03-30 10:52:38 -0300196 * The current flashmap API does not check the amount of space allocated when
197 * loading sector data from the flash device, allowing for smaller counts here
198 * would most surely incur in overruns.
199 *
200 * TODO: make flashmap API receive the current sector array size.
201 */
202#if BOOT_MAX_IMG_SECTORS < 32
203#error "Too few sectors, please increase BOOT_MAX_IMG_SECTORS to at least 32"
204#endif
205
Fabio Utzig74aef312019-11-28 11:05:34 -0300206#if MCUBOOT_SWAP_USING_MOVE
207#define BOOT_STATUS_MOVE_STATE_COUNT 1
208#define BOOT_STATUS_SWAP_STATE_COUNT 2
209#define BOOT_STATUS_STATE_COUNT (BOOT_STATUS_MOVE_STATE_COUNT + BOOT_STATUS_SWAP_STATE_COUNT)
210#else
David Vincze2d736ad2019-02-18 11:50:22 +0100211#define BOOT_STATUS_STATE_COUNT 3
Fabio Utzig74aef312019-11-28 11:05:34 -0300212#endif
213
214/** Maximum number of image sectors supported by the bootloader. */
David Vincze2d736ad2019-02-18 11:50:22 +0100215#define BOOT_STATUS_MAX_ENTRIES BOOT_MAX_IMG_SECTORS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800216
David Vincze2d736ad2019-02-18 11:50:22 +0100217#define BOOT_PRIMARY_SLOT 0
218#define BOOT_SECONDARY_SLOT 1
Christopher Collins92ea77f2016-12-12 15:59:26 -0800219
David Vincze2d736ad2019-02-18 11:50:22 +0100220#define BOOT_STATUS_SOURCE_NONE 0
221#define BOOT_STATUS_SOURCE_SCRATCH 1
222#define BOOT_STATUS_SOURCE_PRIMARY_SLOT 2
223
Marti Bolivarc50926f2017-06-14 09:35:40 -0400224/**
225 * Compatibility shim for flash sector type.
226 *
227 * This can be deleted when flash_area_to_sectors() is removed.
228 */
229#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
230typedef struct flash_sector boot_sector_t;
231#else
232typedef struct flash_area boot_sector_t;
233#endif
234
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400235/** Private state maintained during boot. */
236struct boot_loader_state {
237 struct {
238 struct image_header hdr;
Marti Bolivarc0b47912017-06-13 17:18:09 -0400239 const struct flash_area *area;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400240 boot_sector_t *sectors;
Marti Bolivar84898652017-06-13 17:20:22 -0400241 size_t num_sectors;
David Vinczeba3bd602019-06-17 16:01:43 +0200242 } imgs[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS];
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400243
Fabio Utzig12d59162019-11-28 10:01:59 -0300244#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200245 struct {
246 const struct flash_area *area;
247 boot_sector_t *sectors;
248 size_t num_sectors;
249 } scratch;
Fabio Utzig12d59162019-11-28 10:01:59 -0300250#endif
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400251
David Vinczeba3bd602019-06-17 16:01:43 +0200252 uint8_t swap_type[BOOT_IMAGE_NUMBER];
David Brownab449182019-11-15 09:32:52 -0700253 uint32_t write_sz;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300254
255#if defined(MCUBOOT_ENC_IMAGES)
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300256 struct enc_key_data enc[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS];
Fabio Utzig10ee6482019-08-01 12:04:52 -0300257#endif
258
Fabio Utzigb0f04732019-07-31 09:49:19 -0300259#if (BOOT_IMAGE_NUMBER > 1)
260 uint8_t curr_img_idx;
261#endif
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400262};
263
Raef Colese8fe6cf2020-05-26 13:07:40 +0100264fih_int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
265 size_t slen, uint8_t key_id);
266
267fih_int boot_fih_memequal(const void *s1, const void *s2, size_t n);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800268
Christopher Collinsa1c12042019-05-23 14:00:28 -0700269int boot_magic_compatible_check(uint8_t tbl_val, uint8_t val);
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300270uint32_t boot_status_sz(uint32_t min_write_sz);
David Brownab449182019-11-15 09:32:52 -0700271uint32_t boot_trailer_sz(uint32_t min_write_sz);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300272int boot_status_entries(int image_index, const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800273uint32_t boot_status_off(const struct flash_area *fap);
274int boot_read_swap_state(const struct flash_area *fap,
275 struct boot_swap_state *state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300276int boot_read_swap_state_by_id(int flash_area_id,
277 struct boot_swap_state *state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800278int boot_write_magic(const struct flash_area *fap);
Fabio Utzig12d59162019-11-28 10:01:59 -0300279int boot_write_status(const struct boot_loader_state *state, struct boot_status *bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800280int boot_write_copy_done(const struct flash_area *fap);
281int boot_write_image_ok(const struct flash_area *fap);
David Vinczee2453472019-06-17 12:31:59 +0200282int boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
283 uint8_t image_num);
Fabio Utzig46490722017-09-04 15:34:32 -0300284int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300285int boot_read_swap_size(int image_index, uint32_t *swap_size);
Fabio Utzig12d59162019-11-28 10:01:59 -0300286int boot_slots_compatible(struct boot_loader_state *state);
287uint32_t boot_status_internal_off(const struct boot_status *bs, int elem_sz);
288int boot_read_image_header(struct boot_loader_state *state, int slot,
289 struct image_header *out_hdr, struct boot_status *bs);
290int boot_copy_region(struct boot_loader_state *state,
291 const struct flash_area *fap_src,
292 const struct flash_area *fap_dst,
293 uint32_t off_src, uint32_t off_dst, uint32_t sz);
294int boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz);
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300295bool boot_status_is_reset(const struct boot_status *bs);
Fabio Utzig12d59162019-11-28 10:01:59 -0300296
Fabio Utzigba829042018-09-18 08:29:34 -0300297#ifdef MCUBOOT_ENC_IMAGES
298int boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
Fabio Utzig4741c452019-12-19 15:32:41 -0300299 const struct boot_status *bs);
300int boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300301#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800302
David Browneffb06e2019-10-10 14:45:32 -0600303/**
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300304 * Checks that a buffer is erased according to what the erase value for the
305 * flash device provided in `flash_area` is.
306 *
307 * @returns true if the buffer is erased; false if any of the bytes is not
308 * erased, or when buffer is NULL, or when len == 0.
309 */
310bool bootutil_buffer_is_erased(const struct flash_area *area,
311 const void *buffer, size_t len);
312
313/**
David Browneffb06e2019-10-10 14:45:32 -0600314 * Safe (non-overflowing) uint32_t addition. Returns true, and stores
315 * the result in *dest if it can be done without overflow. Otherwise,
316 * returns false.
317 */
318static inline bool boot_u32_safe_add(uint32_t *dest, uint32_t a, uint32_t b)
319{
320 /*
321 * "a + b <= UINT32_MAX", subtract 'b' from both sides to avoid
322 * the overflow.
323 */
324 if (a > UINT32_MAX - b) {
325 return false;
326 } else {
327 *dest = a + b;
328 return true;
329 }
330}
331
332/**
333 * Safe (non-overflowing) uint16_t addition. Returns true, and stores
334 * the result in *dest if it can be done without overflow. Otherwise,
335 * returns false.
336 */
337static inline bool boot_u16_safe_add(uint16_t *dest, uint16_t a, uint16_t b)
338{
339 uint32_t tmp = a + b;
340 if (tmp > UINT16_MAX) {
341 return false;
342 } else {
343 *dest = tmp;
344 return true;
345 }
346}
347
Marti Bolivarf804f622017-06-12 15:41:48 -0400348/*
349 * Accessors for the contents of struct boot_loader_state.
350 */
351
Marti Bolivarc0b47912017-06-13 17:18:09 -0400352/* These are macros so they can be used as lvalues. */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300353#if (BOOT_IMAGE_NUMBER > 1)
354#define BOOT_CURR_IMG(state) ((state)->curr_img_idx)
355#else
356#define BOOT_CURR_IMG(state) 0
Fabio Utzigbc077932019-08-26 11:16:34 -0300357#endif
358#ifdef MCUBOOT_ENC_IMAGES
359#define BOOT_CURR_ENC(state) ((state)->enc[BOOT_CURR_IMG(state)])
360#else
361#define BOOT_CURR_ENC(state) NULL
Fabio Utzigb0f04732019-07-31 09:49:19 -0300362#endif
363#define BOOT_IMG(state, slot) ((state)->imgs[BOOT_CURR_IMG(state)][(slot)])
David Vinczeba3bd602019-06-17 16:01:43 +0200364#define BOOT_IMG_AREA(state, slot) (BOOT_IMG(state, slot).area)
Marti Bolivare10a7392017-06-14 16:20:07 -0400365#define BOOT_WRITE_SZ(state) ((state)->write_sz)
Fabio Utzigb0f04732019-07-31 09:49:19 -0300366#define BOOT_SWAP_TYPE(state) ((state)->swap_type[BOOT_CURR_IMG(state)])
Fabio Utzigc9621352019-08-08 12:15:51 -0300367#define BOOT_TLV_OFF(hdr) ((hdr)->ih_hdr_size + (hdr)->ih_img_size)
Marti Bolivarc0b47912017-06-13 17:18:09 -0400368
Fabio Utzige575b0b2019-09-11 12:34:23 -0300369#define BOOT_IS_UPGRADE(swap_type) \
370 (((swap_type) == BOOT_SWAP_TYPE_TEST) || \
371 ((swap_type) == BOOT_SWAP_TYPE_REVERT) || \
372 ((swap_type) == BOOT_SWAP_TYPE_PERM))
373
Marti Bolivarf804f622017-06-12 15:41:48 -0400374static inline struct image_header*
375boot_img_hdr(struct boot_loader_state *state, size_t slot)
376{
David Vinczeba3bd602019-06-17 16:01:43 +0200377 return &BOOT_IMG(state, slot).hdr;
Marti Bolivarf804f622017-06-12 15:41:48 -0400378}
379
Marti Bolivard3269fd2017-06-12 16:31:12 -0400380static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300381boot_img_num_sectors(const struct boot_loader_state *state, size_t slot)
Marti Bolivard3269fd2017-06-12 16:31:12 -0400382{
David Vinczeba3bd602019-06-17 16:01:43 +0200383 return BOOT_IMG(state, slot).num_sectors;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400384}
385
Marti Bolivar135b8f62017-06-13 17:22:13 -0400386/*
387 * Offset of the slot from the beginning of the flash device.
388 */
389static inline uint32_t
390boot_img_slot_off(struct boot_loader_state *state, size_t slot)
391{
David Vinczeba3bd602019-06-17 16:01:43 +0200392 return BOOT_IMG(state, slot).area->fa_off;
Marti Bolivar135b8f62017-06-13 17:22:13 -0400393}
394
Marti Bolivarc50926f2017-06-14 09:35:40 -0400395#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
396
Marti Bolivard3269fd2017-06-12 16:31:12 -0400397static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300398boot_img_sector_size(const struct boot_loader_state *state,
Marti Bolivard3269fd2017-06-12 16:31:12 -0400399 size_t slot, size_t sector)
400{
David Vinczeba3bd602019-06-17 16:01:43 +0200401 return BOOT_IMG(state, slot).sectors[sector].fa_size;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400402}
403
Marti Bolivarea088872017-06-12 17:10:49 -0400404/*
405 * Offset of the sector from the beginning of the image, NOT the flash
406 * device.
407 */
408static inline uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300409boot_img_sector_off(const struct boot_loader_state *state, size_t slot,
Marti Bolivarea088872017-06-12 17:10:49 -0400410 size_t sector)
411{
David Vinczeba3bd602019-06-17 16:01:43 +0200412 return BOOT_IMG(state, slot).sectors[sector].fa_off -
413 BOOT_IMG(state, slot).sectors[0].fa_off;
Marti Bolivarea088872017-06-12 17:10:49 -0400414}
415
Marti Bolivarc50926f2017-06-14 09:35:40 -0400416#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
417
418static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300419boot_img_sector_size(const struct boot_loader_state *state,
Marti Bolivarc50926f2017-06-14 09:35:40 -0400420 size_t slot, size_t sector)
421{
David Vinczeba3bd602019-06-17 16:01:43 +0200422 return BOOT_IMG(state, slot).sectors[sector].fs_size;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400423}
424
425static inline uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300426boot_img_sector_off(const struct boot_loader_state *state, size_t slot,
Marti Bolivarc50926f2017-06-14 09:35:40 -0400427 size_t sector)
428{
David Vinczeba3bd602019-06-17 16:01:43 +0200429 return BOOT_IMG(state, slot).sectors[sector].fs_off -
430 BOOT_IMG(state, slot).sectors[0].fs_off;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400431}
432
Marti Bolivarc50926f2017-06-14 09:35:40 -0400433#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
434
Tamas Banfe031092020-09-10 17:32:39 +0200435#ifdef MCUBOOT_RAM_LOAD
436#define LOAD_IMAGE_DATA(hdr, fap, start, output, size) \
437 (memcpy((output),(void*)((hdr)->ih_load_addr + (start)), \
Tamas Bane4885a62020-11-12 13:50:07 +0000438 (size)), 0)
Tamas Banfe031092020-09-10 17:32:39 +0200439#else
440#define LOAD_IMAGE_DATA(hdr, fap, start, output, size) \
441 (flash_area_read((fap), (start), (output), (size)))
442#endif /* MCUBOOT_RAM_LOAD */
443
Christopher Collins92ea77f2016-12-12 15:59:26 -0800444#ifdef __cplusplus
445}
446#endif
447
448#endif