blob: 8cb753326c73435bfc62bbce86f2ce850afce61c [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
30#ifdef __cplusplus
31extern "C" {
32#endif
33
Marti Bolivar248da082018-04-24 15:11:39 -040034#ifdef MCUBOOT_HAVE_ASSERT_H
35#include "mcuboot_config/mcuboot_assert.h"
Fabio Utzig57c40f72017-12-12 21:48:30 -020036#else
37#define ASSERT assert
38#endif
39
Christopher Collins92ea77f2016-12-12 15:59:26 -080040struct flash_area;
41
42#define BOOT_EFLASH 1
43#define BOOT_EFILE 2
44#define BOOT_EBADIMAGE 3
45#define BOOT_EBADVECT 4
46#define BOOT_EBADSTATUS 5
47#define BOOT_ENOMEM 6
48#define BOOT_EBADARGS 7
49
50#define BOOT_TMPBUF_SZ 256
51
Christopher Collins92ea77f2016-12-12 15:59:26 -080052/*
53 * Maintain state of copy progress.
54 */
55struct boot_status {
Fabio Utzig2473ac02017-05-02 12:45:02 -030056 uint32_t idx; /* Which area we're operating on */
57 uint8_t state; /* Which part of the swapping process are we at */
58 uint8_t use_scratch; /* Are status bytes ever written to scratch? */
Fabio Utzig46490722017-09-04 15:34:32 -030059 uint32_t swap_size; /* Total size of swapped image */
Christopher Collins92ea77f2016-12-12 15:59:26 -080060};
61
Fabio Utzig39000012018-07-30 12:40:20 -030062#define BOOT_MAGIC_GOOD 1
63#define BOOT_MAGIC_BAD 2
64#define BOOT_MAGIC_UNSET 3
65#define BOOT_MAGIC_ANY 4 /* NOTE: control only, not dependent on sector */
66
67/*
68 * NOTE: leave BOOT_FLAG_SET equal to one, this is written to flash!
69 */
70#define BOOT_FLAG_SET 1
71#define BOOT_FLAG_BAD 2
72#define BOOT_FLAG_UNSET 3
73#define BOOT_FLAG_ANY 4 /* NOTE: control only, not dependent on sector */
74
75#define BOOT_STATUS_IDX_0 1
76
77#define BOOT_STATUS_STATE_0 1
78#define BOOT_STATUS_STATE_1 2
79#define BOOT_STATUS_STATE_2 3
Christopher Collins92ea77f2016-12-12 15:59:26 -080080
81/**
82 * End-of-image slot structure.
83 *
84 * 0 1 2 3
85 * 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
86 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collins92ea77f2016-12-12 15:59:26 -080087 * ~ ~
88 * ~ Swap status (variable, aligned) ~
89 * ~ ~
90 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig46490722017-09-04 15:34:32 -030091 * | Swap size |
Christopher Collins92ea77f2016-12-12 15:59:26 -080092 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig39000012018-07-30 12:40:20 -030093 * ~ padding with erased val (MAX ALIGN - 4) ~
Fabio Utzig46490722017-09-04 15:34:32 -030094 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig39000012018-07-30 12:40:20 -030095 * | Copy done | padding with erased val (MAX ALIGN - 1) ~
Fabio Utzig46490722017-09-04 15:34:32 -030096 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig39000012018-07-30 12:40:20 -030097 * | Image OK | padding with erased val (MAX ALIGN - 1) ~
Fabio Utzig46490722017-09-04 15:34:32 -030098 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99 * ~ MAGIC (16 octets) ~
Christopher Collins92ea77f2016-12-12 15:59:26 -0800100 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101 */
102
103extern const uint32_t boot_img_magic[4];
104
105struct boot_swap_state {
106 uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */
107 uint8_t copy_done;
108 uint8_t image_ok;
109};
110
Marti Bolivarf9bfddd2018-04-24 14:28:33 -0400111#define BOOT_MAX_IMG_SECTORS MCUBOOT_MAX_IMG_SECTORS
Fabio Utziga1fae672018-03-30 10:52:38 -0300112
113/*
114 * The current flashmap API does not check the amount of space allocated when
115 * loading sector data from the flash device, allowing for smaller counts here
116 * would most surely incur in overruns.
117 *
118 * TODO: make flashmap API receive the current sector array size.
119 */
120#if BOOT_MAX_IMG_SECTORS < 32
121#error "Too few sectors, please increase BOOT_MAX_IMG_SECTORS to at least 32"
122#endif
123
124/** Number of image slots in flash; currently limited to two. */
125#define BOOT_NUM_SLOTS 2
126
127/** Maximum number of image sectors supported by the bootloader. */
128#define BOOT_STATUS_STATE_COUNT 3
129#define BOOT_STATUS_MAX_ENTRIES BOOT_MAX_IMG_SECTORS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800130
131#define BOOT_STATUS_SOURCE_NONE 0
132#define BOOT_STATUS_SOURCE_SCRATCH 1
133#define BOOT_STATUS_SOURCE_SLOT0 2
134
Fabio Utzig2473ac02017-05-02 12:45:02 -0300135#define BOOT_FLAG_IMAGE_OK 0
136#define BOOT_FLAG_COPY_DONE 1
137
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300138extern const uint32_t BOOT_MAGIC_SZ;
139
Marti Bolivarc50926f2017-06-14 09:35:40 -0400140/**
141 * Compatibility shim for flash sector type.
142 *
143 * This can be deleted when flash_area_to_sectors() is removed.
144 */
145#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
146typedef struct flash_sector boot_sector_t;
147#else
148typedef struct flash_area boot_sector_t;
149#endif
150
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400151/** Private state maintained during boot. */
152struct boot_loader_state {
153 struct {
154 struct image_header hdr;
Marti Bolivarc0b47912017-06-13 17:18:09 -0400155 const struct flash_area *area;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400156 boot_sector_t *sectors;
Marti Bolivar84898652017-06-13 17:20:22 -0400157 size_t num_sectors;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400158 } imgs[BOOT_NUM_SLOTS];
159
Marti Bolivarc0b47912017-06-13 17:18:09 -0400160 const struct flash_area *scratch_area;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400161
162 uint8_t write_sz;
163};
164
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200165int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
166 size_t slen, uint8_t key_id);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800167
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300168uint32_t boot_slots_trailer_sz(uint8_t min_write_sz);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400169int boot_status_entries(const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800170uint32_t boot_status_off(const struct flash_area *fap);
171int boot_read_swap_state(const struct flash_area *fap,
172 struct boot_swap_state *state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300173int boot_read_swap_state_by_id(int flash_area_id,
174 struct boot_swap_state *state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800175int boot_write_magic(const struct flash_area *fap);
176int boot_write_status(struct boot_status *bs);
177int boot_schedule_test_swap(void);
178int boot_write_copy_done(const struct flash_area *fap);
179int boot_write_image_ok(const struct flash_area *fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300180int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
181int boot_read_swap_size(uint32_t *swap_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182
Marti Bolivarf804f622017-06-12 15:41:48 -0400183/*
184 * Accessors for the contents of struct boot_loader_state.
185 */
186
Marti Bolivarc0b47912017-06-13 17:18:09 -0400187/* These are macros so they can be used as lvalues. */
188#define BOOT_IMG_AREA(state, slot) ((state)->imgs[(slot)].area)
189#define BOOT_SCRATCH_AREA(state) ((state)->scratch_area)
Marti Bolivare10a7392017-06-14 16:20:07 -0400190#define BOOT_WRITE_SZ(state) ((state)->write_sz)
Marti Bolivarc0b47912017-06-13 17:18:09 -0400191
Marti Bolivarf804f622017-06-12 15:41:48 -0400192static inline struct image_header*
193boot_img_hdr(struct boot_loader_state *state, size_t slot)
194{
195 return &state->imgs[slot].hdr;
196}
197
Marti Bolivard3269fd2017-06-12 16:31:12 -0400198static inline size_t
199boot_img_num_sectors(struct boot_loader_state *state, size_t slot)
200{
201 return state->imgs[slot].num_sectors;
202}
203
Marti Bolivar135b8f62017-06-13 17:22:13 -0400204/*
205 * Offset of the slot from the beginning of the flash device.
206 */
207static inline uint32_t
208boot_img_slot_off(struct boot_loader_state *state, size_t slot)
209{
210 return state->imgs[slot].area->fa_off;
211}
212
213static inline size_t boot_scratch_area_size(struct boot_loader_state *state)
214{
215 return state->scratch_area->fa_size;
216}
217
Marti Bolivarc50926f2017-06-14 09:35:40 -0400218#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
219
Marti Bolivard3269fd2017-06-12 16:31:12 -0400220static inline size_t
221boot_img_sector_size(struct boot_loader_state *state,
222 size_t slot, size_t sector)
223{
224 return state->imgs[slot].sectors[sector].fa_size;
225}
226
Marti Bolivarea088872017-06-12 17:10:49 -0400227/*
228 * Offset of the sector from the beginning of the image, NOT the flash
229 * device.
230 */
231static inline uint32_t
232boot_img_sector_off(struct boot_loader_state *state, size_t slot,
233 size_t sector)
234{
235 return state->imgs[slot].sectors[sector].fa_off -
236 state->imgs[slot].sectors[0].fa_off;
237}
238
Marti Bolivarcca28a92017-06-12 16:52:22 -0400239static inline int
240boot_initialize_area(struct boot_loader_state *state, int flash_area)
241{
Marti Bolivarcca28a92017-06-12 16:52:22 -0400242 int num_sectors = BOOT_MAX_IMG_SECTORS;
243 size_t slot;
244 int rc;
245
246 switch (flash_area) {
247 case FLASH_AREA_IMAGE_0:
248 slot = 0;
249 break;
250 case FLASH_AREA_IMAGE_1:
251 slot = 1;
252 break;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400253 default:
254 return BOOT_EFLASH;
255 }
256
257 rc = flash_area_to_sectors(flash_area, &num_sectors,
258 state->imgs[slot].sectors);
259 if (rc != 0) {
260 return rc;
261 }
Marti Bolivar84898652017-06-13 17:20:22 -0400262 state->imgs[slot].num_sectors = (size_t)num_sectors;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400263 return 0;
264}
265
Marti Bolivarc50926f2017-06-14 09:35:40 -0400266#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
267
268static inline size_t
269boot_img_sector_size(struct boot_loader_state *state,
270 size_t slot, size_t sector)
271{
272 return state->imgs[slot].sectors[sector].fs_size;
273}
274
275static inline uint32_t
276boot_img_sector_off(struct boot_loader_state *state, size_t slot,
277 size_t sector)
278{
279 return state->imgs[slot].sectors[sector].fs_off -
280 state->imgs[slot].sectors[0].fs_off;
281}
282
283static inline int
284boot_initialize_area(struct boot_loader_state *state, int flash_area)
285{
286 uint32_t num_sectors;
287 struct flash_sector *out_sectors;
288 size_t *out_num_sectors;
289 int rc;
290
291 switch (flash_area) {
292 case FLASH_AREA_IMAGE_0:
293 num_sectors = BOOT_MAX_IMG_SECTORS;
294 out_sectors = state->imgs[0].sectors;
295 out_num_sectors = &state->imgs[0].num_sectors;
296 break;
297 case FLASH_AREA_IMAGE_1:
298 num_sectors = BOOT_MAX_IMG_SECTORS;
299 out_sectors = state->imgs[1].sectors;
300 out_num_sectors = &state->imgs[1].num_sectors;
301 break;
302 default:
303 return -1;
304 }
305
306 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
307 if (rc != 0) {
308 return rc;
309 }
310 *out_num_sectors = num_sectors;
311 return 0;
312}
313
314#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
315
Christopher Collins92ea77f2016-12-12 15:59:26 -0800316#ifdef __cplusplus
317}
318#endif
319
320#endif