blob: 3f0a68d61e6ae76cbed24190262749bd7860a140 [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
Fabio Utzig644b8d42017-04-20 07:56:05 -030043#define BOOT_MAX_ALIGN 8
44
Christopher Collins92ea77f2016-12-12 15:59:26 -080045/*
46 * Maintain state of copy progress.
47 */
48struct boot_status {
Fabio Utzig2473ac02017-05-02 12:45:02 -030049 uint32_t idx; /* Which area we're operating on */
50 uint8_t state; /* Which part of the swapping process are we at */
51 uint8_t use_scratch; /* Are status bytes ever written to scratch? */
Christopher Collins92ea77f2016-12-12 15:59:26 -080052};
53
54#define BOOT_MAGIC_GOOD 1
55#define BOOT_MAGIC_BAD 2
56#define BOOT_MAGIC_UNSET 3
57
58/**
59 * End-of-image slot structure.
60 *
61 * 0 1 2 3
62 * 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
63 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 * ~ MAGIC (16 octets) ~
65 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66 * ~ ~
67 * ~ Swap status (variable, aligned) ~
68 * ~ ~
69 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70 * | Copy done | 0xff padding (up to min-write-sz - 1) ~
71 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72 * | Image OK | 0xff padding (up to min-write-sz - 1) ~
73 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74 */
75
76extern const uint32_t boot_img_magic[4];
77
78struct boot_swap_state {
79 uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */
80 uint8_t copy_done;
81 uint8_t image_ok;
82};
83
84#define BOOT_STATUS_STATE_COUNT 3
85#define BOOT_STATUS_MAX_ENTRIES 128
86
87#define BOOT_STATUS_SOURCE_NONE 0
88#define BOOT_STATUS_SOURCE_SCRATCH 1
89#define BOOT_STATUS_SOURCE_SLOT0 2
90
Fabio Utzig2473ac02017-05-02 12:45:02 -030091#define BOOT_FLAG_IMAGE_OK 0
92#define BOOT_FLAG_COPY_DONE 1
93
Fabio Utzigde8a38a2017-05-23 11:15:01 -040094#define BOOT_FLAG_SET 0x01
95#define BOOT_FLAG_UNSET 0xff
Fabio Utzigba49f842017-05-22 10:52:38 -040096
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030097extern const uint32_t BOOT_MAGIC_SZ;
98
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040099/** Number of image slots in flash; currently limited to two. */
100#define BOOT_NUM_SLOTS 2
101
Marti Bolivarcca28a92017-06-12 16:52:22 -0400102/** Maximum number of image sectors supported by the bootloader. */
103#define BOOT_MAX_IMG_SECTORS 120
104
Marti Bolivarc50926f2017-06-14 09:35:40 -0400105/**
106 * Compatibility shim for flash sector type.
107 *
108 * This can be deleted when flash_area_to_sectors() is removed.
109 */
110#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
111typedef struct flash_sector boot_sector_t;
112#else
113typedef struct flash_area boot_sector_t;
114#endif
115
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400116/** Private state maintained during boot. */
117struct boot_loader_state {
118 struct {
119 struct image_header hdr;
Marti Bolivarc0b47912017-06-13 17:18:09 -0400120 const struct flash_area *area;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400121 boot_sector_t *sectors;
Marti Bolivar84898652017-06-13 17:20:22 -0400122 size_t num_sectors;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400123 } imgs[BOOT_NUM_SLOTS];
124
Marti Bolivarc0b47912017-06-13 17:18:09 -0400125 const struct flash_area *scratch_area;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400126
127 uint8_t write_sz;
128};
129
Christopher Collins92ea77f2016-12-12 15:59:26 -0800130int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen,
131 uint8_t key_id);
132
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300133uint32_t boot_slots_trailer_sz(uint8_t min_write_sz);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400134int boot_status_entries(const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800135uint32_t boot_status_off(const struct flash_area *fap);
136int boot_read_swap_state(const struct flash_area *fap,
137 struct boot_swap_state *state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300138int boot_read_swap_state_by_id(int flash_area_id,
139 struct boot_swap_state *state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800140int boot_write_magic(const struct flash_area *fap);
141int boot_write_status(struct boot_status *bs);
142int boot_schedule_test_swap(void);
143int boot_write_copy_done(const struct flash_area *fap);
144int boot_write_image_ok(const struct flash_area *fap);
145
Marti Bolivarf804f622017-06-12 15:41:48 -0400146/*
147 * Accessors for the contents of struct boot_loader_state.
148 */
149
Marti Bolivarc0b47912017-06-13 17:18:09 -0400150/* These are macros so they can be used as lvalues. */
151#define BOOT_IMG_AREA(state, slot) ((state)->imgs[(slot)].area)
152#define BOOT_SCRATCH_AREA(state) ((state)->scratch_area)
Marti Bolivare10a7392017-06-14 16:20:07 -0400153#define BOOT_WRITE_SZ(state) ((state)->write_sz)
Marti Bolivarc0b47912017-06-13 17:18:09 -0400154
Marti Bolivarf804f622017-06-12 15:41:48 -0400155static inline struct image_header*
156boot_img_hdr(struct boot_loader_state *state, size_t slot)
157{
158 return &state->imgs[slot].hdr;
159}
160
Marti Bolivare2587152017-06-12 15:52:05 -0400161static inline uint8_t
162boot_img_fa_device_id(struct boot_loader_state *state, size_t slot)
163{
Marti Bolivarc0b47912017-06-13 17:18:09 -0400164 return state->imgs[slot].area->fa_device_id;
Marti Bolivare2587152017-06-12 15:52:05 -0400165}
166
167static inline uint8_t
168boot_scratch_fa_device_id(struct boot_loader_state *state)
169{
Marti Bolivarc0b47912017-06-13 17:18:09 -0400170 return state->scratch_area->fa_device_id;
Marti Bolivare2587152017-06-12 15:52:05 -0400171}
172
Marti Bolivard3269fd2017-06-12 16:31:12 -0400173static inline size_t
174boot_img_num_sectors(struct boot_loader_state *state, size_t slot)
175{
176 return state->imgs[slot].num_sectors;
177}
178
Marti Bolivar135b8f62017-06-13 17:22:13 -0400179/*
180 * Offset of the slot from the beginning of the flash device.
181 */
182static inline uint32_t
183boot_img_slot_off(struct boot_loader_state *state, size_t slot)
184{
185 return state->imgs[slot].area->fa_off;
186}
187
188static inline size_t boot_scratch_area_size(struct boot_loader_state *state)
189{
190 return state->scratch_area->fa_size;
191}
192
Marti Bolivarc50926f2017-06-14 09:35:40 -0400193#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
194
Marti Bolivard3269fd2017-06-12 16:31:12 -0400195static inline size_t
196boot_img_sector_size(struct boot_loader_state *state,
197 size_t slot, size_t sector)
198{
199 return state->imgs[slot].sectors[sector].fa_size;
200}
201
Marti Bolivarea088872017-06-12 17:10:49 -0400202/*
203 * Offset of the sector from the beginning of the image, NOT the flash
204 * device.
205 */
206static inline uint32_t
207boot_img_sector_off(struct boot_loader_state *state, size_t slot,
208 size_t sector)
209{
210 return state->imgs[slot].sectors[sector].fa_off -
211 state->imgs[slot].sectors[0].fa_off;
212}
213
Marti Bolivarcca28a92017-06-12 16:52:22 -0400214static inline int
215boot_initialize_area(struct boot_loader_state *state, int flash_area)
216{
Marti Bolivarcca28a92017-06-12 16:52:22 -0400217 int num_sectors = BOOT_MAX_IMG_SECTORS;
218 size_t slot;
219 int rc;
220
221 switch (flash_area) {
222 case FLASH_AREA_IMAGE_0:
223 slot = 0;
224 break;
225 case FLASH_AREA_IMAGE_1:
226 slot = 1;
227 break;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400228 default:
229 return BOOT_EFLASH;
230 }
231
232 rc = flash_area_to_sectors(flash_area, &num_sectors,
233 state->imgs[slot].sectors);
234 if (rc != 0) {
235 return rc;
236 }
Marti Bolivar84898652017-06-13 17:20:22 -0400237 state->imgs[slot].num_sectors = (size_t)num_sectors;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400238 return 0;
239}
240
Marti Bolivarc50926f2017-06-14 09:35:40 -0400241#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
242
243static inline size_t
244boot_img_sector_size(struct boot_loader_state *state,
245 size_t slot, size_t sector)
246{
247 return state->imgs[slot].sectors[sector].fs_size;
248}
249
250static inline uint32_t
251boot_img_sector_off(struct boot_loader_state *state, size_t slot,
252 size_t sector)
253{
254 return state->imgs[slot].sectors[sector].fs_off -
255 state->imgs[slot].sectors[0].fs_off;
256}
257
258static inline int
259boot_initialize_area(struct boot_loader_state *state, int flash_area)
260{
261 uint32_t num_sectors;
262 struct flash_sector *out_sectors;
263 size_t *out_num_sectors;
264 int rc;
265
266 switch (flash_area) {
267 case FLASH_AREA_IMAGE_0:
268 num_sectors = BOOT_MAX_IMG_SECTORS;
269 out_sectors = state->imgs[0].sectors;
270 out_num_sectors = &state->imgs[0].num_sectors;
271 break;
272 case FLASH_AREA_IMAGE_1:
273 num_sectors = BOOT_MAX_IMG_SECTORS;
274 out_sectors = state->imgs[1].sectors;
275 out_num_sectors = &state->imgs[1].num_sectors;
276 break;
277 default:
278 return -1;
279 }
280
281 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
282 if (rc != 0) {
283 return rc;
284 }
285 *out_num_sectors = num_sectors;
286 return 0;
287}
288
289#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
290
Christopher Collins92ea77f2016-12-12 15:59:26 -0800291#ifdef __cplusplus
292}
293#endif
294
295#endif