blob: dba19c645b9d4a4eea24598bc1b407d255639275 [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? */
Fabio Utzig46490722017-09-04 15:34:32 -030064 uint32_t swap_size; /* Total size of swapped image */
Fabio Utzigba829042018-09-18 08:29:34 -030065#ifdef MCUBOOT_ENC_IMAGES
66 uint8_t enckey[2][BOOT_ENC_KEY_SIZE];
67#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080068};
69
Fabio Utzig39000012018-07-30 12:40:20 -030070#define BOOT_MAGIC_GOOD 1
71#define BOOT_MAGIC_BAD 2
72#define BOOT_MAGIC_UNSET 3
73#define BOOT_MAGIC_ANY 4 /* NOTE: control only, not dependent on sector */
74
75/*
76 * NOTE: leave BOOT_FLAG_SET equal to one, this is written to flash!
77 */
78#define BOOT_FLAG_SET 1
79#define BOOT_FLAG_BAD 2
80#define BOOT_FLAG_UNSET 3
81#define BOOT_FLAG_ANY 4 /* NOTE: control only, not dependent on sector */
82
83#define BOOT_STATUS_IDX_0 1
84
85#define BOOT_STATUS_STATE_0 1
86#define BOOT_STATUS_STATE_1 2
87#define BOOT_STATUS_STATE_2 3
Christopher Collins92ea77f2016-12-12 15:59:26 -080088
89/**
90 * End-of-image slot structure.
91 *
92 * 0 1 2 3
93 * 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
94 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collins92ea77f2016-12-12 15:59:26 -080095 * ~ ~
96 * ~ Swap status (variable, aligned) ~
97 * ~ ~
98 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig46490722017-09-04 15:34:32 -030099 * | Swap size |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800100 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig39000012018-07-30 12:40:20 -0300101 * ~ padding with erased val (MAX ALIGN - 4) ~
Fabio Utzig46490722017-09-04 15:34:32 -0300102 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig39000012018-07-30 12:40:20 -0300103 * | Copy done | padding with erased val (MAX ALIGN - 1) ~
Fabio Utzig46490722017-09-04 15:34:32 -0300104 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig39000012018-07-30 12:40:20 -0300105 * | Image OK | padding with erased val (MAX ALIGN - 1) ~
Fabio Utzig46490722017-09-04 15:34:32 -0300106 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107 * ~ MAGIC (16 octets) ~
Christopher Collins92ea77f2016-12-12 15:59:26 -0800108 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 */
110
111extern const uint32_t boot_img_magic[4];
112
113struct boot_swap_state {
114 uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */
115 uint8_t copy_done;
116 uint8_t image_ok;
117};
118
Marti Bolivarf9bfddd2018-04-24 14:28:33 -0400119#define BOOT_MAX_IMG_SECTORS MCUBOOT_MAX_IMG_SECTORS
Fabio Utziga1fae672018-03-30 10:52:38 -0300120
121/*
122 * The current flashmap API does not check the amount of space allocated when
123 * loading sector data from the flash device, allowing for smaller counts here
124 * would most surely incur in overruns.
125 *
126 * TODO: make flashmap API receive the current sector array size.
127 */
128#if BOOT_MAX_IMG_SECTORS < 32
129#error "Too few sectors, please increase BOOT_MAX_IMG_SECTORS to at least 32"
130#endif
131
132/** Number of image slots in flash; currently limited to two. */
David Vincze2d736ad2019-02-18 11:50:22 +0100133#define BOOT_NUM_SLOTS 2
Fabio Utziga1fae672018-03-30 10:52:38 -0300134
135/** Maximum number of image sectors supported by the bootloader. */
David Vincze2d736ad2019-02-18 11:50:22 +0100136#define BOOT_STATUS_STATE_COUNT 3
137#define BOOT_STATUS_MAX_ENTRIES BOOT_MAX_IMG_SECTORS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800138
David Vincze2d736ad2019-02-18 11:50:22 +0100139#define BOOT_PRIMARY_SLOT 0
140#define BOOT_SECONDARY_SLOT 1
Christopher Collins92ea77f2016-12-12 15:59:26 -0800141
David Vincze2d736ad2019-02-18 11:50:22 +0100142#define BOOT_STATUS_SOURCE_NONE 0
143#define BOOT_STATUS_SOURCE_SCRATCH 1
144#define BOOT_STATUS_SOURCE_PRIMARY_SLOT 2
145
146#define BOOT_FLAG_IMAGE_OK 0
147#define BOOT_FLAG_COPY_DONE 1
148
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300149extern const uint32_t BOOT_MAGIC_SZ;
150
Marti Bolivarc50926f2017-06-14 09:35:40 -0400151/**
152 * Compatibility shim for flash sector type.
153 *
154 * This can be deleted when flash_area_to_sectors() is removed.
155 */
156#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
157typedef struct flash_sector boot_sector_t;
158#else
159typedef struct flash_area boot_sector_t;
160#endif
161
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400162/** Private state maintained during boot. */
163struct boot_loader_state {
164 struct {
165 struct image_header hdr;
Marti Bolivarc0b47912017-06-13 17:18:09 -0400166 const struct flash_area *area;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400167 boot_sector_t *sectors;
Marti Bolivar84898652017-06-13 17:20:22 -0400168 size_t num_sectors;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400169 } imgs[BOOT_NUM_SLOTS];
170
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200171 struct {
172 const struct flash_area *area;
173 boot_sector_t *sectors;
174 size_t num_sectors;
175 } scratch;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400176
177 uint8_t write_sz;
178};
179
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200180int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
181 size_t slen, uint8_t key_id);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300183uint32_t boot_slots_trailer_sz(uint8_t min_write_sz);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400184int boot_status_entries(const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800185uint32_t boot_status_off(const struct flash_area *fap);
186int boot_read_swap_state(const struct flash_area *fap,
187 struct boot_swap_state *state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300188int boot_read_swap_state_by_id(int flash_area_id,
189 struct boot_swap_state *state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800190int boot_write_magic(const struct flash_area *fap);
191int boot_write_status(struct boot_status *bs);
192int boot_schedule_test_swap(void);
193int boot_write_copy_done(const struct flash_area *fap);
194int boot_write_image_ok(const struct flash_area *fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300195int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
196int boot_read_swap_size(uint32_t *swap_size);
Fabio Utzigba829042018-09-18 08:29:34 -0300197#ifdef MCUBOOT_ENC_IMAGES
198int boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
199 const uint8_t *enckey);
200int boot_read_enc_key(uint8_t slot, uint8_t *enckey);
201#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800202
Marti Bolivarf804f622017-06-12 15:41:48 -0400203/*
204 * Accessors for the contents of struct boot_loader_state.
205 */
206
Marti Bolivarc0b47912017-06-13 17:18:09 -0400207/* These are macros so they can be used as lvalues. */
208#define BOOT_IMG_AREA(state, slot) ((state)->imgs[(slot)].area)
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200209#define BOOT_SCRATCH_AREA(state) ((state)->scratch.area)
Marti Bolivare10a7392017-06-14 16:20:07 -0400210#define BOOT_WRITE_SZ(state) ((state)->write_sz)
Marti Bolivarc0b47912017-06-13 17:18:09 -0400211
Marti Bolivarf804f622017-06-12 15:41:48 -0400212static inline struct image_header*
213boot_img_hdr(struct boot_loader_state *state, size_t slot)
214{
215 return &state->imgs[slot].hdr;
216}
217
Marti Bolivard3269fd2017-06-12 16:31:12 -0400218static inline size_t
219boot_img_num_sectors(struct boot_loader_state *state, size_t slot)
220{
221 return state->imgs[slot].num_sectors;
222}
223
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200224static inline size_t
225boot_scratch_num_sectors(struct boot_loader_state *state)
226{
227 return state->scratch.num_sectors;
228}
229
Marti Bolivar135b8f62017-06-13 17:22:13 -0400230/*
231 * Offset of the slot from the beginning of the flash device.
232 */
233static inline uint32_t
234boot_img_slot_off(struct boot_loader_state *state, size_t slot)
235{
236 return state->imgs[slot].area->fa_off;
237}
238
239static inline size_t boot_scratch_area_size(struct boot_loader_state *state)
240{
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200241 return BOOT_SCRATCH_AREA(state)->fa_size;
Marti Bolivar135b8f62017-06-13 17:22:13 -0400242}
243
Marti Bolivarc50926f2017-06-14 09:35:40 -0400244#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
245
Marti Bolivard3269fd2017-06-12 16:31:12 -0400246static inline size_t
247boot_img_sector_size(struct boot_loader_state *state,
248 size_t slot, size_t sector)
249{
250 return state->imgs[slot].sectors[sector].fa_size;
251}
252
Marti Bolivarea088872017-06-12 17:10:49 -0400253/*
254 * Offset of the sector from the beginning of the image, NOT the flash
255 * device.
256 */
257static inline uint32_t
258boot_img_sector_off(struct boot_loader_state *state, size_t slot,
259 size_t sector)
260{
261 return state->imgs[slot].sectors[sector].fa_off -
262 state->imgs[slot].sectors[0].fa_off;
263}
264
Marti Bolivarcca28a92017-06-12 16:52:22 -0400265static inline int
266boot_initialize_area(struct boot_loader_state *state, int flash_area)
267{
Marti Bolivarcca28a92017-06-12 16:52:22 -0400268 int num_sectors = BOOT_MAX_IMG_SECTORS;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400269 int rc;
270
271 switch (flash_area) {
David Vincze2d736ad2019-02-18 11:50:22 +0100272 case FLASH_AREA_IMAGE_PRIMARY:
273 rc = flash_area_to_sectors(flash_area, &num_sectors,
274 state->imgs[BOOT_PRIMARY_SLOT].sectors);
275 state->imgs[BOOT_PRIMARY_SLOT].num_sectors = (size_t)num_sectors;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400276 break;
David Vincze2d736ad2019-02-18 11:50:22 +0100277 case FLASH_AREA_IMAGE_SECONDARY:
278 rc = flash_area_to_sectors(flash_area, &num_sectors,
279 state->imgs[BOOT_SECONDARY_SLOT].sectors);
280 state->imgs[BOOT_SECONDARY_SLOT].num_sectors = (size_t)num_sectors;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200281 break;
282 case FLASH_AREA_IMAGE_SCRATCH:
David Vincze2d736ad2019-02-18 11:50:22 +0100283 rc = flash_area_to_sectors(flash_area, &num_sectors,
284 state->scratch.sectors);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200285 state->scratch.num_sectors = (size_t)num_sectors;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400286 break;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400287 default:
288 return BOOT_EFLASH;
289 }
290
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200291 return rc;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400292}
293
Marti Bolivarc50926f2017-06-14 09:35:40 -0400294#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
295
296static inline size_t
297boot_img_sector_size(struct boot_loader_state *state,
298 size_t slot, size_t sector)
299{
300 return state->imgs[slot].sectors[sector].fs_size;
301}
302
303static inline uint32_t
304boot_img_sector_off(struct boot_loader_state *state, size_t slot,
305 size_t sector)
306{
307 return state->imgs[slot].sectors[sector].fs_off -
308 state->imgs[slot].sectors[0].fs_off;
309}
310
311static inline int
312boot_initialize_area(struct boot_loader_state *state, int flash_area)
313{
314 uint32_t num_sectors;
315 struct flash_sector *out_sectors;
316 size_t *out_num_sectors;
317 int rc;
318
319 switch (flash_area) {
David Vincze2d736ad2019-02-18 11:50:22 +0100320 case FLASH_AREA_IMAGE_PRIMARY:
Marti Bolivarc50926f2017-06-14 09:35:40 -0400321 num_sectors = BOOT_MAX_IMG_SECTORS;
David Vincze2d736ad2019-02-18 11:50:22 +0100322 out_sectors = state->imgs[BOOT_PRIMARY_SLOT].sectors;
323 out_num_sectors = &state->imgs[BOOT_PRIMARY_SLOT].num_sectors;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400324 break;
David Vincze2d736ad2019-02-18 11:50:22 +0100325 case FLASH_AREA_IMAGE_SECONDARY:
Marti Bolivarc50926f2017-06-14 09:35:40 -0400326 num_sectors = BOOT_MAX_IMG_SECTORS;
David Vincze2d736ad2019-02-18 11:50:22 +0100327 out_sectors = state->imgs[BOOT_SECONDARY_SLOT].sectors;
328 out_num_sectors = &state->imgs[BOOT_SECONDARY_SLOT].num_sectors;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400329 break;
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200330 case FLASH_AREA_IMAGE_SCRATCH:
331 num_sectors = BOOT_MAX_IMG_SECTORS;
332 out_sectors = state->scratch.sectors;
333 out_num_sectors = &state->scratch.num_sectors;
334 break;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400335 default:
336 return -1;
337 }
338
339 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
340 if (rc != 0) {
341 return rc;
342 }
343 *out_num_sectors = num_sectors;
344 return 0;
345}
346
347#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
348
Christopher Collins92ea77f2016-12-12 15:59:26 -0800349#ifdef __cplusplus
350}
351#endif
352
353#endif