blob: c8a1c8ab7850765461c26483a00cb7cf54f0096f [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. */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100122#define BOOT_NUM_SLOTS 2
David Vincze39e78552018-10-10 17:10:01 +0200123
124/** Maximum number of image sectors supported by the bootloader. */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100125#define BOOT_STATUS_STATE_COUNT 3
Tamas Banf70ef8c2017-12-19 15:35:09 +0000126
David Vincze8bdfc2d2019-03-18 15:49:23 +0100127#define BOOT_PRIMARY_SLOT 0
128#define BOOT_SECONDARY_SLOT 1
Tamas Banf70ef8c2017-12-19 15:35:09 +0000129
David Vincze8bdfc2d2019-03-18 15:49:23 +0100130#define BOOT_STATUS_SOURCE_NONE 0
131#define BOOT_STATUS_SOURCE_SCRATCH 1
132#define BOOT_STATUS_SOURCE_PRIMARY_SLOT 2
133
134#define BOOT_FLAG_IMAGE_OK 0
135#define BOOT_FLAG_COPY_DONE 1
Tamas Banf70ef8c2017-12-19 15:35:09 +0000136
Tamas Banf70ef8c2017-12-19 15:35:09 +0000137extern const uint32_t BOOT_MAGIC_SZ;
138
Tamas Banf70ef8c2017-12-19 15:35:09 +0000139/**
140 * Compatibility shim for flash sector type.
141 *
142 * This can be deleted when flash_area_to_sectors() is removed.
143 */
144#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
145typedef struct flash_sector boot_sector_t;
146#else
147typedef struct flash_area boot_sector_t;
148#endif
149
150/** Private state maintained during boot. */
151struct boot_loader_state {
152 struct {
153 struct image_header hdr;
154 const struct flash_area *area;
155 boot_sector_t *sectors;
156 size_t num_sectors;
157 } imgs[BOOT_NUM_SLOTS];
158
159 const struct flash_area *scratch_area;
160
161 uint8_t write_sz;
162};
163
David Vincze39e78552018-10-10 17:10:01 +0200164int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
165 size_t slen, uint8_t key_id);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000166
167uint32_t boot_slots_trailer_sz(uint8_t min_write_sz);
168int boot_status_entries(const struct flash_area *fap);
169uint32_t boot_status_off(const struct flash_area *fap);
170int boot_read_swap_state(const struct flash_area *fap,
171 struct boot_swap_state *state);
172int boot_read_swap_state_by_id(int flash_area_id,
173 struct boot_swap_state *state);
174int boot_write_magic(const struct flash_area *fap);
175int boot_write_status(struct boot_status *bs);
176int boot_schedule_test_swap(void);
177int boot_write_copy_done(const struct flash_area *fap);
178int boot_write_image_ok(const struct flash_area *fap);
179int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
180int boot_read_swap_size(uint32_t *swap_size);
181
182/*
183 * Accessors for the contents of struct boot_loader_state.
184 */
185
186/* These are macros so they can be used as lvalues. */
187#define BOOT_IMG_AREA(state, slot) ((state)->imgs[(slot)].area)
188#define BOOT_SCRATCH_AREA(state) ((state)->scratch_area)
189#define BOOT_WRITE_SZ(state) ((state)->write_sz)
190
191static inline struct image_header*
192boot_img_hdr(struct boot_loader_state *state, size_t slot)
193{
194 return &state->imgs[slot].hdr;
195}
196
197static inline uint8_t
198boot_img_fa_device_id(struct boot_loader_state *state, size_t slot)
199{
200 return state->imgs[slot].area->fa_device_id;
201}
202
203static inline uint8_t
204boot_scratch_fa_device_id(struct boot_loader_state *state)
205{
206 return state->scratch_area->fa_device_id;
207}
208
209static inline size_t
210boot_img_num_sectors(struct boot_loader_state *state, size_t slot)
211{
212 return state->imgs[slot].num_sectors;
213}
214
215/*
216 * Offset of the slot from the beginning of the flash device.
217 */
218static inline uint32_t
219boot_img_slot_off(struct boot_loader_state *state, size_t slot)
220{
221 return state->imgs[slot].area->fa_off;
222}
223
224static inline size_t boot_scratch_area_size(struct boot_loader_state *state)
225{
226 return state->scratch_area->fa_size;
227}
228
229#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
230
231static inline size_t
232boot_img_sector_size(struct boot_loader_state *state,
233 size_t slot, size_t sector)
234{
235 return state->imgs[slot].sectors[sector].fa_size;
236}
237
238/*
239 * Offset of the sector from the beginning of the image, NOT the flash
240 * device.
241 */
242static inline uint32_t
243boot_img_sector_off(struct boot_loader_state *state, size_t slot,
244 size_t sector)
245{
246 return state->imgs[slot].sectors[sector].fa_off -
247 state->imgs[slot].sectors[0].fa_off;
248}
249
250static inline int
251boot_initialize_area(struct boot_loader_state *state, int flash_area)
252{
253 int num_sectors = BOOT_MAX_IMG_SECTORS;
254 size_t slot;
255 int rc;
256
257 switch (flash_area) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100258 case FLASH_AREA_IMAGE_PRIMARY:
259 slot = BOOT_PRIMARY_SLOT;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000260 break;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100261 case FLASH_AREA_IMAGE_SECONDARY:
262 slot = BOOT_SECONDARY_SLOT;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000263 break;
264 default:
265 return BOOT_EFLASH;
266 }
267
268 rc = flash_area_to_sectors(flash_area, &num_sectors,
269 state->imgs[slot].sectors);
270 if (rc != 0) {
271 return rc;
272 }
273 state->imgs[slot].num_sectors = (size_t)num_sectors;
274 return 0;
275}
276
277#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
278
279static inline size_t
280boot_img_sector_size(struct boot_loader_state *state,
281 size_t slot, size_t sector)
282{
283 return state->imgs[slot].sectors[sector].fs_size;
284}
285
286static inline uint32_t
287boot_img_sector_off(struct boot_loader_state *state, size_t slot,
288 size_t sector)
289{
290 return state->imgs[slot].sectors[sector].fs_off -
291 state->imgs[slot].sectors[0].fs_off;
292}
293
294static inline int
295boot_initialize_area(struct boot_loader_state *state, int flash_area)
296{
297 uint32_t num_sectors;
298 struct flash_sector *out_sectors;
299 size_t *out_num_sectors;
300 int rc;
301
302 switch (flash_area) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100303 case FLASH_AREA_IMAGE_PRIMARY:
Tamas Banf70ef8c2017-12-19 15:35:09 +0000304 num_sectors = BOOT_MAX_IMG_SECTORS;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100305 out_sectors = state->imgs[BOOT_PRIMARY_SLOT].sectors;
306 out_num_sectors = &state->imgs[BOOT_PRIMARY_SLOT].num_sectors;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000307 break;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100308 case FLASH_AREA_IMAGE_SECONDARY:
Tamas Banf70ef8c2017-12-19 15:35:09 +0000309 num_sectors = BOOT_MAX_IMG_SECTORS;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100310 out_sectors = state->imgs[BOOT_SECONDARY_SLOT].sectors;
311 out_num_sectors = &state->imgs[BOOT_SECONDARY_SLOT].num_sectors;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000312 break;
313 default:
314 return -1;
315 }
316
317 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
318 if (rc != 0) {
319 return rc;
320 }
321 *out_num_sectors = num_sectors;
322 return 0;
323}
324
325#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
326
327#ifdef __cplusplus
328}
329#endif
330
331#endif