blob: 75709ccdd2b74ccdc18bc868cc3d4eed0535980a [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
Christopher Collins92ea77f2016-12-12 15:59:26 -080062/*
63 * Maintain state of copy progress.
64 */
65struct boot_status {
Fabio Utzig2473ac02017-05-02 12:45:02 -030066 uint32_t idx; /* Which area we're operating on */
67 uint8_t state; /* Which part of the swapping process are we at */
68 uint8_t use_scratch; /* Are status bytes ever written to scratch? */
Christopher Collinsa1c12042019-05-23 14:00:28 -070069 uint8_t swap_type; /* The type of swap in effect */
Fabio Utzig46490722017-09-04 15:34:32 -030070 uint32_t swap_size; /* Total size of swapped image */
Fabio Utzigba829042018-09-18 08:29:34 -030071#ifdef MCUBOOT_ENC_IMAGES
72 uint8_t enckey[2][BOOT_ENC_KEY_SIZE];
73#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080074};
75
Fabio Utzig39000012018-07-30 12:40:20 -030076#define BOOT_MAGIC_GOOD 1
77#define BOOT_MAGIC_BAD 2
78#define BOOT_MAGIC_UNSET 3
79#define BOOT_MAGIC_ANY 4 /* NOTE: control only, not dependent on sector */
Christopher Collinsa1c12042019-05-23 14:00:28 -070080#define BOOT_MAGIC_NOTGOOD 5 /* NOTE: control only, not dependent on sector */
Fabio Utzig39000012018-07-30 12:40:20 -030081
82/*
83 * NOTE: leave BOOT_FLAG_SET equal to one, this is written to flash!
84 */
85#define BOOT_FLAG_SET 1
86#define BOOT_FLAG_BAD 2
87#define BOOT_FLAG_UNSET 3
88#define BOOT_FLAG_ANY 4 /* NOTE: control only, not dependent on sector */
89
90#define BOOT_STATUS_IDX_0 1
91
92#define BOOT_STATUS_STATE_0 1
93#define BOOT_STATUS_STATE_1 2
94#define BOOT_STATUS_STATE_2 3
Christopher Collins92ea77f2016-12-12 15:59:26 -080095
96/**
97 * End-of-image slot structure.
98 *
Christopher Collinsa1c12042019-05-23 14:00:28 -070099 * 0 1 2 3
100 * 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
101 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102 * ~ ~
103 * ~ Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3) ~
104 * ~ ~
105 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106 * | Encryption key 0 (16 octets) [*] |
107 * | |
108 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 * | Encryption key 1 (16 octets) [*] |
110 * | |
111 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112 * | Swap size (4 octets) |
113 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Vinczee2453472019-06-17 12:31:59 +0200114 * | Swap info | 0xff padding (7 octets) |
Christopher Collinsa1c12042019-05-23 14:00:28 -0700115 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116 * | Copy done | 0xff padding (7 octets) |
117 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
118 * | Image OK | 0xff padding (7 octets) |
119 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
120 * | MAGIC (16 octets) |
121 * | |
122 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
123 *
124 * [*]: Only present if the encryption option is enabled
125 * (`MCUBOOT_ENC_IMAGES`).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800126 */
127
128extern const uint32_t boot_img_magic[4];
129
130struct boot_swap_state {
Christopher Collinsa1c12042019-05-23 14:00:28 -0700131 uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */
132 uint8_t swap_type; /* One of the BOOT_SWAP_TYPE_[...] values. */
133 uint8_t copy_done; /* One of the BOOT_FLAG_[...] values. */
134 uint8_t image_ok; /* One of the BOOT_FLAG_[...] values. */
David Vinczee2453472019-06-17 12:31:59 +0200135 uint8_t image_num; /* Boot status belongs to this image */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800136};
137
David Vinczeb75c12a2019-03-22 14:58:33 +0100138#ifdef MCUBOOT_IMAGE_NUMBER
139#define BOOT_IMAGE_NUMBER MCUBOOT_IMAGE_NUMBER
140#else
141#define BOOT_IMAGE_NUMBER 1
142#endif
143
Fabio Utzigabec0732019-07-31 08:40:22 -0300144_Static_assert(BOOT_IMAGE_NUMBER > 0, "Invalid value for BOOT_IMAGE_NUMBER");
145
Marti Bolivarf9bfddd2018-04-24 14:28:33 -0400146#define BOOT_MAX_IMG_SECTORS MCUBOOT_MAX_IMG_SECTORS
Fabio Utziga1fae672018-03-30 10:52:38 -0300147
148/*
David Vinczee2453472019-06-17 12:31:59 +0200149 * Extract the swap type and image number from image trailers's swap_info
150 * filed.
151 */
152#define BOOT_GET_SWAP_TYPE(swap_info) ((swap_info) & 0x0F)
153#define BOOT_GET_IMAGE_NUM(swap_info) ((swap_info) >> 4)
154
155/* Construct the swap_info field from swap type and image number */
156#define BOOT_SET_SWAP_INFO(swap_info, image, type) { \
157 assert((image) < 0xF); \
158 assert((type) < 0xF); \
159 (swap_info) = (image) << 4 \
160 | (type); \
161 }
162
163/*
Fabio Utziga1fae672018-03-30 10:52:38 -0300164 * The current flashmap API does not check the amount of space allocated when
165 * loading sector data from the flash device, allowing for smaller counts here
166 * would most surely incur in overruns.
167 *
168 * TODO: make flashmap API receive the current sector array size.
169 */
170#if BOOT_MAX_IMG_SECTORS < 32
171#error "Too few sectors, please increase BOOT_MAX_IMG_SECTORS to at least 32"
172#endif
173
174/** Number of image slots in flash; currently limited to two. */
David Vincze2d736ad2019-02-18 11:50:22 +0100175#define BOOT_NUM_SLOTS 2
Fabio Utziga1fae672018-03-30 10:52:38 -0300176
177/** Maximum number of image sectors supported by the bootloader. */
David Vincze2d736ad2019-02-18 11:50:22 +0100178#define BOOT_STATUS_STATE_COUNT 3
179#define BOOT_STATUS_MAX_ENTRIES BOOT_MAX_IMG_SECTORS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800180
David Vincze2d736ad2019-02-18 11:50:22 +0100181#define BOOT_PRIMARY_SLOT 0
182#define BOOT_SECONDARY_SLOT 1
Christopher Collins92ea77f2016-12-12 15:59:26 -0800183
David Vincze2d736ad2019-02-18 11:50:22 +0100184#define BOOT_STATUS_SOURCE_NONE 0
185#define BOOT_STATUS_SOURCE_SCRATCH 1
186#define BOOT_STATUS_SOURCE_PRIMARY_SLOT 2
187
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300188extern const uint32_t BOOT_MAGIC_SZ;
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;
Marti Bolivar84898652017-06-13 17:20:22 -0400207 size_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 Utzig2bd980a2018-11-26 10:38:17 -0200210 struct {
211 const struct flash_area *area;
212 boot_sector_t *sectors;
213 size_t num_sectors;
214 } scratch;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400215
David Vinczeba3bd602019-06-17 16:01:43 +0200216 uint8_t swap_type[BOOT_IMAGE_NUMBER];
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400217 uint8_t write_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300218#if (BOOT_IMAGE_NUMBER > 1)
219 uint8_t curr_img_idx;
220#endif
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400221};
222
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200223int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
224 size_t slen, uint8_t key_id);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800225
Christopher Collinsa1c12042019-05-23 14:00:28 -0700226int boot_magic_compatible_check(uint8_t tbl_val, uint8_t val);
Christopher Collins2adef702019-05-22 14:37:31 -0700227uint32_t boot_trailer_sz(uint8_t min_write_sz);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300228int boot_status_entries(int image_index, const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800229uint32_t boot_status_off(const struct flash_area *fap);
David Vinczee2453472019-06-17 12:31:59 +0200230uint32_t boot_swap_info_off(const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800231int boot_read_swap_state(const struct flash_area *fap,
232 struct boot_swap_state *state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300233int boot_read_swap_state_by_id(int flash_area_id,
234 struct boot_swap_state *state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800235int boot_write_magic(const struct flash_area *fap);
236int boot_write_status(struct boot_status *bs);
237int boot_schedule_test_swap(void);
238int boot_write_copy_done(const struct flash_area *fap);
239int boot_write_image_ok(const struct flash_area *fap);
David Vinczee2453472019-06-17 12:31:59 +0200240int boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
241 uint8_t image_num);
Fabio Utzig46490722017-09-04 15:34:32 -0300242int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300243int boot_read_swap_size(int image_index, uint32_t *swap_size);
Fabio Utzigba829042018-09-18 08:29:34 -0300244#ifdef MCUBOOT_ENC_IMAGES
245int boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
246 const uint8_t *enckey);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300247int boot_read_enc_key(int image_index, uint8_t slot, uint8_t *enckey);
Fabio Utzigba829042018-09-18 08:29:34 -0300248#endif
David Vinczee32483f2019-06-13 10:46:24 +0200249#if (BOOT_IMAGE_NUMBER > 1)
250int boot_is_version_sufficient(struct image_version *req,
251 struct image_version *ver);
252#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800253
Marti Bolivarf804f622017-06-12 15:41:48 -0400254/*
255 * Accessors for the contents of struct boot_loader_state.
256 */
257
Marti Bolivarc0b47912017-06-13 17:18:09 -0400258/* These are macros so they can be used as lvalues. */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300259#if (BOOT_IMAGE_NUMBER > 1)
260#define BOOT_CURR_IMG(state) ((state)->curr_img_idx)
261#else
262#define BOOT_CURR_IMG(state) 0
263#endif
264#define BOOT_IMG(state, slot) ((state)->imgs[BOOT_CURR_IMG(state)][(slot)])
David Vinczeba3bd602019-06-17 16:01:43 +0200265#define BOOT_IMG_AREA(state, slot) (BOOT_IMG(state, slot).area)
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200266#define BOOT_SCRATCH_AREA(state) ((state)->scratch.area)
Marti Bolivare10a7392017-06-14 16:20:07 -0400267#define BOOT_WRITE_SZ(state) ((state)->write_sz)
Fabio Utzigb0f04732019-07-31 09:49:19 -0300268#define BOOT_SWAP_TYPE(state) ((state)->swap_type[BOOT_CURR_IMG(state)])
Marti Bolivarc0b47912017-06-13 17:18:09 -0400269
Marti Bolivarf804f622017-06-12 15:41:48 -0400270static inline struct image_header*
271boot_img_hdr(struct boot_loader_state *state, size_t slot)
272{
David Vinczeba3bd602019-06-17 16:01:43 +0200273 return &BOOT_IMG(state, slot).hdr;
Marti Bolivarf804f622017-06-12 15:41:48 -0400274}
275
Marti Bolivard3269fd2017-06-12 16:31:12 -0400276static inline size_t
277boot_img_num_sectors(struct boot_loader_state *state, size_t slot)
278{
David Vinczeba3bd602019-06-17 16:01:43 +0200279 return BOOT_IMG(state, slot).num_sectors;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400280}
281
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200282static inline size_t
283boot_scratch_num_sectors(struct boot_loader_state *state)
284{
285 return state->scratch.num_sectors;
286}
287
Marti Bolivar135b8f62017-06-13 17:22:13 -0400288/*
289 * Offset of the slot from the beginning of the flash device.
290 */
291static inline uint32_t
292boot_img_slot_off(struct boot_loader_state *state, size_t slot)
293{
David Vinczeba3bd602019-06-17 16:01:43 +0200294 return BOOT_IMG(state, slot).area->fa_off;
Marti Bolivar135b8f62017-06-13 17:22:13 -0400295}
296
297static inline size_t boot_scratch_area_size(struct boot_loader_state *state)
298{
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200299 return BOOT_SCRATCH_AREA(state)->fa_size;
Marti Bolivar135b8f62017-06-13 17:22:13 -0400300}
301
Marti Bolivarc50926f2017-06-14 09:35:40 -0400302#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
303
Marti Bolivard3269fd2017-06-12 16:31:12 -0400304static inline size_t
305boot_img_sector_size(struct boot_loader_state *state,
306 size_t slot, size_t sector)
307{
David Vinczeba3bd602019-06-17 16:01:43 +0200308 return BOOT_IMG(state, slot).sectors[sector].fa_size;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400309}
310
Marti Bolivarea088872017-06-12 17:10:49 -0400311/*
312 * Offset of the sector from the beginning of the image, NOT the flash
313 * device.
314 */
315static inline uint32_t
316boot_img_sector_off(struct boot_loader_state *state, size_t slot,
317 size_t sector)
318{
David Vinczeba3bd602019-06-17 16:01:43 +0200319 return BOOT_IMG(state, slot).sectors[sector].fa_off -
320 BOOT_IMG(state, slot).sectors[0].fa_off;
Marti Bolivarea088872017-06-12 17:10:49 -0400321}
322
Marti Bolivarcca28a92017-06-12 16:52:22 -0400323static inline int
324boot_initialize_area(struct boot_loader_state *state, int flash_area)
325{
Marti Bolivarcca28a92017-06-12 16:52:22 -0400326 int num_sectors = BOOT_MAX_IMG_SECTORS;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400327 int rc;
328
Fabio Utzigb0f04732019-07-31 09:49:19 -0300329 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100330 rc = flash_area_to_sectors(flash_area, &num_sectors,
David Vinczeba3bd602019-06-17 16:01:43 +0200331 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
332 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
David Vinczeb75c12a2019-03-22 14:58:33 +0100333
Fabio Utzigb0f04732019-07-31 09:49:19 -0300334 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100335 rc = flash_area_to_sectors(flash_area, &num_sectors,
David Vinczeba3bd602019-06-17 16:01:43 +0200336 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
337 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
David Vinczeb75c12a2019-03-22 14:58:33 +0100338
339 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
David Vincze2d736ad2019-02-18 11:50:22 +0100340 rc = flash_area_to_sectors(flash_area, &num_sectors,
341 state->scratch.sectors);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200342 state->scratch.num_sectors = (size_t)num_sectors;
David Vinczeb75c12a2019-03-22 14:58:33 +0100343 } else {
Marti Bolivarcca28a92017-06-12 16:52:22 -0400344 return BOOT_EFLASH;
345 }
346
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200347 return rc;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400348}
349
Marti Bolivarc50926f2017-06-14 09:35:40 -0400350#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
351
352static inline size_t
353boot_img_sector_size(struct boot_loader_state *state,
354 size_t slot, size_t sector)
355{
David Vinczeba3bd602019-06-17 16:01:43 +0200356 return BOOT_IMG(state, slot).sectors[sector].fs_size;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400357}
358
359static inline uint32_t
360boot_img_sector_off(struct boot_loader_state *state, size_t slot,
361 size_t sector)
362{
David Vinczeba3bd602019-06-17 16:01:43 +0200363 return BOOT_IMG(state, slot).sectors[sector].fs_off -
364 BOOT_IMG(state, slot).sectors[0].fs_off;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400365}
366
367static inline int
368boot_initialize_area(struct boot_loader_state *state, int flash_area)
369{
370 uint32_t num_sectors;
371 struct flash_sector *out_sectors;
372 size_t *out_num_sectors;
373 int rc;
374
David Vinczeb75c12a2019-03-22 14:58:33 +0100375 num_sectors = BOOT_MAX_IMG_SECTORS;
376
Fabio Utzigb0f04732019-07-31 09:49:19 -0300377 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
David Vinczeba3bd602019-06-17 16:01:43 +0200378 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
379 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300380 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
David Vinczeba3bd602019-06-17 16:01:43 +0200381 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
382 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
David Vinczeb75c12a2019-03-22 14:58:33 +0100383 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200384 out_sectors = state->scratch.sectors;
385 out_num_sectors = &state->scratch.num_sectors;
David Vinczeb75c12a2019-03-22 14:58:33 +0100386 } else {
387 return BOOT_EFLASH;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400388 }
389
390 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
391 if (rc != 0) {
392 return rc;
393 }
394 *out_num_sectors = num_sectors;
395 return 0;
396}
397
398#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
399
Christopher Collins92ea77f2016-12-12 15:59:26 -0800400#ifdef __cplusplus
401}
402#endif
403
404#endif