blob: 9c3d2583d1f0dda7c41bdfc1e146b0a518ce54c2 [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
20#ifndef H_BOOTUTIL_PRIV_
21#define H_BOOTUTIL_PRIV_
22
Tamas Banf70ef8c2017-12-19 15:35:09 +000023#include "flash_map/flash_map.h"
24#include "bootutil/image.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30struct flash_area;
31
32#define BOOT_EFLASH 1
33#define BOOT_EFILE 2
34#define BOOT_EBADIMAGE 3
35#define BOOT_EBADVECT 4
36#define BOOT_EBADSTATUS 5
37#define BOOT_ENOMEM 6
38#define BOOT_EBADARGS 7
39
40#define BOOT_TMPBUF_SZ 256
41
42/*
43 * Maintain state of copy progress.
44 */
45struct boot_status {
46 uint32_t idx; /* Which area we're operating on */
47 uint8_t state; /* Which part of the swapping process are we at */
48 uint8_t use_scratch; /* Are status bytes ever written to scratch? */
49 uint32_t swap_size; /* Total size of swapped image */
50};
51
52#define BOOT_MAGIC_GOOD 1
53#define BOOT_MAGIC_BAD 2
54#define BOOT_MAGIC_UNSET 3
55
56/**
57 * End-of-image slot structure.
58 *
59 * 0 1 2 3
60 * 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
61 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62 * ~ ~
63 * ~ Swap status (variable, aligned) ~
64 * ~ ~
65 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66 * | Swap size |
67 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68 * ~ 0xff padding (MAX ALIGN - 4) ~
69 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70 * | Copy done | 0xff padding (MAX ALIGN - 1) ~
71 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72 * | Image OK | 0xff padding (MAX ALIGN - 1) ~
73 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74 * ~ MAGIC (16 octets) ~
75 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76 */
77
78extern const uint32_t boot_img_magic[4];
79
80struct boot_swap_state {
81 uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */
82 uint8_t copy_done;
83 uint8_t image_ok;
84};
85
86#define BOOT_STATUS_STATE_COUNT 3
Tamas Banc3828852018-02-01 12:24:16 +000087#define BOOT_STATUS_MAX_ENTRIES 256
Tamas Banf70ef8c2017-12-19 15:35:09 +000088
89#define BOOT_STATUS_SOURCE_NONE 0
90#define BOOT_STATUS_SOURCE_SCRATCH 1
91#define BOOT_STATUS_SOURCE_SLOT0 2
92
93#define BOOT_FLAG_IMAGE_OK 0
94#define BOOT_FLAG_COPY_DONE 1
95
96#define BOOT_FLAG_SET 0x01
97#define BOOT_FLAG_UNSET 0xff
98
99extern const uint32_t BOOT_MAGIC_SZ;
100
101/** Number of image slots in flash; currently limited to two. */
102#define BOOT_NUM_SLOTS 2
103
104/** Maximum number of image sectors supported by the bootloader. */
Tamas Banc3828852018-02-01 12:24:16 +0000105#define BOOT_MAX_IMG_SECTORS 256
Tamas Banf70ef8c2017-12-19 15:35:09 +0000106
107/**
108 * Compatibility shim for flash sector type.
109 *
110 * This can be deleted when flash_area_to_sectors() is removed.
111 */
112#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
113typedef struct flash_sector boot_sector_t;
114#else
115typedef struct flash_area boot_sector_t;
116#endif
117
118/** Private state maintained during boot. */
119struct boot_loader_state {
120 struct {
121 struct image_header hdr;
122 const struct flash_area *area;
123 boot_sector_t *sectors;
124 size_t num_sectors;
125 } imgs[BOOT_NUM_SLOTS];
126
127 const struct flash_area *scratch_area;
128
129 uint8_t write_sz;
130};
131
132int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen,
133 uint8_t key_id);
134
135uint32_t boot_slots_trailer_sz(uint8_t min_write_sz);
136int boot_status_entries(const struct flash_area *fap);
137uint32_t boot_status_off(const struct flash_area *fap);
138int boot_read_swap_state(const struct flash_area *fap,
139 struct boot_swap_state *state);
140int boot_read_swap_state_by_id(int flash_area_id,
141 struct boot_swap_state *state);
142int boot_write_magic(const struct flash_area *fap);
143int boot_write_status(struct boot_status *bs);
144int boot_schedule_test_swap(void);
145int boot_write_copy_done(const struct flash_area *fap);
146int boot_write_image_ok(const struct flash_area *fap);
147int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
148int boot_read_swap_size(uint32_t *swap_size);
149
150/*
151 * Accessors for the contents of struct boot_loader_state.
152 */
153
154/* These are macros so they can be used as lvalues. */
155#define BOOT_IMG_AREA(state, slot) ((state)->imgs[(slot)].area)
156#define BOOT_SCRATCH_AREA(state) ((state)->scratch_area)
157#define BOOT_WRITE_SZ(state) ((state)->write_sz)
158
159static inline struct image_header*
160boot_img_hdr(struct boot_loader_state *state, size_t slot)
161{
162 return &state->imgs[slot].hdr;
163}
164
165static inline uint8_t
166boot_img_fa_device_id(struct boot_loader_state *state, size_t slot)
167{
168 return state->imgs[slot].area->fa_device_id;
169}
170
171static inline uint8_t
172boot_scratch_fa_device_id(struct boot_loader_state *state)
173{
174 return state->scratch_area->fa_device_id;
175}
176
177static inline size_t
178boot_img_num_sectors(struct boot_loader_state *state, size_t slot)
179{
180 return state->imgs[slot].num_sectors;
181}
182
183/*
184 * Offset of the slot from the beginning of the flash device.
185 */
186static inline uint32_t
187boot_img_slot_off(struct boot_loader_state *state, size_t slot)
188{
189 return state->imgs[slot].area->fa_off;
190}
191
192static inline size_t boot_scratch_area_size(struct boot_loader_state *state)
193{
194 return state->scratch_area->fa_size;
195}
196
197#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
198
199static inline size_t
200boot_img_sector_size(struct boot_loader_state *state,
201 size_t slot, size_t sector)
202{
203 return state->imgs[slot].sectors[sector].fa_size;
204}
205
206/*
207 * Offset of the sector from the beginning of the image, NOT the flash
208 * device.
209 */
210static inline uint32_t
211boot_img_sector_off(struct boot_loader_state *state, size_t slot,
212 size_t sector)
213{
214 return state->imgs[slot].sectors[sector].fa_off -
215 state->imgs[slot].sectors[0].fa_off;
216}
217
218static inline int
219boot_initialize_area(struct boot_loader_state *state, int flash_area)
220{
221 int num_sectors = BOOT_MAX_IMG_SECTORS;
222 size_t slot;
223 int rc;
224
225 switch (flash_area) {
226 case FLASH_AREA_IMAGE_0:
227 slot = 0;
228 break;
229 case FLASH_AREA_IMAGE_1:
230 slot = 1;
231 break;
232 default:
233 return BOOT_EFLASH;
234 }
235
236 rc = flash_area_to_sectors(flash_area, &num_sectors,
237 state->imgs[slot].sectors);
238 if (rc != 0) {
239 return rc;
240 }
241 state->imgs[slot].num_sectors = (size_t)num_sectors;
242 return 0;
243}
244
245#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
246
247static inline size_t
248boot_img_sector_size(struct boot_loader_state *state,
249 size_t slot, size_t sector)
250{
251 return state->imgs[slot].sectors[sector].fs_size;
252}
253
254static inline uint32_t
255boot_img_sector_off(struct boot_loader_state *state, size_t slot,
256 size_t sector)
257{
258 return state->imgs[slot].sectors[sector].fs_off -
259 state->imgs[slot].sectors[0].fs_off;
260}
261
262static inline int
263boot_initialize_area(struct boot_loader_state *state, int flash_area)
264{
265 uint32_t num_sectors;
266 struct flash_sector *out_sectors;
267 size_t *out_num_sectors;
268 int rc;
269
270 switch (flash_area) {
271 case FLASH_AREA_IMAGE_0:
272 num_sectors = BOOT_MAX_IMG_SECTORS;
273 out_sectors = state->imgs[0].sectors;
274 out_num_sectors = &state->imgs[0].num_sectors;
275 break;
276 case FLASH_AREA_IMAGE_1:
277 num_sectors = BOOT_MAX_IMG_SECTORS;
278 out_sectors = state->imgs[1].sectors;
279 out_num_sectors = &state->imgs[1].num_sectors;
280 break;
281 default:
282 return -1;
283 }
284
285 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
286 if (rc != 0) {
287 return rc;
288 }
289 *out_num_sectors = num_sectors;
290 return 0;
291}
292
293#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
294
295#ifdef __cplusplus
296}
297#endif
298
299#endif