blob: 08e8c831a261b090953e5e23549510f183f4a9d1 [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
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
Christopher Collins92ea77f2016-12-12 15:59:26 -080043/*
44 * Maintain state of copy progress.
45 */
46struct boot_status {
Fabio Utzig2473ac02017-05-02 12:45:02 -030047 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? */
Fabio Utzig46490722017-09-04 15:34:32 -030050 uint32_t swap_size; /* Total size of swapped image */
Christopher Collins92ea77f2016-12-12 15:59:26 -080051};
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collins92ea77f2016-12-12 15:59:26 -080063 * ~ ~
64 * ~ Swap status (variable, aligned) ~
65 * ~ ~
66 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig46490722017-09-04 15:34:32 -030067 * | Swap size |
Christopher Collins92ea77f2016-12-12 15:59:26 -080068 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig46490722017-09-04 15:34:32 -030069 * ~ 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) ~
Christopher Collins92ea77f2016-12-12 15:59:26 -080076 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
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
88#define BOOT_STATUS_MAX_ENTRIES 128
89
90#define BOOT_STATUS_SOURCE_NONE 0
91#define BOOT_STATUS_SOURCE_SCRATCH 1
92#define BOOT_STATUS_SOURCE_SLOT0 2
93
Fabio Utzig2473ac02017-05-02 12:45:02 -030094#define BOOT_FLAG_IMAGE_OK 0
95#define BOOT_FLAG_COPY_DONE 1
96
Fabio Utzigde8a38a2017-05-23 11:15:01 -040097#define BOOT_FLAG_SET 0x01
98#define BOOT_FLAG_UNSET 0xff
Fabio Utzigba49f842017-05-22 10:52:38 -040099
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300100extern const uint32_t BOOT_MAGIC_SZ;
101
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400102/** Number of image slots in flash; currently limited to two. */
103#define BOOT_NUM_SLOTS 2
104
Marti Bolivarcca28a92017-06-12 16:52:22 -0400105/** Maximum number of image sectors supported by the bootloader. */
106#define BOOT_MAX_IMG_SECTORS 120
107
Marti Bolivarc50926f2017-06-14 09:35:40 -0400108/**
109 * Compatibility shim for flash sector type.
110 *
111 * This can be deleted when flash_area_to_sectors() is removed.
112 */
113#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
114typedef struct flash_sector boot_sector_t;
115#else
116typedef struct flash_area boot_sector_t;
117#endif
118
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400119/** Private state maintained during boot. */
120struct boot_loader_state {
121 struct {
122 struct image_header hdr;
Marti Bolivarc0b47912017-06-13 17:18:09 -0400123 const struct flash_area *area;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400124 boot_sector_t *sectors;
Marti Bolivar84898652017-06-13 17:20:22 -0400125 size_t num_sectors;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400126 } imgs[BOOT_NUM_SLOTS];
127
Marti Bolivarc0b47912017-06-13 17:18:09 -0400128 const struct flash_area *scratch_area;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400129
130 uint8_t write_sz;
131};
132
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200133int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
134 size_t slen, uint8_t key_id);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800135
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300136uint32_t boot_slots_trailer_sz(uint8_t min_write_sz);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400137int boot_status_entries(const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800138uint32_t boot_status_off(const struct flash_area *fap);
139int boot_read_swap_state(const struct flash_area *fap,
140 struct boot_swap_state *state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300141int boot_read_swap_state_by_id(int flash_area_id,
142 struct boot_swap_state *state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800143int boot_write_magic(const struct flash_area *fap);
144int boot_write_status(struct boot_status *bs);
145int boot_schedule_test_swap(void);
146int boot_write_copy_done(const struct flash_area *fap);
147int boot_write_image_ok(const struct flash_area *fap);
Fabio Utzig46490722017-09-04 15:34:32 -0300148int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
149int boot_read_swap_size(uint32_t *swap_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800150
Marti Bolivarf804f622017-06-12 15:41:48 -0400151/*
152 * Accessors for the contents of struct boot_loader_state.
153 */
154
Marti Bolivarc0b47912017-06-13 17:18:09 -0400155/* These are macros so they can be used as lvalues. */
156#define BOOT_IMG_AREA(state, slot) ((state)->imgs[(slot)].area)
157#define BOOT_SCRATCH_AREA(state) ((state)->scratch_area)
Marti Bolivare10a7392017-06-14 16:20:07 -0400158#define BOOT_WRITE_SZ(state) ((state)->write_sz)
Marti Bolivarc0b47912017-06-13 17:18:09 -0400159
Marti Bolivarf804f622017-06-12 15:41:48 -0400160static inline struct image_header*
161boot_img_hdr(struct boot_loader_state *state, size_t slot)
162{
163 return &state->imgs[slot].hdr;
164}
165
Marti Bolivare2587152017-06-12 15:52:05 -0400166static inline uint8_t
167boot_img_fa_device_id(struct boot_loader_state *state, size_t slot)
168{
Marti Bolivarc0b47912017-06-13 17:18:09 -0400169 return state->imgs[slot].area->fa_device_id;
Marti Bolivare2587152017-06-12 15:52:05 -0400170}
171
172static inline uint8_t
173boot_scratch_fa_device_id(struct boot_loader_state *state)
174{
Marti Bolivarc0b47912017-06-13 17:18:09 -0400175 return state->scratch_area->fa_device_id;
Marti Bolivare2587152017-06-12 15:52:05 -0400176}
177
Marti Bolivard3269fd2017-06-12 16:31:12 -0400178static inline size_t
179boot_img_num_sectors(struct boot_loader_state *state, size_t slot)
180{
181 return state->imgs[slot].num_sectors;
182}
183
Marti Bolivar135b8f62017-06-13 17:22:13 -0400184/*
185 * Offset of the slot from the beginning of the flash device.
186 */
187static inline uint32_t
188boot_img_slot_off(struct boot_loader_state *state, size_t slot)
189{
190 return state->imgs[slot].area->fa_off;
191}
192
193static inline size_t boot_scratch_area_size(struct boot_loader_state *state)
194{
195 return state->scratch_area->fa_size;
196}
197
Marti Bolivarc50926f2017-06-14 09:35:40 -0400198#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
199
Marti Bolivard3269fd2017-06-12 16:31:12 -0400200static inline size_t
201boot_img_sector_size(struct boot_loader_state *state,
202 size_t slot, size_t sector)
203{
204 return state->imgs[slot].sectors[sector].fa_size;
205}
206
Marti Bolivarea088872017-06-12 17:10:49 -0400207/*
208 * Offset of the sector from the beginning of the image, NOT the flash
209 * device.
210 */
211static inline uint32_t
212boot_img_sector_off(struct boot_loader_state *state, size_t slot,
213 size_t sector)
214{
215 return state->imgs[slot].sectors[sector].fa_off -
216 state->imgs[slot].sectors[0].fa_off;
217}
218
Marti Bolivarcca28a92017-06-12 16:52:22 -0400219static inline int
220boot_initialize_area(struct boot_loader_state *state, int flash_area)
221{
Marti Bolivarcca28a92017-06-12 16:52:22 -0400222 int num_sectors = BOOT_MAX_IMG_SECTORS;
223 size_t slot;
224 int rc;
225
226 switch (flash_area) {
227 case FLASH_AREA_IMAGE_0:
228 slot = 0;
229 break;
230 case FLASH_AREA_IMAGE_1:
231 slot = 1;
232 break;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400233 default:
234 return BOOT_EFLASH;
235 }
236
237 rc = flash_area_to_sectors(flash_area, &num_sectors,
238 state->imgs[slot].sectors);
239 if (rc != 0) {
240 return rc;
241 }
Marti Bolivar84898652017-06-13 17:20:22 -0400242 state->imgs[slot].num_sectors = (size_t)num_sectors;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400243 return 0;
244}
245
Marti Bolivarc50926f2017-06-14 09:35:40 -0400246#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
247
248static inline size_t
249boot_img_sector_size(struct boot_loader_state *state,
250 size_t slot, size_t sector)
251{
252 return state->imgs[slot].sectors[sector].fs_size;
253}
254
255static inline uint32_t
256boot_img_sector_off(struct boot_loader_state *state, size_t slot,
257 size_t sector)
258{
259 return state->imgs[slot].sectors[sector].fs_off -
260 state->imgs[slot].sectors[0].fs_off;
261}
262
263static inline int
264boot_initialize_area(struct boot_loader_state *state, int flash_area)
265{
266 uint32_t num_sectors;
267 struct flash_sector *out_sectors;
268 size_t *out_num_sectors;
269 int rc;
270
271 switch (flash_area) {
272 case FLASH_AREA_IMAGE_0:
273 num_sectors = BOOT_MAX_IMG_SECTORS;
274 out_sectors = state->imgs[0].sectors;
275 out_num_sectors = &state->imgs[0].num_sectors;
276 break;
277 case FLASH_AREA_IMAGE_1:
278 num_sectors = BOOT_MAX_IMG_SECTORS;
279 out_sectors = state->imgs[1].sectors;
280 out_num_sectors = &state->imgs[1].num_sectors;
281 break;
282 default:
283 return -1;
284 }
285
286 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
287 if (rc != 0) {
288 return rc;
289 }
290 *out_num_sectors = num_sectors;
291 return 0;
292}
293
294#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
295
Christopher Collins92ea77f2016-12-12 15:59:26 -0800296#ifdef __cplusplus
297}
298#endif
299
300#endif