blob: b6bb7921a8dfde411abd159755670d26da4b92cf [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
20#ifndef H_BOOTUTIL_PRIV_
21#define H_BOOTUTIL_PRIV_
22
Marti Bolivarcca28a92017-06-12 16:52:22 -040023#include "sysflash/sysflash.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020024
25#include <flash_map_backend/flash_map_backend.h>
26
Christopher Collins92ea77f2016-12-12 15:59:26 -080027#include "bootutil/image.h"
Marti Bolivarf91bca52018-04-12 12:40:46 -040028#include "mcuboot_config/mcuboot_config.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080029
Fabio Utzigba829042018-09-18 08:29:34 -030030#ifdef MCUBOOT_ENC_IMAGES
31#include "bootutil/enc_key.h"
32#endif
33
Christopher Collins92ea77f2016-12-12 15:59:26 -080034#ifdef __cplusplus
35extern "C" {
36#endif
37
Marti Bolivar248da082018-04-24 15:11:39 -040038#ifdef MCUBOOT_HAVE_ASSERT_H
39#include "mcuboot_config/mcuboot_assert.h"
Fabio Utzig57c40f72017-12-12 21:48:30 -020040#else
41#define ASSERT assert
42#endif
43
Christopher Collins92ea77f2016-12-12 15:59:26 -080044struct flash_area;
45
46#define BOOT_EFLASH 1
47#define BOOT_EFILE 2
48#define BOOT_EBADIMAGE 3
49#define BOOT_EBADVECT 4
50#define BOOT_EBADSTATUS 5
51#define BOOT_ENOMEM 6
52#define BOOT_EBADARGS 7
53
54#define BOOT_TMPBUF_SZ 256
55
Christopher Collins92ea77f2016-12-12 15:59:26 -080056/*
57 * Maintain state of copy progress.
58 */
59struct boot_status {
Fabio Utzig2473ac02017-05-02 12:45:02 -030060 uint32_t idx; /* Which area we're operating on */
61 uint8_t state; /* Which part of the swapping process are we at */
62 uint8_t use_scratch; /* Are status bytes ever written to scratch? */
Fabio Utzig46490722017-09-04 15:34:32 -030063 uint32_t swap_size; /* Total size of swapped image */
Fabio Utzigba829042018-09-18 08:29:34 -030064#ifdef MCUBOOT_ENC_IMAGES
65 uint8_t enckey[2][BOOT_ENC_KEY_SIZE];
66#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080067};
68
Fabio Utzig39000012018-07-30 12:40:20 -030069#define BOOT_MAGIC_GOOD 1
70#define BOOT_MAGIC_BAD 2
71#define BOOT_MAGIC_UNSET 3
72#define BOOT_MAGIC_ANY 4 /* NOTE: control only, not dependent on sector */
73
74/*
75 * NOTE: leave BOOT_FLAG_SET equal to one, this is written to flash!
76 */
77#define BOOT_FLAG_SET 1
78#define BOOT_FLAG_BAD 2
79#define BOOT_FLAG_UNSET 3
80#define BOOT_FLAG_ANY 4 /* NOTE: control only, not dependent on sector */
81
82#define BOOT_STATUS_IDX_0 1
83
84#define BOOT_STATUS_STATE_0 1
85#define BOOT_STATUS_STATE_1 2
86#define BOOT_STATUS_STATE_2 3
Christopher Collins92ea77f2016-12-12 15:59:26 -080087
88/**
89 * End-of-image slot structure.
90 *
91 * 0 1 2 3
92 * 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
93 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collins92ea77f2016-12-12 15:59:26 -080094 * ~ ~
95 * ~ Swap status (variable, aligned) ~
96 * ~ ~
97 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig46490722017-09-04 15:34:32 -030098 * | Swap size |
Christopher Collins92ea77f2016-12-12 15:59:26 -080099 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig39000012018-07-30 12:40:20 -0300100 * ~ padding with erased val (MAX ALIGN - 4) ~
Fabio Utzig46490722017-09-04 15:34:32 -0300101 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig39000012018-07-30 12:40:20 -0300102 * | Copy done | padding with erased val (MAX ALIGN - 1) ~
Fabio Utzig46490722017-09-04 15:34:32 -0300103 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig39000012018-07-30 12:40:20 -0300104 * | Image OK | padding with erased val (MAX ALIGN - 1) ~
Fabio Utzig46490722017-09-04 15:34:32 -0300105 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106 * ~ MAGIC (16 octets) ~
Christopher Collins92ea77f2016-12-12 15:59:26 -0800107 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108 */
109
110extern const uint32_t boot_img_magic[4];
111
112struct boot_swap_state {
113 uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */
114 uint8_t copy_done;
115 uint8_t image_ok;
116};
117
Marti Bolivarf9bfddd2018-04-24 14:28:33 -0400118#define BOOT_MAX_IMG_SECTORS MCUBOOT_MAX_IMG_SECTORS
Fabio Utziga1fae672018-03-30 10:52:38 -0300119
120/*
121 * The current flashmap API does not check the amount of space allocated when
122 * loading sector data from the flash device, allowing for smaller counts here
123 * would most surely incur in overruns.
124 *
125 * TODO: make flashmap API receive the current sector array size.
126 */
127#if BOOT_MAX_IMG_SECTORS < 32
128#error "Too few sectors, please increase BOOT_MAX_IMG_SECTORS to at least 32"
129#endif
130
131/** Number of image slots in flash; currently limited to two. */
David Vincze2d736ad2019-02-18 11:50:22 +0100132#define BOOT_NUM_SLOTS 2
Fabio Utziga1fae672018-03-30 10:52:38 -0300133
134/** Maximum number of image sectors supported by the bootloader. */
David Vincze2d736ad2019-02-18 11:50:22 +0100135#define BOOT_STATUS_STATE_COUNT 3
136#define BOOT_STATUS_MAX_ENTRIES BOOT_MAX_IMG_SECTORS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800137
David Vincze2d736ad2019-02-18 11:50:22 +0100138#define BOOT_PRIMARY_SLOT 0
139#define BOOT_SECONDARY_SLOT 1
Christopher Collins92ea77f2016-12-12 15:59:26 -0800140
David Vincze2d736ad2019-02-18 11:50:22 +0100141#define BOOT_STATUS_SOURCE_NONE 0
142#define BOOT_STATUS_SOURCE_SCRATCH 1
143#define BOOT_STATUS_SOURCE_PRIMARY_SLOT 2
144
145#define BOOT_FLAG_IMAGE_OK 0
146#define BOOT_FLAG_COPY_DONE 1
147
148#if defined(MCUBOOT_MYNEWT)
149 #define FLASH_AREA_IMAGE_PRIMARY FLASH_AREA_IMAGE_0
150 #define FLASH_AREA_IMAGE_SECONDARY FLASH_AREA_IMAGE_1
151#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -0300152
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300153extern const uint32_t BOOT_MAGIC_SZ;
154
Marti Bolivarc50926f2017-06-14 09:35:40 -0400155/**
156 * Compatibility shim for flash sector type.
157 *
158 * This can be deleted when flash_area_to_sectors() is removed.
159 */
160#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
161typedef struct flash_sector boot_sector_t;
162#else
163typedef struct flash_area boot_sector_t;
164#endif
165
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400166/** Private state maintained during boot. */
167struct boot_loader_state {
168 struct {
169 struct image_header hdr;
Marti Bolivarc0b47912017-06-13 17:18:09 -0400170 const struct flash_area *area;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400171 boot_sector_t *sectors;
Marti Bolivar84898652017-06-13 17:20:22 -0400172 size_t num_sectors;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400173 } imgs[BOOT_NUM_SLOTS];
174
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200175 struct {
176 const struct flash_area *area;
177 boot_sector_t *sectors;
178 size_t num_sectors;
179 } scratch;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400180
181 uint8_t write_sz;
182};
183
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200184int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
185 size_t slen, uint8_t key_id);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800186
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300187uint32_t boot_slots_trailer_sz(uint8_t min_write_sz);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400188int boot_status_entries(const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800189uint32_t boot_status_off(const struct flash_area *fap);
190int boot_read_swap_state(const struct flash_area *fap,
191 struct boot_swap_state *state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300192int boot_read_swap_state_by_id(int flash_area_id,
193 struct boot_swap_state *state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800194int boot_write_magic(const struct flash_area *fap);
195int boot_write_status(struct boot_status *bs);
196int boot_schedule_test_swap(void);
197int boot_write_copy_done(const struct flash_area *fap);
198int boot_write_image_ok(const struct flash_area *fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300199int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
200int boot_read_swap_size(uint32_t *swap_size);
Fabio Utzigba829042018-09-18 08:29:34 -0300201#ifdef MCUBOOT_ENC_IMAGES
202int boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
203 const uint8_t *enckey);
204int boot_read_enc_key(uint8_t slot, uint8_t *enckey);
205#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800206
Marti Bolivarf804f622017-06-12 15:41:48 -0400207/*
208 * Accessors for the contents of struct boot_loader_state.
209 */
210
Marti Bolivarc0b47912017-06-13 17:18:09 -0400211/* These are macros so they can be used as lvalues. */
212#define BOOT_IMG_AREA(state, slot) ((state)->imgs[(slot)].area)
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200213#define BOOT_SCRATCH_AREA(state) ((state)->scratch.area)
Marti Bolivare10a7392017-06-14 16:20:07 -0400214#define BOOT_WRITE_SZ(state) ((state)->write_sz)
Marti Bolivarc0b47912017-06-13 17:18:09 -0400215
Marti Bolivarf804f622017-06-12 15:41:48 -0400216static inline struct image_header*
217boot_img_hdr(struct boot_loader_state *state, size_t slot)
218{
219 return &state->imgs[slot].hdr;
220}
221
Marti Bolivard3269fd2017-06-12 16:31:12 -0400222static inline size_t
223boot_img_num_sectors(struct boot_loader_state *state, size_t slot)
224{
225 return state->imgs[slot].num_sectors;
226}
227
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200228static inline size_t
229boot_scratch_num_sectors(struct boot_loader_state *state)
230{
231 return state->scratch.num_sectors;
232}
233
Marti Bolivar135b8f62017-06-13 17:22:13 -0400234/*
235 * Offset of the slot from the beginning of the flash device.
236 */
237static inline uint32_t
238boot_img_slot_off(struct boot_loader_state *state, size_t slot)
239{
240 return state->imgs[slot].area->fa_off;
241}
242
243static inline size_t boot_scratch_area_size(struct boot_loader_state *state)
244{
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200245 return BOOT_SCRATCH_AREA(state)->fa_size;
Marti Bolivar135b8f62017-06-13 17:22:13 -0400246}
247
Marti Bolivarc50926f2017-06-14 09:35:40 -0400248#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
249
Marti Bolivard3269fd2017-06-12 16:31:12 -0400250static inline size_t
251boot_img_sector_size(struct boot_loader_state *state,
252 size_t slot, size_t sector)
253{
254 return state->imgs[slot].sectors[sector].fa_size;
255}
256
Marti Bolivarea088872017-06-12 17:10:49 -0400257/*
258 * Offset of the sector from the beginning of the image, NOT the flash
259 * device.
260 */
261static inline uint32_t
262boot_img_sector_off(struct boot_loader_state *state, size_t slot,
263 size_t sector)
264{
265 return state->imgs[slot].sectors[sector].fa_off -
266 state->imgs[slot].sectors[0].fa_off;
267}
268
Marti Bolivarcca28a92017-06-12 16:52:22 -0400269static inline int
270boot_initialize_area(struct boot_loader_state *state, int flash_area)
271{
Marti Bolivarcca28a92017-06-12 16:52:22 -0400272 int num_sectors = BOOT_MAX_IMG_SECTORS;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400273 int rc;
274
275 switch (flash_area) {
David Vincze2d736ad2019-02-18 11:50:22 +0100276 case FLASH_AREA_IMAGE_PRIMARY:
277 rc = flash_area_to_sectors(flash_area, &num_sectors,
278 state->imgs[BOOT_PRIMARY_SLOT].sectors);
279 state->imgs[BOOT_PRIMARY_SLOT].num_sectors = (size_t)num_sectors;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400280 break;
David Vincze2d736ad2019-02-18 11:50:22 +0100281 case FLASH_AREA_IMAGE_SECONDARY:
282 rc = flash_area_to_sectors(flash_area, &num_sectors,
283 state->imgs[BOOT_SECONDARY_SLOT].sectors);
284 state->imgs[BOOT_SECONDARY_SLOT].num_sectors = (size_t)num_sectors;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200285 break;
286 case FLASH_AREA_IMAGE_SCRATCH:
David Vincze2d736ad2019-02-18 11:50:22 +0100287 rc = flash_area_to_sectors(flash_area, &num_sectors,
288 state->scratch.sectors);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200289 state->scratch.num_sectors = (size_t)num_sectors;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400290 break;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400291 default:
292 return BOOT_EFLASH;
293 }
294
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200295 return rc;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400296}
297
Marti Bolivarc50926f2017-06-14 09:35:40 -0400298#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
299
300static inline size_t
301boot_img_sector_size(struct boot_loader_state *state,
302 size_t slot, size_t sector)
303{
304 return state->imgs[slot].sectors[sector].fs_size;
305}
306
307static inline uint32_t
308boot_img_sector_off(struct boot_loader_state *state, size_t slot,
309 size_t sector)
310{
311 return state->imgs[slot].sectors[sector].fs_off -
312 state->imgs[slot].sectors[0].fs_off;
313}
314
315static inline int
316boot_initialize_area(struct boot_loader_state *state, int flash_area)
317{
318 uint32_t num_sectors;
319 struct flash_sector *out_sectors;
320 size_t *out_num_sectors;
321 int rc;
322
323 switch (flash_area) {
David Vincze2d736ad2019-02-18 11:50:22 +0100324 case FLASH_AREA_IMAGE_PRIMARY:
Marti Bolivarc50926f2017-06-14 09:35:40 -0400325 num_sectors = BOOT_MAX_IMG_SECTORS;
David Vincze2d736ad2019-02-18 11:50:22 +0100326 out_sectors = state->imgs[BOOT_PRIMARY_SLOT].sectors;
327 out_num_sectors = &state->imgs[BOOT_PRIMARY_SLOT].num_sectors;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400328 break;
David Vincze2d736ad2019-02-18 11:50:22 +0100329 case FLASH_AREA_IMAGE_SECONDARY:
Marti Bolivarc50926f2017-06-14 09:35:40 -0400330 num_sectors = BOOT_MAX_IMG_SECTORS;
David Vincze2d736ad2019-02-18 11:50:22 +0100331 out_sectors = state->imgs[BOOT_SECONDARY_SLOT].sectors;
332 out_num_sectors = &state->imgs[BOOT_SECONDARY_SLOT].num_sectors;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400333 break;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200334 case FLASH_AREA_IMAGE_SCRATCH:
335 num_sectors = BOOT_MAX_IMG_SECTORS;
336 out_sectors = state->scratch.sectors;
337 out_num_sectors = &state->scratch.num_sectors;
338 break;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400339 default:
340 return -1;
341 }
342
343 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
344 if (rc != 0) {
345 return rc;
346 }
347 *out_num_sectors = num_sectors;
348 return 0;
349}
350
351#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
352
Christopher Collins92ea77f2016-12-12 15:59:26 -0800353#ifdef __cplusplus
354}
355#endif
356
357#endif