blob: 353033892ecf08872bc6370d701f8f62054ec04a [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? */
Christopher Collins92ea77f2016-12-12 15:59:26 -080050};
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 * ~ MAGIC (16 octets) ~
63 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64 * ~ ~
65 * ~ Swap status (variable, aligned) ~
66 * ~ ~
67 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68 * | Copy done | 0xff padding (up to min-write-sz - 1) ~
69 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
70 * | Image OK | 0xff padding (up to min-write-sz - 1) ~
71 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72 */
73
74extern const uint32_t boot_img_magic[4];
75
76struct boot_swap_state {
77 uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */
78 uint8_t copy_done;
79 uint8_t image_ok;
80};
81
82#define BOOT_STATUS_STATE_COUNT 3
83#define BOOT_STATUS_MAX_ENTRIES 128
84
85#define BOOT_STATUS_SOURCE_NONE 0
86#define BOOT_STATUS_SOURCE_SCRATCH 1
87#define BOOT_STATUS_SOURCE_SLOT0 2
88
Fabio Utzig2473ac02017-05-02 12:45:02 -030089#define BOOT_FLAG_IMAGE_OK 0
90#define BOOT_FLAG_COPY_DONE 1
91
Fabio Utzigde8a38a2017-05-23 11:15:01 -040092#define BOOT_FLAG_SET 0x01
93#define BOOT_FLAG_UNSET 0xff
Fabio Utzigba49f842017-05-22 10:52:38 -040094
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030095extern const uint32_t BOOT_MAGIC_SZ;
96
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040097/** Number of image slots in flash; currently limited to two. */
98#define BOOT_NUM_SLOTS 2
99
Marti Bolivarcca28a92017-06-12 16:52:22 -0400100/** Maximum number of image sectors supported by the bootloader. */
101#define BOOT_MAX_IMG_SECTORS 120
102
Marti Bolivarc50926f2017-06-14 09:35:40 -0400103/**
104 * Compatibility shim for flash sector type.
105 *
106 * This can be deleted when flash_area_to_sectors() is removed.
107 */
108#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
109typedef struct flash_sector boot_sector_t;
110#else
111typedef struct flash_area boot_sector_t;
112#endif
113
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400114/** Private state maintained during boot. */
115struct boot_loader_state {
116 struct {
117 struct image_header hdr;
Marti Bolivarc0b47912017-06-13 17:18:09 -0400118 const struct flash_area *area;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400119 boot_sector_t *sectors;
Marti Bolivar84898652017-06-13 17:20:22 -0400120 size_t num_sectors;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400121 } imgs[BOOT_NUM_SLOTS];
122
Marti Bolivarc0b47912017-06-13 17:18:09 -0400123 const struct flash_area *scratch_area;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400124
125 uint8_t write_sz;
126};
127
Christopher Collins92ea77f2016-12-12 15:59:26 -0800128int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen,
129 uint8_t key_id);
130
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300131uint32_t boot_slots_trailer_sz(uint8_t min_write_sz);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400132int boot_status_entries(const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800133uint32_t boot_status_off(const struct flash_area *fap);
134int boot_read_swap_state(const struct flash_area *fap,
135 struct boot_swap_state *state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300136int boot_read_swap_state_by_id(int flash_area_id,
137 struct boot_swap_state *state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800138int boot_write_magic(const struct flash_area *fap);
139int boot_write_status(struct boot_status *bs);
140int boot_schedule_test_swap(void);
141int boot_write_copy_done(const struct flash_area *fap);
142int boot_write_image_ok(const struct flash_area *fap);
143
Marti Bolivarf804f622017-06-12 15:41:48 -0400144/*
145 * Accessors for the contents of struct boot_loader_state.
146 */
147
Marti Bolivarc0b47912017-06-13 17:18:09 -0400148/* These are macros so they can be used as lvalues. */
149#define BOOT_IMG_AREA(state, slot) ((state)->imgs[(slot)].area)
150#define BOOT_SCRATCH_AREA(state) ((state)->scratch_area)
Marti Bolivare10a7392017-06-14 16:20:07 -0400151#define BOOT_WRITE_SZ(state) ((state)->write_sz)
Marti Bolivarc0b47912017-06-13 17:18:09 -0400152
Marti Bolivarf804f622017-06-12 15:41:48 -0400153static inline struct image_header*
154boot_img_hdr(struct boot_loader_state *state, size_t slot)
155{
156 return &state->imgs[slot].hdr;
157}
158
Marti Bolivare2587152017-06-12 15:52:05 -0400159static inline uint8_t
160boot_img_fa_device_id(struct boot_loader_state *state, size_t slot)
161{
Marti Bolivarc0b47912017-06-13 17:18:09 -0400162 return state->imgs[slot].area->fa_device_id;
Marti Bolivare2587152017-06-12 15:52:05 -0400163}
164
165static inline uint8_t
166boot_scratch_fa_device_id(struct boot_loader_state *state)
167{
Marti Bolivarc0b47912017-06-13 17:18:09 -0400168 return state->scratch_area->fa_device_id;
Marti Bolivare2587152017-06-12 15:52:05 -0400169}
170
Marti Bolivard3269fd2017-06-12 16:31:12 -0400171static inline size_t
172boot_img_num_sectors(struct boot_loader_state *state, size_t slot)
173{
174 return state->imgs[slot].num_sectors;
175}
176
Marti Bolivar135b8f62017-06-13 17:22:13 -0400177/*
178 * Offset of the slot from the beginning of the flash device.
179 */
180static inline uint32_t
181boot_img_slot_off(struct boot_loader_state *state, size_t slot)
182{
183 return state->imgs[slot].area->fa_off;
184}
185
186static inline size_t boot_scratch_area_size(struct boot_loader_state *state)
187{
188 return state->scratch_area->fa_size;
189}
190
Marti Bolivarc50926f2017-06-14 09:35:40 -0400191#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
192
Marti Bolivard3269fd2017-06-12 16:31:12 -0400193static inline size_t
194boot_img_sector_size(struct boot_loader_state *state,
195 size_t slot, size_t sector)
196{
197 return state->imgs[slot].sectors[sector].fa_size;
198}
199
Marti Bolivarea088872017-06-12 17:10:49 -0400200/*
201 * Offset of the sector from the beginning of the image, NOT the flash
202 * device.
203 */
204static inline uint32_t
205boot_img_sector_off(struct boot_loader_state *state, size_t slot,
206 size_t sector)
207{
208 return state->imgs[slot].sectors[sector].fa_off -
209 state->imgs[slot].sectors[0].fa_off;
210}
211
Marti Bolivarcca28a92017-06-12 16:52:22 -0400212static inline int
213boot_initialize_area(struct boot_loader_state *state, int flash_area)
214{
Marti Bolivarcca28a92017-06-12 16:52:22 -0400215 int num_sectors = BOOT_MAX_IMG_SECTORS;
216 size_t slot;
217 int rc;
218
219 switch (flash_area) {
220 case FLASH_AREA_IMAGE_0:
221 slot = 0;
222 break;
223 case FLASH_AREA_IMAGE_1:
224 slot = 1;
225 break;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400226 default:
227 return BOOT_EFLASH;
228 }
229
230 rc = flash_area_to_sectors(flash_area, &num_sectors,
231 state->imgs[slot].sectors);
232 if (rc != 0) {
233 return rc;
234 }
Marti Bolivar84898652017-06-13 17:20:22 -0400235 state->imgs[slot].num_sectors = (size_t)num_sectors;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400236 return 0;
237}
238
Marti Bolivarc50926f2017-06-14 09:35:40 -0400239#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
240
241static inline size_t
242boot_img_sector_size(struct boot_loader_state *state,
243 size_t slot, size_t sector)
244{
245 return state->imgs[slot].sectors[sector].fs_size;
246}
247
248static inline uint32_t
249boot_img_sector_off(struct boot_loader_state *state, size_t slot,
250 size_t sector)
251{
252 return state->imgs[slot].sectors[sector].fs_off -
253 state->imgs[slot].sectors[0].fs_off;
254}
255
256static inline int
257boot_initialize_area(struct boot_loader_state *state, int flash_area)
258{
259 uint32_t num_sectors;
260 struct flash_sector *out_sectors;
261 size_t *out_num_sectors;
262 int rc;
263
264 switch (flash_area) {
265 case FLASH_AREA_IMAGE_0:
266 num_sectors = BOOT_MAX_IMG_SECTORS;
267 out_sectors = state->imgs[0].sectors;
268 out_num_sectors = &state->imgs[0].num_sectors;
269 break;
270 case FLASH_AREA_IMAGE_1:
271 num_sectors = BOOT_MAX_IMG_SECTORS;
272 out_sectors = state->imgs[1].sectors;
273 out_num_sectors = &state->imgs[1].num_sectors;
274 break;
275 default:
276 return -1;
277 }
278
279 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
280 if (rc != 0) {
281 return rc;
282 }
283 *out_num_sectors = num_sectors;
284 return 0;
285}
286
287#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
288
Christopher Collins92ea77f2016-12-12 15:59:26 -0800289#ifdef __cplusplus
290}
291#endif
292
293#endif