blob: 1eb5a4a846fef539d577ac70c36518fff0d0e5d3 [file] [log] [blame]
Tamas Banf70ef8c2017-12-19 15:35:09 +00001/*
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
David Vincze39e78552018-10-10 17:10:01 +020020/*
21 * Original code taken from mcuboot project at:
22 * https://github.com/JuulLabs-OSS/mcuboot
23 * Git SHA of the original version: 178be54bd6e5f035cc60e98205535682acd26e64
24 * Modifications are Copyright (c) 2018-2019 Arm Limited.
25 */
26
Tamas Banf70ef8c2017-12-19 15:35:09 +000027#ifndef H_BOOTUTIL_PRIV_
28#define H_BOOTUTIL_PRIV_
29
Tamas Banf70ef8c2017-12-19 15:35:09 +000030#include "flash_map/flash_map.h"
31#include "bootutil/image.h"
Mate Toth-Pala76e2ab2018-05-31 15:43:01 +020032#include "flash_layout.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000033
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38struct flash_area;
39
40#define BOOT_EFLASH 1
41#define BOOT_EFILE 2
42#define BOOT_EBADIMAGE 3
43#define BOOT_EBADVECT 4
44#define BOOT_EBADSTATUS 5
45#define BOOT_ENOMEM 6
46#define BOOT_EBADARGS 7
David Vincze060968d2019-05-23 01:13:14 +020047#define BOOT_EBADMAGIC 8
Tamas Banf70ef8c2017-12-19 15:35:09 +000048
49#define BOOT_TMPBUF_SZ 256
50
51/*
52 * Maintain state of copy progress.
53 */
54struct boot_status {
55 uint32_t idx; /* Which area we're operating on */
56 uint8_t state; /* Which part of the swapping process are we at */
57 uint8_t use_scratch; /* Are status bytes ever written to scratch? */
58 uint32_t swap_size; /* Total size of swapped image */
59};
60
David Vincze39e78552018-10-10 17:10:01 +020061#define BOOT_MAGIC_GOOD 1
62#define BOOT_MAGIC_BAD 2
63#define BOOT_MAGIC_UNSET 3
64#define BOOT_MAGIC_ANY 4 /* NOTE: control only, not dependent on sector */
65
66/*
67 * NOTE: leave BOOT_FLAG_SET equal to one, this is written to flash!
68 */
69#define BOOT_FLAG_SET 1
70#define BOOT_FLAG_BAD 2
71#define BOOT_FLAG_UNSET 3
72#define BOOT_FLAG_ANY 4 /* NOTE: control only, not dependent on sector */
73
74#define BOOT_STATUS_IDX_0 1
75
76#define BOOT_STATUS_STATE_0 1
77#define BOOT_STATUS_STATE_1 2
78#define BOOT_STATUS_STATE_2 3
Tamas Banf70ef8c2017-12-19 15:35:09 +000079
80/**
81 * End-of-image slot structure.
82 *
83 * 0 1 2 3
84 * 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
85 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86 * ~ ~
87 * ~ Swap status (variable, aligned) ~
88 * ~ ~
89 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
90 * | Swap size |
91 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Vincze39e78552018-10-10 17:10:01 +020092 * ~ padding with erased val (MAX ALIGN - 4) ~
Tamas Banf70ef8c2017-12-19 15:35:09 +000093 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Vincze39e78552018-10-10 17:10:01 +020094 * | Copy done | padding with erased val (MAX ALIGN - 1) ~
Tamas Banf70ef8c2017-12-19 15:35:09 +000095 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Vincze39e78552018-10-10 17:10:01 +020096 * | Image OK | padding with erased val (MAX ALIGN - 1) ~
Tamas Banf70ef8c2017-12-19 15:35:09 +000097 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98 * ~ MAGIC (16 octets) ~
99 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100 */
101
102extern const uint32_t boot_img_magic[4];
103
104struct boot_swap_state {
105 uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */
106 uint8_t copy_done;
107 uint8_t image_ok;
108};
109
David Vincze39e78552018-10-10 17:10:01 +0200110/*
111 * The current flashmap API does not check the amount of space allocated when
112 * loading sector data from the flash device, allowing for smaller counts here
113 * would most surely incur in overruns.
114 *
115 * TODO: make flashmap API receive the current sector array size.
116 */
117#if BOOT_MAX_IMG_SECTORS < 32
118#error "Too few sectors, please increase BOOT_MAX_IMG_SECTORS to at least 32"
119#endif
120
121/** Number of image slots in flash; currently limited to two. */
122#define BOOT_NUM_SLOTS 2
123
124/** Maximum number of image sectors supported by the bootloader. */
125#define BOOT_STATUS_STATE_COUNT 3
Tamas Banf70ef8c2017-12-19 15:35:09 +0000126
127#define BOOT_STATUS_SOURCE_NONE 0
128#define BOOT_STATUS_SOURCE_SCRATCH 1
129#define BOOT_STATUS_SOURCE_SLOT0 2
130
131#define BOOT_FLAG_IMAGE_OK 0
132#define BOOT_FLAG_COPY_DONE 1
133
Tamas Banf70ef8c2017-12-19 15:35:09 +0000134extern const uint32_t BOOT_MAGIC_SZ;
135
Tamas Banf70ef8c2017-12-19 15:35:09 +0000136/**
137 * Compatibility shim for flash sector type.
138 *
139 * This can be deleted when flash_area_to_sectors() is removed.
140 */
141#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
142typedef struct flash_sector boot_sector_t;
143#else
144typedef struct flash_area boot_sector_t;
145#endif
146
147/** Private state maintained during boot. */
148struct boot_loader_state {
149 struct {
150 struct image_header hdr;
151 const struct flash_area *area;
152 boot_sector_t *sectors;
153 size_t num_sectors;
154 } imgs[BOOT_NUM_SLOTS];
155
156 const struct flash_area *scratch_area;
157
158 uint8_t write_sz;
159};
160
David Vincze39e78552018-10-10 17:10:01 +0200161int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
162 size_t slen, uint8_t key_id);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000163
164uint32_t boot_slots_trailer_sz(uint8_t min_write_sz);
165int boot_status_entries(const struct flash_area *fap);
166uint32_t boot_status_off(const struct flash_area *fap);
167int boot_read_swap_state(const struct flash_area *fap,
168 struct boot_swap_state *state);
169int boot_read_swap_state_by_id(int flash_area_id,
170 struct boot_swap_state *state);
171int boot_write_magic(const struct flash_area *fap);
172int boot_write_status(struct boot_status *bs);
173int boot_schedule_test_swap(void);
174int boot_write_copy_done(const struct flash_area *fap);
175int boot_write_image_ok(const struct flash_area *fap);
176int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
177int boot_read_swap_size(uint32_t *swap_size);
178
179/*
180 * Accessors for the contents of struct boot_loader_state.
181 */
182
183/* These are macros so they can be used as lvalues. */
184#define BOOT_IMG_AREA(state, slot) ((state)->imgs[(slot)].area)
185#define BOOT_SCRATCH_AREA(state) ((state)->scratch_area)
186#define BOOT_WRITE_SZ(state) ((state)->write_sz)
187
188static inline struct image_header*
189boot_img_hdr(struct boot_loader_state *state, size_t slot)
190{
191 return &state->imgs[slot].hdr;
192}
193
194static inline uint8_t
195boot_img_fa_device_id(struct boot_loader_state *state, size_t slot)
196{
197 return state->imgs[slot].area->fa_device_id;
198}
199
200static inline uint8_t
201boot_scratch_fa_device_id(struct boot_loader_state *state)
202{
203 return state->scratch_area->fa_device_id;
204}
205
206static inline size_t
207boot_img_num_sectors(struct boot_loader_state *state, size_t slot)
208{
209 return state->imgs[slot].num_sectors;
210}
211
212/*
213 * Offset of the slot from the beginning of the flash device.
214 */
215static inline uint32_t
216boot_img_slot_off(struct boot_loader_state *state, size_t slot)
217{
218 return state->imgs[slot].area->fa_off;
219}
220
221static inline size_t boot_scratch_area_size(struct boot_loader_state *state)
222{
223 return state->scratch_area->fa_size;
224}
225
226#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
227
228static inline size_t
229boot_img_sector_size(struct boot_loader_state *state,
230 size_t slot, size_t sector)
231{
232 return state->imgs[slot].sectors[sector].fa_size;
233}
234
235/*
236 * Offset of the sector from the beginning of the image, NOT the flash
237 * device.
238 */
239static inline uint32_t
240boot_img_sector_off(struct boot_loader_state *state, size_t slot,
241 size_t sector)
242{
243 return state->imgs[slot].sectors[sector].fa_off -
244 state->imgs[slot].sectors[0].fa_off;
245}
246
247static inline int
248boot_initialize_area(struct boot_loader_state *state, int flash_area)
249{
250 int num_sectors = BOOT_MAX_IMG_SECTORS;
251 size_t slot;
252 int rc;
253
254 switch (flash_area) {
255 case FLASH_AREA_IMAGE_0:
256 slot = 0;
257 break;
258 case FLASH_AREA_IMAGE_1:
259 slot = 1;
260 break;
261 default:
262 return BOOT_EFLASH;
263 }
264
265 rc = flash_area_to_sectors(flash_area, &num_sectors,
266 state->imgs[slot].sectors);
267 if (rc != 0) {
268 return rc;
269 }
270 state->imgs[slot].num_sectors = (size_t)num_sectors;
271 return 0;
272}
273
274#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
275
276static inline size_t
277boot_img_sector_size(struct boot_loader_state *state,
278 size_t slot, size_t sector)
279{
280 return state->imgs[slot].sectors[sector].fs_size;
281}
282
283static inline uint32_t
284boot_img_sector_off(struct boot_loader_state *state, size_t slot,
285 size_t sector)
286{
287 return state->imgs[slot].sectors[sector].fs_off -
288 state->imgs[slot].sectors[0].fs_off;
289}
290
291static inline int
292boot_initialize_area(struct boot_loader_state *state, int flash_area)
293{
294 uint32_t num_sectors;
295 struct flash_sector *out_sectors;
296 size_t *out_num_sectors;
297 int rc;
298
299 switch (flash_area) {
300 case FLASH_AREA_IMAGE_0:
301 num_sectors = BOOT_MAX_IMG_SECTORS;
302 out_sectors = state->imgs[0].sectors;
303 out_num_sectors = &state->imgs[0].num_sectors;
304 break;
305 case FLASH_AREA_IMAGE_1:
306 num_sectors = BOOT_MAX_IMG_SECTORS;
307 out_sectors = state->imgs[1].sectors;
308 out_num_sectors = &state->imgs[1].num_sectors;
309 break;
310 default:
311 return -1;
312 }
313
314 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
315 if (rc != 0) {
316 return rc;
317 }
318 *out_num_sectors = num_sectors;
319 return 0;
320}
321
322#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
323
324#ifdef __cplusplus
325}
326#endif
327
328#endif