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