blob: 1889aae6396c898380b9f8d5c1cb26297ad5b4ed [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"
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040024#include "flash_map/flash_map.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080025#include "bootutil/image.h"
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
Fabio Utzig57c40f72017-12-12 21:48:30 -020031#ifdef __BOOTSIM__
32#include "bootsim.h"
33#else
34#define ASSERT assert
35#endif
36
Christopher Collins92ea77f2016-12-12 15:59:26 -080037struct flash_area;
38
39#define BOOT_EFLASH 1
40#define BOOT_EFILE 2
41#define BOOT_EBADIMAGE 3
42#define BOOT_EBADVECT 4
43#define BOOT_EBADSTATUS 5
44#define BOOT_ENOMEM 6
45#define BOOT_EBADARGS 7
46
47#define BOOT_TMPBUF_SZ 256
48
Christopher Collins92ea77f2016-12-12 15:59:26 -080049/*
50 * Maintain state of copy progress.
51 */
52struct boot_status {
Fabio Utzig2473ac02017-05-02 12:45:02 -030053 uint32_t idx; /* Which area we're operating on */
54 uint8_t state; /* Which part of the swapping process are we at */
55 uint8_t use_scratch; /* Are status bytes ever written to scratch? */
Fabio Utzig46490722017-09-04 15:34:32 -030056 uint32_t swap_size; /* Total size of swapped image */
Christopher Collins92ea77f2016-12-12 15:59:26 -080057};
58
59#define BOOT_MAGIC_GOOD 1
60#define BOOT_MAGIC_BAD 2
61#define BOOT_MAGIC_UNSET 3
62
63/**
64 * End-of-image slot structure.
65 *
66 * 0 1 2 3
67 * 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
68 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collins92ea77f2016-12-12 15:59:26 -080069 * ~ ~
70 * ~ Swap status (variable, aligned) ~
71 * ~ ~
72 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig46490722017-09-04 15:34:32 -030073 * | Swap size |
Christopher Collins92ea77f2016-12-12 15:59:26 -080074 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig46490722017-09-04 15:34:32 -030075 * ~ 0xff padding (MAX ALIGN - 4) ~
76 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
77 * | Copy done | 0xff padding (MAX ALIGN - 1) ~
78 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
79 * | Image OK | 0xff padding (MAX ALIGN - 1) ~
80 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
81 * ~ MAGIC (16 octets) ~
Christopher Collins92ea77f2016-12-12 15:59:26 -080082 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83 */
84
85extern const uint32_t boot_img_magic[4];
86
87struct boot_swap_state {
88 uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */
89 uint8_t copy_done;
90 uint8_t image_ok;
91};
92
Fabio Utziga1fae672018-03-30 10:52:38 -030093#if defined(__BOOTSIM__)
94#define BOOT_MAX_IMG_SECTORS 128
95#elif defined(__ZEPHYR__)
96#define BOOT_MAX_IMG_SECTORS CONFIG_BOOT_MAX_IMG_SECTORS
97#elif defined(MCUBOOT_MYNEWT)
98#define BOOT_MAX_IMG_SECTORS MYNEWT_VAL(BOOTUTIL_MAX_IMG_SECTORS)
99#else
100#error "Invalid target OS"
101#endif
102
103/*
104 * The current flashmap API does not check the amount of space allocated when
105 * loading sector data from the flash device, allowing for smaller counts here
106 * would most surely incur in overruns.
107 *
108 * TODO: make flashmap API receive the current sector array size.
109 */
110#if BOOT_MAX_IMG_SECTORS < 32
111#error "Too few sectors, please increase BOOT_MAX_IMG_SECTORS to at least 32"
112#endif
113
114/** Number of image slots in flash; currently limited to two. */
115#define BOOT_NUM_SLOTS 2
116
117/** Maximum number of image sectors supported by the bootloader. */
118#define BOOT_STATUS_STATE_COUNT 3
119#define BOOT_STATUS_MAX_ENTRIES BOOT_MAX_IMG_SECTORS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800120
121#define BOOT_STATUS_SOURCE_NONE 0
122#define BOOT_STATUS_SOURCE_SCRATCH 1
123#define BOOT_STATUS_SOURCE_SLOT0 2
124
Fabio Utzig2473ac02017-05-02 12:45:02 -0300125#define BOOT_FLAG_IMAGE_OK 0
126#define BOOT_FLAG_COPY_DONE 1
127
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400128#define BOOT_FLAG_SET 0x01
129#define BOOT_FLAG_UNSET 0xff
Fabio Utzigba49f842017-05-22 10:52:38 -0400130
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300131extern const uint32_t BOOT_MAGIC_SZ;
132
Marti Bolivarc50926f2017-06-14 09:35:40 -0400133/**
134 * Compatibility shim for flash sector type.
135 *
136 * This can be deleted when flash_area_to_sectors() is removed.
137 */
138#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
139typedef struct flash_sector boot_sector_t;
140#else
141typedef struct flash_area boot_sector_t;
142#endif
143
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400144/** Private state maintained during boot. */
145struct boot_loader_state {
146 struct {
147 struct image_header hdr;
Marti Bolivarc0b47912017-06-13 17:18:09 -0400148 const struct flash_area *area;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400149 boot_sector_t *sectors;
Marti Bolivar84898652017-06-13 17:20:22 -0400150 size_t num_sectors;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400151 } imgs[BOOT_NUM_SLOTS];
152
Marti Bolivarc0b47912017-06-13 17:18:09 -0400153 const struct flash_area *scratch_area;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400154
155 uint8_t write_sz;
156};
157
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200158int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
159 size_t slen, uint8_t key_id);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800160
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300161uint32_t boot_slots_trailer_sz(uint8_t min_write_sz);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400162int boot_status_entries(const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800163uint32_t boot_status_off(const struct flash_area *fap);
164int boot_read_swap_state(const struct flash_area *fap,
165 struct boot_swap_state *state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300166int boot_read_swap_state_by_id(int flash_area_id,
167 struct boot_swap_state *state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800168int boot_write_magic(const struct flash_area *fap);
169int boot_write_status(struct boot_status *bs);
170int boot_schedule_test_swap(void);
171int boot_write_copy_done(const struct flash_area *fap);
172int boot_write_image_ok(const struct flash_area *fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300173int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
174int boot_read_swap_size(uint32_t *swap_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800175
Marti Bolivarf804f622017-06-12 15:41:48 -0400176/*
177 * Accessors for the contents of struct boot_loader_state.
178 */
179
Marti Bolivarc0b47912017-06-13 17:18:09 -0400180/* These are macros so they can be used as lvalues. */
181#define BOOT_IMG_AREA(state, slot) ((state)->imgs[(slot)].area)
182#define BOOT_SCRATCH_AREA(state) ((state)->scratch_area)
Marti Bolivare10a7392017-06-14 16:20:07 -0400183#define BOOT_WRITE_SZ(state) ((state)->write_sz)
Marti Bolivarc0b47912017-06-13 17:18:09 -0400184
Marti Bolivarf804f622017-06-12 15:41:48 -0400185static inline struct image_header*
186boot_img_hdr(struct boot_loader_state *state, size_t slot)
187{
188 return &state->imgs[slot].hdr;
189}
190
Marti Bolivare2587152017-06-12 15:52:05 -0400191static inline uint8_t
192boot_img_fa_device_id(struct boot_loader_state *state, size_t slot)
193{
Marti Bolivarc0b47912017-06-13 17:18:09 -0400194 return state->imgs[slot].area->fa_device_id;
Marti Bolivare2587152017-06-12 15:52:05 -0400195}
196
197static inline uint8_t
198boot_scratch_fa_device_id(struct boot_loader_state *state)
199{
Marti Bolivarc0b47912017-06-13 17:18:09 -0400200 return state->scratch_area->fa_device_id;
Marti Bolivare2587152017-06-12 15:52:05 -0400201}
202
Marti Bolivard3269fd2017-06-12 16:31:12 -0400203static inline size_t
204boot_img_num_sectors(struct boot_loader_state *state, size_t slot)
205{
206 return state->imgs[slot].num_sectors;
207}
208
Marti Bolivar135b8f62017-06-13 17:22:13 -0400209/*
210 * Offset of the slot from the beginning of the flash device.
211 */
212static inline uint32_t
213boot_img_slot_off(struct boot_loader_state *state, size_t slot)
214{
215 return state->imgs[slot].area->fa_off;
216}
217
218static inline size_t boot_scratch_area_size(struct boot_loader_state *state)
219{
220 return state->scratch_area->fa_size;
221}
222
Marti Bolivarc50926f2017-06-14 09:35:40 -0400223#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
224
Marti Bolivard3269fd2017-06-12 16:31:12 -0400225static inline size_t
226boot_img_sector_size(struct boot_loader_state *state,
227 size_t slot, size_t sector)
228{
229 return state->imgs[slot].sectors[sector].fa_size;
230}
231
Marti Bolivarea088872017-06-12 17:10:49 -0400232/*
233 * Offset of the sector from the beginning of the image, NOT the flash
234 * device.
235 */
236static inline uint32_t
237boot_img_sector_off(struct boot_loader_state *state, size_t slot,
238 size_t sector)
239{
240 return state->imgs[slot].sectors[sector].fa_off -
241 state->imgs[slot].sectors[0].fa_off;
242}
243
Marti Bolivarcca28a92017-06-12 16:52:22 -0400244static inline int
245boot_initialize_area(struct boot_loader_state *state, int flash_area)
246{
Marti Bolivarcca28a92017-06-12 16:52:22 -0400247 int num_sectors = BOOT_MAX_IMG_SECTORS;
248 size_t slot;
249 int rc;
250
251 switch (flash_area) {
252 case FLASH_AREA_IMAGE_0:
253 slot = 0;
254 break;
255 case FLASH_AREA_IMAGE_1:
256 slot = 1;
257 break;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400258 default:
259 return BOOT_EFLASH;
260 }
261
262 rc = flash_area_to_sectors(flash_area, &num_sectors,
263 state->imgs[slot].sectors);
264 if (rc != 0) {
265 return rc;
266 }
Marti Bolivar84898652017-06-13 17:20:22 -0400267 state->imgs[slot].num_sectors = (size_t)num_sectors;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400268 return 0;
269}
270
Marti Bolivarc50926f2017-06-14 09:35:40 -0400271#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
272
273static inline size_t
274boot_img_sector_size(struct boot_loader_state *state,
275 size_t slot, size_t sector)
276{
277 return state->imgs[slot].sectors[sector].fs_size;
278}
279
280static inline uint32_t
281boot_img_sector_off(struct boot_loader_state *state, size_t slot,
282 size_t sector)
283{
284 return state->imgs[slot].sectors[sector].fs_off -
285 state->imgs[slot].sectors[0].fs_off;
286}
287
288static inline int
289boot_initialize_area(struct boot_loader_state *state, int flash_area)
290{
291 uint32_t num_sectors;
292 struct flash_sector *out_sectors;
293 size_t *out_num_sectors;
294 int rc;
295
296 switch (flash_area) {
297 case FLASH_AREA_IMAGE_0:
298 num_sectors = BOOT_MAX_IMG_SECTORS;
299 out_sectors = state->imgs[0].sectors;
300 out_num_sectors = &state->imgs[0].num_sectors;
301 break;
302 case FLASH_AREA_IMAGE_1:
303 num_sectors = BOOT_MAX_IMG_SECTORS;
304 out_sectors = state->imgs[1].sectors;
305 out_num_sectors = &state->imgs[1].num_sectors;
306 break;
307 default:
308 return -1;
309 }
310
311 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
312 if (rc != 0) {
313 return rc;
314 }
315 *out_num_sectors = num_sectors;
316 return 0;
317}
318
319#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
320
Christopher Collins92ea77f2016-12-12 15:59:26 -0800321#ifdef __cplusplus
322}
323#endif
324
325#endif