blob: 180cd5c716e28b019beb38c327a985c772c0f788 [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"
Marti Bolivarf91bca52018-04-12 12:40:46 -040039#include "mcuboot_config/mcuboot_config.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080040
Fabio Utzigba829042018-09-18 08:29:34 -030041#ifdef MCUBOOT_ENC_IMAGES
42#include "bootutil/enc_key.h"
43#endif
44
Christopher Collins92ea77f2016-12-12 15:59:26 -080045#ifdef __cplusplus
46extern "C" {
47#endif
48
Marti Bolivar248da082018-04-24 15:11:39 -040049#ifdef MCUBOOT_HAVE_ASSERT_H
50#include "mcuboot_config/mcuboot_assert.h"
Fabio Utzig57c40f72017-12-12 21:48:30 -020051#else
Blaž Hrastnikf62ea0c2020-06-12 13:30:53 +090052#include <assert.h>
Fabio Utzig57c40f72017-12-12 21:48:30 -020053#define ASSERT assert
54#endif
55
Christopher Collins92ea77f2016-12-12 15:59:26 -080056struct flash_area;
57
David Vinczee32483f2019-06-13 10:46:24 +020058#define BOOT_EFLASH 1
59#define BOOT_EFILE 2
60#define BOOT_EBADIMAGE 3
61#define BOOT_EBADVECT 4
62#define BOOT_EBADSTATUS 5
63#define BOOT_ENOMEM 6
64#define BOOT_EBADARGS 7
65#define BOOT_EBADVERSION 8
Christopher Collins92ea77f2016-12-12 15:59:26 -080066
67#define BOOT_TMPBUF_SZ 256
68
Fabio Utzig1e4284b2019-08-23 11:55:27 -030069/** Number of image slots in flash; currently limited to two. */
70#define BOOT_NUM_SLOTS 2
71
David Vinczee574f2d2020-07-10 11:42:03 +020072#if (defined(MCUBOOT_OVERWRITE_ONLY) + \
73 defined(MCUBOOT_SWAP_USING_MOVE) + \
Tamas Banfe031092020-09-10 17:32:39 +020074 defined(MCUBOOT_DIRECT_XIP) + \
75 defined(MCUBOOT_RAM_LOAD)) > 1
76#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 -030077#endif
78
David Vinczee574f2d2020-07-10 11:42:03 +020079#if !defined(MCUBOOT_OVERWRITE_ONLY) && \
80 !defined(MCUBOOT_SWAP_USING_MOVE) && \
Tamas Banfe031092020-09-10 17:32:39 +020081 !defined(MCUBOOT_DIRECT_XIP) && \
82 !defined(MCUBOOT_RAM_LOAD)
Fabio Utzig12d59162019-11-28 10:01:59 -030083#define MCUBOOT_SWAP_USING_SCRATCH 1
84#endif
85
Fabio Utzig74aef312019-11-28 11:05:34 -030086#define BOOT_STATUS_OP_MOVE 1
87#define BOOT_STATUS_OP_SWAP 2
88
Christopher Collins92ea77f2016-12-12 15:59:26 -080089/*
90 * Maintain state of copy progress.
91 */
92struct boot_status {
Fabio Utzig2473ac02017-05-02 12:45:02 -030093 uint32_t idx; /* Which area we're operating on */
94 uint8_t state; /* Which part of the swapping process are we at */
Fabio Utzig74aef312019-11-28 11:05:34 -030095 uint8_t op; /* What operation are we performing? */
Fabio Utzig2473ac02017-05-02 12:45:02 -030096 uint8_t use_scratch; /* Are status bytes ever written to scratch? */
Christopher Collinsa1c12042019-05-23 14:00:28 -070097 uint8_t swap_type; /* The type of swap in effect */
Fabio Utzig46490722017-09-04 15:34:32 -030098 uint32_t swap_size; /* Total size of swapped image */
Fabio Utzigba829042018-09-18 08:29:34 -030099#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300100 uint8_t enckey[BOOT_NUM_SLOTS][BOOT_ENC_KEY_SIZE];
Fabio Utzig4741c452019-12-19 15:32:41 -0300101#if MCUBOOT_SWAP_SAVE_ENCTLV
102 uint8_t enctlv[BOOT_NUM_SLOTS][BOOT_ENC_TLV_ALIGN_SIZE];
103#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300104#endif
Fabio Utzigb86e6882019-12-10 09:51:17 -0300105 int source; /* Which slot contains swap status metadata */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800106};
107
Fabio Utzig39000012018-07-30 12:40:20 -0300108#define BOOT_MAGIC_GOOD 1
109#define BOOT_MAGIC_BAD 2
110#define BOOT_MAGIC_UNSET 3
111#define BOOT_MAGIC_ANY 4 /* NOTE: control only, not dependent on sector */
Christopher Collinsa1c12042019-05-23 14:00:28 -0700112#define BOOT_MAGIC_NOTGOOD 5 /* NOTE: control only, not dependent on sector */
Fabio Utzig39000012018-07-30 12:40:20 -0300113
114/*
115 * NOTE: leave BOOT_FLAG_SET equal to one, this is written to flash!
116 */
117#define BOOT_FLAG_SET 1
118#define BOOT_FLAG_BAD 2
119#define BOOT_FLAG_UNSET 3
120#define BOOT_FLAG_ANY 4 /* NOTE: control only, not dependent on sector */
121
122#define BOOT_STATUS_IDX_0 1
123
124#define BOOT_STATUS_STATE_0 1
125#define BOOT_STATUS_STATE_1 2
126#define BOOT_STATUS_STATE_2 3
Christopher Collins92ea77f2016-12-12 15:59:26 -0800127
128/**
129 * End-of-image slot structure.
130 *
Christopher Collinsa1c12042019-05-23 14:00:28 -0700131 * 0 1 2 3
132 * 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
133 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
134 * ~ ~
135 * ~ Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3) ~
136 * ~ ~
137 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
138 * | Encryption key 0 (16 octets) [*] |
139 * | |
140 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
141 * | Encryption key 1 (16 octets) [*] |
142 * | |
143 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
144 * | Swap size (4 octets) |
145 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Vinczee2453472019-06-17 12:31:59 +0200146 * | Swap info | 0xff padding (7 octets) |
Christopher Collinsa1c12042019-05-23 14:00:28 -0700147 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
148 * | Copy done | 0xff padding (7 octets) |
149 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
150 * | Image OK | 0xff padding (7 octets) |
151 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
152 * | MAGIC (16 octets) |
153 * | |
154 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
155 *
156 * [*]: Only present if the encryption option is enabled
157 * (`MCUBOOT_ENC_IMAGES`).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800158 */
159
160extern const uint32_t boot_img_magic[4];
161
162struct boot_swap_state {
Christopher Collinsa1c12042019-05-23 14:00:28 -0700163 uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */
164 uint8_t swap_type; /* One of the BOOT_SWAP_TYPE_[...] values. */
165 uint8_t copy_done; /* One of the BOOT_FLAG_[...] values. */
166 uint8_t image_ok; /* One of the BOOT_FLAG_[...] values. */
David Vinczee2453472019-06-17 12:31:59 +0200167 uint8_t image_num; /* Boot status belongs to this image */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800168};
169
David Vinczeb75c12a2019-03-22 14:58:33 +0100170#ifdef MCUBOOT_IMAGE_NUMBER
171#define BOOT_IMAGE_NUMBER MCUBOOT_IMAGE_NUMBER
172#else
173#define BOOT_IMAGE_NUMBER 1
174#endif
175
Fabio Utzigabec0732019-07-31 08:40:22 -0300176_Static_assert(BOOT_IMAGE_NUMBER > 0, "Invalid value for BOOT_IMAGE_NUMBER");
177
Tamas Banfe031092020-09-10 17:32:39 +0200178#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
179#define ARE_SLOTS_EQUIVALENT() 0
David Vinczee574f2d2020-07-10 11:42:03 +0200180#else
Tamas Banfe031092020-09-10 17:32:39 +0200181#define ARE_SLOTS_EQUIVALENT() 1
David Vinczee574f2d2020-07-10 11:42:03 +0200182
183#if (BOOT_IMAGE_NUMBER != 1)
Tamas Banfe031092020-09-10 17:32:39 +0200184#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 +0200185#endif
186#ifdef MCUBOOT_ENC_IMAGES
Tamas Banfe031092020-09-10 17:32:39 +0200187#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 +0200188#endif
Tamas Banfe031092020-09-10 17:32:39 +0200189#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200190
Marti Bolivarf9bfddd2018-04-24 14:28:33 -0400191#define BOOT_MAX_IMG_SECTORS MCUBOOT_MAX_IMG_SECTORS
Fabio Utziga1fae672018-03-30 10:52:38 -0300192
193/*
David Vinczee2453472019-06-17 12:31:59 +0200194 * Extract the swap type and image number from image trailers's swap_info
195 * filed.
196 */
197#define BOOT_GET_SWAP_TYPE(swap_info) ((swap_info) & 0x0F)
198#define BOOT_GET_IMAGE_NUM(swap_info) ((swap_info) >> 4)
199
200/* Construct the swap_info field from swap type and image number */
201#define BOOT_SET_SWAP_INFO(swap_info, image, type) { \
202 assert((image) < 0xF); \
203 assert((type) < 0xF); \
204 (swap_info) = (image) << 4 \
205 | (type); \
206 }
207
David Vinczee574f2d2020-07-10 11:42:03 +0200208#define BOOT_LOG_IMAGE_INFO(slot, hdr) \
209 BOOT_LOG_INF("%-9s slot: version=%u.%u.%u+%u", \
210 ((slot) == BOOT_PRIMARY_SLOT) ? "Primary" : "Secondary", \
211 (hdr)->ih_ver.iv_major, \
212 (hdr)->ih_ver.iv_minor, \
213 (hdr)->ih_ver.iv_revision, \
214 (hdr)->ih_ver.iv_build_num)
215
David Vinczee2453472019-06-17 12:31:59 +0200216/*
Fabio Utziga1fae672018-03-30 10:52:38 -0300217 * The current flashmap API does not check the amount of space allocated when
218 * loading sector data from the flash device, allowing for smaller counts here
219 * would most surely incur in overruns.
220 *
221 * TODO: make flashmap API receive the current sector array size.
222 */
223#if BOOT_MAX_IMG_SECTORS < 32
224#error "Too few sectors, please increase BOOT_MAX_IMG_SECTORS to at least 32"
225#endif
226
Fabio Utzig74aef312019-11-28 11:05:34 -0300227#if MCUBOOT_SWAP_USING_MOVE
228#define BOOT_STATUS_MOVE_STATE_COUNT 1
229#define BOOT_STATUS_SWAP_STATE_COUNT 2
230#define BOOT_STATUS_STATE_COUNT (BOOT_STATUS_MOVE_STATE_COUNT + BOOT_STATUS_SWAP_STATE_COUNT)
231#else
David Vincze2d736ad2019-02-18 11:50:22 +0100232#define BOOT_STATUS_STATE_COUNT 3
Fabio Utzig74aef312019-11-28 11:05:34 -0300233#endif
234
235/** Maximum number of image sectors supported by the bootloader. */
David Vincze2d736ad2019-02-18 11:50:22 +0100236#define BOOT_STATUS_MAX_ENTRIES BOOT_MAX_IMG_SECTORS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800237
David Vincze2d736ad2019-02-18 11:50:22 +0100238#define BOOT_PRIMARY_SLOT 0
239#define BOOT_SECONDARY_SLOT 1
Christopher Collins92ea77f2016-12-12 15:59:26 -0800240
David Vincze2d736ad2019-02-18 11:50:22 +0100241#define BOOT_STATUS_SOURCE_NONE 0
242#define BOOT_STATUS_SOURCE_SCRATCH 1
243#define BOOT_STATUS_SOURCE_PRIMARY_SLOT 2
244
David Brown2b8a6952019-10-01 16:14:53 -0600245#define BOOT_MAGIC_SZ (sizeof boot_img_magic)
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300246
Marti Bolivarc50926f2017-06-14 09:35:40 -0400247/**
248 * Compatibility shim for flash sector type.
249 *
250 * This can be deleted when flash_area_to_sectors() is removed.
251 */
252#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
253typedef struct flash_sector boot_sector_t;
254#else
255typedef struct flash_area boot_sector_t;
256#endif
257
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400258/** Private state maintained during boot. */
259struct boot_loader_state {
260 struct {
261 struct image_header hdr;
Marti Bolivarc0b47912017-06-13 17:18:09 -0400262 const struct flash_area *area;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400263 boot_sector_t *sectors;
Marti Bolivar84898652017-06-13 17:20:22 -0400264 size_t num_sectors;
David Vinczeba3bd602019-06-17 16:01:43 +0200265 } imgs[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS];
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400266
Fabio Utzig12d59162019-11-28 10:01:59 -0300267#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200268 struct {
269 const struct flash_area *area;
270 boot_sector_t *sectors;
271 size_t num_sectors;
272 } scratch;
Fabio Utzig12d59162019-11-28 10:01:59 -0300273#endif
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400274
David Vinczeba3bd602019-06-17 16:01:43 +0200275 uint8_t swap_type[BOOT_IMAGE_NUMBER];
David Brownab449182019-11-15 09:32:52 -0700276 uint32_t write_sz;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300277
278#if defined(MCUBOOT_ENC_IMAGES)
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300279 struct enc_key_data enc[BOOT_IMAGE_NUMBER][BOOT_NUM_SLOTS];
Fabio Utzig10ee6482019-08-01 12:04:52 -0300280#endif
281
Fabio Utzigb0f04732019-07-31 09:49:19 -0300282#if (BOOT_IMAGE_NUMBER > 1)
283 uint8_t curr_img_idx;
284#endif
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400285};
286
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200287int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
288 size_t slen, uint8_t key_id);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800289
Christopher Collinsa1c12042019-05-23 14:00:28 -0700290int boot_magic_compatible_check(uint8_t tbl_val, uint8_t val);
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300291uint32_t boot_status_sz(uint32_t min_write_sz);
David Brownab449182019-11-15 09:32:52 -0700292uint32_t boot_trailer_sz(uint32_t min_write_sz);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300293int boot_status_entries(int image_index, const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800294uint32_t boot_status_off(const struct flash_area *fap);
David Vinczee2453472019-06-17 12:31:59 +0200295uint32_t boot_swap_info_off(const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800296int boot_read_swap_state(const struct flash_area *fap,
297 struct boot_swap_state *state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300298int boot_read_swap_state_by_id(int flash_area_id,
299 struct boot_swap_state *state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800300int boot_write_magic(const struct flash_area *fap);
Fabio Utzig12d59162019-11-28 10:01:59 -0300301int boot_write_status(const struct boot_loader_state *state, struct boot_status *bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800302int boot_write_copy_done(const struct flash_area *fap);
303int boot_write_image_ok(const struct flash_area *fap);
David Vinczee2453472019-06-17 12:31:59 +0200304int boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
305 uint8_t image_num);
Fabio Utzig46490722017-09-04 15:34:32 -0300306int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300307int boot_read_swap_size(int image_index, uint32_t *swap_size);
Fabio Utzig12d59162019-11-28 10:01:59 -0300308int boot_slots_compatible(struct boot_loader_state *state);
309uint32_t boot_status_internal_off(const struct boot_status *bs, int elem_sz);
310int boot_read_image_header(struct boot_loader_state *state, int slot,
311 struct image_header *out_hdr, struct boot_status *bs);
312int boot_copy_region(struct boot_loader_state *state,
313 const struct flash_area *fap_src,
314 const struct flash_area *fap_dst,
315 uint32_t off_src, uint32_t off_dst, uint32_t sz);
316int boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz);
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300317bool boot_status_is_reset(const struct boot_status *bs);
Fabio Utzig12d59162019-11-28 10:01:59 -0300318
Fabio Utzigba829042018-09-18 08:29:34 -0300319#ifdef MCUBOOT_ENC_IMAGES
320int boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
Fabio Utzig4741c452019-12-19 15:32:41 -0300321 const struct boot_status *bs);
322int boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300323#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800324
David Browneffb06e2019-10-10 14:45:32 -0600325/**
326 * Safe (non-overflowing) uint32_t addition. Returns true, and stores
327 * the result in *dest if it can be done without overflow. Otherwise,
328 * returns false.
329 */
330static inline bool boot_u32_safe_add(uint32_t *dest, uint32_t a, uint32_t b)
331{
332 /*
333 * "a + b <= UINT32_MAX", subtract 'b' from both sides to avoid
334 * the overflow.
335 */
336 if (a > UINT32_MAX - b) {
337 return false;
338 } else {
339 *dest = a + b;
340 return true;
341 }
342}
343
344/**
345 * Safe (non-overflowing) uint16_t addition. Returns true, and stores
346 * the result in *dest if it can be done without overflow. Otherwise,
347 * returns false.
348 */
349static inline bool boot_u16_safe_add(uint16_t *dest, uint16_t a, uint16_t b)
350{
351 uint32_t tmp = a + b;
352 if (tmp > UINT16_MAX) {
353 return false;
354 } else {
355 *dest = tmp;
356 return true;
357 }
358}
359
Marti Bolivarf804f622017-06-12 15:41:48 -0400360/*
361 * Accessors for the contents of struct boot_loader_state.
362 */
363
Marti Bolivarc0b47912017-06-13 17:18:09 -0400364/* These are macros so they can be used as lvalues. */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300365#if (BOOT_IMAGE_NUMBER > 1)
366#define BOOT_CURR_IMG(state) ((state)->curr_img_idx)
367#else
368#define BOOT_CURR_IMG(state) 0
Fabio Utzigbc077932019-08-26 11:16:34 -0300369#endif
370#ifdef MCUBOOT_ENC_IMAGES
371#define BOOT_CURR_ENC(state) ((state)->enc[BOOT_CURR_IMG(state)])
372#else
373#define BOOT_CURR_ENC(state) NULL
Fabio Utzigb0f04732019-07-31 09:49:19 -0300374#endif
375#define BOOT_IMG(state, slot) ((state)->imgs[BOOT_CURR_IMG(state)][(slot)])
David Vinczeba3bd602019-06-17 16:01:43 +0200376#define BOOT_IMG_AREA(state, slot) (BOOT_IMG(state, slot).area)
Marti Bolivare10a7392017-06-14 16:20:07 -0400377#define BOOT_WRITE_SZ(state) ((state)->write_sz)
Fabio Utzigb0f04732019-07-31 09:49:19 -0300378#define BOOT_SWAP_TYPE(state) ((state)->swap_type[BOOT_CURR_IMG(state)])
Fabio Utzigc9621352019-08-08 12:15:51 -0300379#define BOOT_TLV_OFF(hdr) ((hdr)->ih_hdr_size + (hdr)->ih_img_size)
Marti Bolivarc0b47912017-06-13 17:18:09 -0400380
Fabio Utzige575b0b2019-09-11 12:34:23 -0300381#define BOOT_IS_UPGRADE(swap_type) \
382 (((swap_type) == BOOT_SWAP_TYPE_TEST) || \
383 ((swap_type) == BOOT_SWAP_TYPE_REVERT) || \
384 ((swap_type) == BOOT_SWAP_TYPE_PERM))
385
Marti Bolivarf804f622017-06-12 15:41:48 -0400386static inline struct image_header*
387boot_img_hdr(struct boot_loader_state *state, size_t slot)
388{
David Vinczeba3bd602019-06-17 16:01:43 +0200389 return &BOOT_IMG(state, slot).hdr;
Marti Bolivarf804f622017-06-12 15:41:48 -0400390}
391
Marti Bolivard3269fd2017-06-12 16:31:12 -0400392static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300393boot_img_num_sectors(const struct boot_loader_state *state, size_t slot)
Marti Bolivard3269fd2017-06-12 16:31:12 -0400394{
David Vinczeba3bd602019-06-17 16:01:43 +0200395 return BOOT_IMG(state, slot).num_sectors;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400396}
397
Marti Bolivar135b8f62017-06-13 17:22:13 -0400398/*
399 * Offset of the slot from the beginning of the flash device.
400 */
401static inline uint32_t
402boot_img_slot_off(struct boot_loader_state *state, size_t slot)
403{
David Vinczeba3bd602019-06-17 16:01:43 +0200404 return BOOT_IMG(state, slot).area->fa_off;
Marti Bolivar135b8f62017-06-13 17:22:13 -0400405}
406
Marti Bolivarc50926f2017-06-14 09:35:40 -0400407#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
408
Marti Bolivard3269fd2017-06-12 16:31:12 -0400409static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300410boot_img_sector_size(const struct boot_loader_state *state,
Marti Bolivard3269fd2017-06-12 16:31:12 -0400411 size_t slot, size_t sector)
412{
David Vinczeba3bd602019-06-17 16:01:43 +0200413 return BOOT_IMG(state, slot).sectors[sector].fa_size;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400414}
415
Marti Bolivarea088872017-06-12 17:10:49 -0400416/*
417 * Offset of the sector from the beginning of the image, NOT the flash
418 * device.
419 */
420static inline uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300421boot_img_sector_off(const struct boot_loader_state *state, size_t slot,
Marti Bolivarea088872017-06-12 17:10:49 -0400422 size_t sector)
423{
David Vinczeba3bd602019-06-17 16:01:43 +0200424 return BOOT_IMG(state, slot).sectors[sector].fa_off -
425 BOOT_IMG(state, slot).sectors[0].fa_off;
Marti Bolivarea088872017-06-12 17:10:49 -0400426}
427
Marti Bolivarc50926f2017-06-14 09:35:40 -0400428#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
429
430static inline size_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300431boot_img_sector_size(const struct boot_loader_state *state,
Marti Bolivarc50926f2017-06-14 09:35:40 -0400432 size_t slot, size_t sector)
433{
David Vinczeba3bd602019-06-17 16:01:43 +0200434 return BOOT_IMG(state, slot).sectors[sector].fs_size;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400435}
436
437static inline uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300438boot_img_sector_off(const struct boot_loader_state *state, size_t slot,
Marti Bolivarc50926f2017-06-14 09:35:40 -0400439 size_t sector)
440{
David Vinczeba3bd602019-06-17 16:01:43 +0200441 return BOOT_IMG(state, slot).sectors[sector].fs_off -
442 BOOT_IMG(state, slot).sectors[0].fs_off;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400443}
444
Marti Bolivarc50926f2017-06-14 09:35:40 -0400445#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
446
Tamas Banfe031092020-09-10 17:32:39 +0200447#ifdef MCUBOOT_RAM_LOAD
448#define LOAD_IMAGE_DATA(hdr, fap, start, output, size) \
449 (memcpy((output),(void*)((hdr)->ih_load_addr + (start)), \
450 (size)) != (output))
451#else
452#define LOAD_IMAGE_DATA(hdr, fap, start, output, size) \
453 (flash_area_read((fap), (start), (output), (size)))
454#endif /* MCUBOOT_RAM_LOAD */
455
Christopher Collins92ea77f2016-12-12 15:59:26 -0800456#ifdef __cplusplus
457}
458#endif
459
460#endif