blob: d2871eee7487ced4afda798f8d60f1f2ba4d1e6b [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 Collins01dfbb62019-03-21 07:28:37 -070027#include "bootutil/bootutil.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080028#include "bootutil/image.h"
Marti Bolivarf91bca52018-04-12 12:40:46 -040029#include "mcuboot_config/mcuboot_config.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080030
Fabio Utzigba829042018-09-18 08:29:34 -030031#ifdef MCUBOOT_ENC_IMAGES
32#include "bootutil/enc_key.h"
33#endif
34
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#ifdef __cplusplus
36extern "C" {
37#endif
38
Marti Bolivar248da082018-04-24 15:11:39 -040039#ifdef MCUBOOT_HAVE_ASSERT_H
40#include "mcuboot_config/mcuboot_assert.h"
Fabio Utzig57c40f72017-12-12 21:48:30 -020041#else
42#define ASSERT assert
43#endif
44
Christopher Collins92ea77f2016-12-12 15:59:26 -080045struct flash_area;
46
47#define BOOT_EFLASH 1
48#define BOOT_EFILE 2
49#define BOOT_EBADIMAGE 3
50#define BOOT_EBADVECT 4
51#define BOOT_EBADSTATUS 5
52#define BOOT_ENOMEM 6
53#define BOOT_EBADARGS 7
54
55#define BOOT_TMPBUF_SZ 256
56
Christopher Collins92ea77f2016-12-12 15:59:26 -080057/*
58 * Maintain state of copy progress.
59 */
60struct boot_status {
Fabio Utzig2473ac02017-05-02 12:45:02 -030061 uint32_t idx; /* Which area we're operating on */
62 uint8_t state; /* Which part of the swapping process are we at */
63 uint8_t use_scratch; /* Are status bytes ever written to scratch? */
Christopher Collinsa1c12042019-05-23 14:00:28 -070064 uint8_t swap_type; /* The type of swap in effect */
Fabio Utzig46490722017-09-04 15:34:32 -030065 uint32_t swap_size; /* Total size of swapped image */
Fabio Utzigba829042018-09-18 08:29:34 -030066#ifdef MCUBOOT_ENC_IMAGES
67 uint8_t enckey[2][BOOT_ENC_KEY_SIZE];
68#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080069};
70
Fabio Utzig39000012018-07-30 12:40:20 -030071#define BOOT_MAGIC_GOOD 1
72#define BOOT_MAGIC_BAD 2
73#define BOOT_MAGIC_UNSET 3
74#define BOOT_MAGIC_ANY 4 /* NOTE: control only, not dependent on sector */
Christopher Collinsa1c12042019-05-23 14:00:28 -070075#define BOOT_MAGIC_NOTGOOD 5 /* NOTE: control only, not dependent on sector */
Fabio Utzig39000012018-07-30 12:40:20 -030076
77/*
78 * NOTE: leave BOOT_FLAG_SET equal to one, this is written to flash!
79 */
80#define BOOT_FLAG_SET 1
81#define BOOT_FLAG_BAD 2
82#define BOOT_FLAG_UNSET 3
83#define BOOT_FLAG_ANY 4 /* NOTE: control only, not dependent on sector */
84
85#define BOOT_STATUS_IDX_0 1
86
87#define BOOT_STATUS_STATE_0 1
88#define BOOT_STATUS_STATE_1 2
89#define BOOT_STATUS_STATE_2 3
Christopher Collins92ea77f2016-12-12 15:59:26 -080090
91/**
92 * End-of-image slot structure.
93 *
Christopher Collinsa1c12042019-05-23 14:00:28 -070094 * 0 1 2 3
95 * 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
96 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97 * ~ ~
98 * ~ Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3) ~
99 * ~ ~
100 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101 * | Encryption key 0 (16 octets) [*] |
102 * | |
103 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104 * | Encryption key 1 (16 octets) [*] |
105 * | |
106 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107 * | Swap size (4 octets) |
108 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 * | Swap type | 0xff padding (7 octets) |
110 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111 * | Copy done | 0xff padding (7 octets) |
112 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113 * | Image OK | 0xff padding (7 octets) |
114 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115 * | MAGIC (16 octets) |
116 * | |
117 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
118 *
119 * [*]: Only present if the encryption option is enabled
120 * (`MCUBOOT_ENC_IMAGES`).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800121 */
122
123extern const uint32_t boot_img_magic[4];
124
125struct boot_swap_state {
Christopher Collinsa1c12042019-05-23 14:00:28 -0700126 uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */
127 uint8_t swap_type; /* One of the BOOT_SWAP_TYPE_[...] values. */
128 uint8_t copy_done; /* One of the BOOT_FLAG_[...] values. */
129 uint8_t image_ok; /* One of the BOOT_FLAG_[...] values. */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800130};
131
Marti Bolivarf9bfddd2018-04-24 14:28:33 -0400132#define BOOT_MAX_IMG_SECTORS MCUBOOT_MAX_IMG_SECTORS
Fabio Utziga1fae672018-03-30 10:52:38 -0300133
134/*
135 * The current flashmap API does not check the amount of space allocated when
136 * loading sector data from the flash device, allowing for smaller counts here
137 * would most surely incur in overruns.
138 *
139 * TODO: make flashmap API receive the current sector array size.
140 */
141#if BOOT_MAX_IMG_SECTORS < 32
142#error "Too few sectors, please increase BOOT_MAX_IMG_SECTORS to at least 32"
143#endif
144
145/** Number of image slots in flash; currently limited to two. */
David Vincze2d736ad2019-02-18 11:50:22 +0100146#define BOOT_NUM_SLOTS 2
Fabio Utziga1fae672018-03-30 10:52:38 -0300147
148/** Maximum number of image sectors supported by the bootloader. */
David Vincze2d736ad2019-02-18 11:50:22 +0100149#define BOOT_STATUS_STATE_COUNT 3
150#define BOOT_STATUS_MAX_ENTRIES BOOT_MAX_IMG_SECTORS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800151
David Vincze2d736ad2019-02-18 11:50:22 +0100152#define BOOT_PRIMARY_SLOT 0
153#define BOOT_SECONDARY_SLOT 1
Christopher Collins92ea77f2016-12-12 15:59:26 -0800154
David Vincze2d736ad2019-02-18 11:50:22 +0100155#define BOOT_STATUS_SOURCE_NONE 0
156#define BOOT_STATUS_SOURCE_SCRATCH 1
157#define BOOT_STATUS_SOURCE_PRIMARY_SLOT 2
158
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300159extern const uint32_t BOOT_MAGIC_SZ;
160
Marti Bolivarc50926f2017-06-14 09:35:40 -0400161/**
162 * Compatibility shim for flash sector type.
163 *
164 * This can be deleted when flash_area_to_sectors() is removed.
165 */
166#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
167typedef struct flash_sector boot_sector_t;
168#else
169typedef struct flash_area boot_sector_t;
170#endif
171
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400172/** Private state maintained during boot. */
173struct boot_loader_state {
174 struct {
175 struct image_header hdr;
Marti Bolivarc0b47912017-06-13 17:18:09 -0400176 const struct flash_area *area;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400177 boot_sector_t *sectors;
Marti Bolivar84898652017-06-13 17:20:22 -0400178 size_t num_sectors;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400179 } imgs[BOOT_NUM_SLOTS];
180
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200181 struct {
182 const struct flash_area *area;
183 boot_sector_t *sectors;
184 size_t num_sectors;
185 } scratch;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400186
187 uint8_t write_sz;
188};
189
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200190int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
191 size_t slen, uint8_t key_id);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800192
Christopher Collinsa1c12042019-05-23 14:00:28 -0700193int boot_magic_compatible_check(uint8_t tbl_val, uint8_t val);
Christopher Collins2adef702019-05-22 14:37:31 -0700194uint32_t boot_trailer_sz(uint8_t min_write_sz);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400195int boot_status_entries(const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800196uint32_t boot_status_off(const struct flash_area *fap);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700197uint32_t boot_swap_type_off(const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800198int boot_read_swap_state(const struct flash_area *fap,
199 struct boot_swap_state *state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300200int boot_read_swap_state_by_id(int flash_area_id,
201 struct boot_swap_state *state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800202int boot_write_magic(const struct flash_area *fap);
203int boot_write_status(struct boot_status *bs);
204int boot_schedule_test_swap(void);
205int boot_write_copy_done(const struct flash_area *fap);
206int boot_write_image_ok(const struct flash_area *fap);
Christopher Collinsa1c12042019-05-23 14:00:28 -0700207int boot_write_swap_type(const struct flash_area *fap, uint8_t swap_type);
Fabio Utzig46490722017-09-04 15:34:32 -0300208int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
209int boot_read_swap_size(uint32_t *swap_size);
Fabio Utzigba829042018-09-18 08:29:34 -0300210#ifdef MCUBOOT_ENC_IMAGES
211int boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
212 const uint8_t *enckey);
213int boot_read_enc_key(uint8_t slot, uint8_t *enckey);
214#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800215
Marti Bolivarf804f622017-06-12 15:41:48 -0400216/*
217 * Accessors for the contents of struct boot_loader_state.
218 */
219
Marti Bolivarc0b47912017-06-13 17:18:09 -0400220/* These are macros so they can be used as lvalues. */
221#define BOOT_IMG_AREA(state, slot) ((state)->imgs[(slot)].area)
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200222#define BOOT_SCRATCH_AREA(state) ((state)->scratch.area)
Marti Bolivare10a7392017-06-14 16:20:07 -0400223#define BOOT_WRITE_SZ(state) ((state)->write_sz)
Marti Bolivarc0b47912017-06-13 17:18:09 -0400224
Marti Bolivarf804f622017-06-12 15:41:48 -0400225static inline struct image_header*
226boot_img_hdr(struct boot_loader_state *state, size_t slot)
227{
228 return &state->imgs[slot].hdr;
229}
230
Marti Bolivard3269fd2017-06-12 16:31:12 -0400231static inline size_t
232boot_img_num_sectors(struct boot_loader_state *state, size_t slot)
233{
234 return state->imgs[slot].num_sectors;
235}
236
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200237static inline size_t
238boot_scratch_num_sectors(struct boot_loader_state *state)
239{
240 return state->scratch.num_sectors;
241}
242
Marti Bolivar135b8f62017-06-13 17:22:13 -0400243/*
244 * Offset of the slot from the beginning of the flash device.
245 */
246static inline uint32_t
247boot_img_slot_off(struct boot_loader_state *state, size_t slot)
248{
249 return state->imgs[slot].area->fa_off;
250}
251
252static inline size_t boot_scratch_area_size(struct boot_loader_state *state)
253{
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200254 return BOOT_SCRATCH_AREA(state)->fa_size;
Marti Bolivar135b8f62017-06-13 17:22:13 -0400255}
256
Marti Bolivarc50926f2017-06-14 09:35:40 -0400257#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
258
Marti Bolivard3269fd2017-06-12 16:31:12 -0400259static inline size_t
260boot_img_sector_size(struct boot_loader_state *state,
261 size_t slot, size_t sector)
262{
263 return state->imgs[slot].sectors[sector].fa_size;
264}
265
Marti Bolivarea088872017-06-12 17:10:49 -0400266/*
267 * Offset of the sector from the beginning of the image, NOT the flash
268 * device.
269 */
270static inline uint32_t
271boot_img_sector_off(struct boot_loader_state *state, size_t slot,
272 size_t sector)
273{
274 return state->imgs[slot].sectors[sector].fa_off -
275 state->imgs[slot].sectors[0].fa_off;
276}
277
Marti Bolivarcca28a92017-06-12 16:52:22 -0400278static inline int
279boot_initialize_area(struct boot_loader_state *state, int flash_area)
280{
Marti Bolivarcca28a92017-06-12 16:52:22 -0400281 int num_sectors = BOOT_MAX_IMG_SECTORS;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400282 int rc;
283
284 switch (flash_area) {
David Vincze2d736ad2019-02-18 11:50:22 +0100285 case FLASH_AREA_IMAGE_PRIMARY:
286 rc = flash_area_to_sectors(flash_area, &num_sectors,
287 state->imgs[BOOT_PRIMARY_SLOT].sectors);
288 state->imgs[BOOT_PRIMARY_SLOT].num_sectors = (size_t)num_sectors;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400289 break;
David Vincze2d736ad2019-02-18 11:50:22 +0100290 case FLASH_AREA_IMAGE_SECONDARY:
291 rc = flash_area_to_sectors(flash_area, &num_sectors,
292 state->imgs[BOOT_SECONDARY_SLOT].sectors);
293 state->imgs[BOOT_SECONDARY_SLOT].num_sectors = (size_t)num_sectors;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200294 break;
295 case FLASH_AREA_IMAGE_SCRATCH:
David Vincze2d736ad2019-02-18 11:50:22 +0100296 rc = flash_area_to_sectors(flash_area, &num_sectors,
297 state->scratch.sectors);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200298 state->scratch.num_sectors = (size_t)num_sectors;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400299 break;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400300 default:
301 return BOOT_EFLASH;
302 }
303
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200304 return rc;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400305}
306
Marti Bolivarc50926f2017-06-14 09:35:40 -0400307#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
308
309static inline size_t
310boot_img_sector_size(struct boot_loader_state *state,
311 size_t slot, size_t sector)
312{
313 return state->imgs[slot].sectors[sector].fs_size;
314}
315
316static inline uint32_t
317boot_img_sector_off(struct boot_loader_state *state, size_t slot,
318 size_t sector)
319{
320 return state->imgs[slot].sectors[sector].fs_off -
321 state->imgs[slot].sectors[0].fs_off;
322}
323
324static inline int
325boot_initialize_area(struct boot_loader_state *state, int flash_area)
326{
327 uint32_t num_sectors;
328 struct flash_sector *out_sectors;
329 size_t *out_num_sectors;
330 int rc;
331
332 switch (flash_area) {
David Vincze2d736ad2019-02-18 11:50:22 +0100333 case FLASH_AREA_IMAGE_PRIMARY:
Marti Bolivarc50926f2017-06-14 09:35:40 -0400334 num_sectors = BOOT_MAX_IMG_SECTORS;
David Vincze2d736ad2019-02-18 11:50:22 +0100335 out_sectors = state->imgs[BOOT_PRIMARY_SLOT].sectors;
336 out_num_sectors = &state->imgs[BOOT_PRIMARY_SLOT].num_sectors;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400337 break;
David Vincze2d736ad2019-02-18 11:50:22 +0100338 case FLASH_AREA_IMAGE_SECONDARY:
Marti Bolivarc50926f2017-06-14 09:35:40 -0400339 num_sectors = BOOT_MAX_IMG_SECTORS;
David Vincze2d736ad2019-02-18 11:50:22 +0100340 out_sectors = state->imgs[BOOT_SECONDARY_SLOT].sectors;
341 out_num_sectors = &state->imgs[BOOT_SECONDARY_SLOT].num_sectors;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400342 break;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200343 case FLASH_AREA_IMAGE_SCRATCH:
344 num_sectors = BOOT_MAX_IMG_SECTORS;
345 out_sectors = state->scratch.sectors;
346 out_num_sectors = &state->scratch.num_sectors;
347 break;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400348 default:
349 return -1;
350 }
351
352 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
353 if (rc != 0) {
354 return rc;
355 }
356 *out_num_sectors = num_sectors;
357 return 0;
358}
359
360#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
361
Christopher Collins92ea77f2016-12-12 15:59:26 -0800362#ifdef __cplusplus
363}
364#endif
365
366#endif