blob: 11d0a54dbc064acae5fa388ab4a3e9d97b8e5ec5 [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
David Vinczee2453472019-06-17 12:31:59 +020020/*
21 * Modifications are Copyright (c) 2019 Arm Limited.
22 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024#ifndef H_BOOTUTIL_PRIV_
25#define H_BOOTUTIL_PRIV_
26
Marti Bolivarcca28a92017-06-12 16:52:22 -040027#include "sysflash/sysflash.h"
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020028
29#include <flash_map_backend/flash_map_backend.h>
30
Christopher Collins01dfbb62019-03-21 07:28:37 -070031#include "bootutil/bootutil.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include "bootutil/image.h"
Marti Bolivarf91bca52018-04-12 12:40:46 -040033#include "mcuboot_config/mcuboot_config.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080034
Fabio Utzigba829042018-09-18 08:29:34 -030035#ifdef MCUBOOT_ENC_IMAGES
36#include "bootutil/enc_key.h"
37#endif
38
Christopher Collins92ea77f2016-12-12 15:59:26 -080039#ifdef __cplusplus
40extern "C" {
41#endif
42
Marti Bolivar248da082018-04-24 15:11:39 -040043#ifdef MCUBOOT_HAVE_ASSERT_H
44#include "mcuboot_config/mcuboot_assert.h"
Fabio Utzig57c40f72017-12-12 21:48:30 -020045#else
46#define ASSERT assert
47#endif
48
Christopher Collins92ea77f2016-12-12 15:59:26 -080049struct flash_area;
50
51#define BOOT_EFLASH 1
52#define BOOT_EFILE 2
53#define BOOT_EBADIMAGE 3
54#define BOOT_EBADVECT 4
55#define BOOT_EBADSTATUS 5
56#define BOOT_ENOMEM 6
57#define BOOT_EBADARGS 7
58
59#define BOOT_TMPBUF_SZ 256
60
Christopher Collins92ea77f2016-12-12 15:59:26 -080061/*
62 * Maintain state of copy progress.
63 */
64struct boot_status {
Fabio Utzig2473ac02017-05-02 12:45:02 -030065 uint32_t idx; /* Which area we're operating on */
66 uint8_t state; /* Which part of the swapping process are we at */
67 uint8_t use_scratch; /* Are status bytes ever written to scratch? */
Christopher Collinsa1c12042019-05-23 14:00:28 -070068 uint8_t swap_type; /* The type of swap in effect */
Fabio Utzig46490722017-09-04 15:34:32 -030069 uint32_t swap_size; /* Total size of swapped image */
Fabio Utzigba829042018-09-18 08:29:34 -030070#ifdef MCUBOOT_ENC_IMAGES
71 uint8_t enckey[2][BOOT_ENC_KEY_SIZE];
72#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -080073};
74
Fabio Utzig39000012018-07-30 12:40:20 -030075#define BOOT_MAGIC_GOOD 1
76#define BOOT_MAGIC_BAD 2
77#define BOOT_MAGIC_UNSET 3
78#define BOOT_MAGIC_ANY 4 /* NOTE: control only, not dependent on sector */
Christopher Collinsa1c12042019-05-23 14:00:28 -070079#define BOOT_MAGIC_NOTGOOD 5 /* NOTE: control only, not dependent on sector */
Fabio Utzig39000012018-07-30 12:40:20 -030080
81/*
82 * NOTE: leave BOOT_FLAG_SET equal to one, this is written to flash!
83 */
84#define BOOT_FLAG_SET 1
85#define BOOT_FLAG_BAD 2
86#define BOOT_FLAG_UNSET 3
87#define BOOT_FLAG_ANY 4 /* NOTE: control only, not dependent on sector */
88
89#define BOOT_STATUS_IDX_0 1
90
91#define BOOT_STATUS_STATE_0 1
92#define BOOT_STATUS_STATE_1 2
93#define BOOT_STATUS_STATE_2 3
Christopher Collins92ea77f2016-12-12 15:59:26 -080094
95/**
96 * End-of-image slot structure.
97 *
Christopher Collinsa1c12042019-05-23 14:00:28 -070098 * 0 1 2 3
99 * 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
100 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101 * ~ ~
102 * ~ Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3) ~
103 * ~ ~
104 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 * | Encryption key 0 (16 octets) [*] |
106 * | |
107 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108 * | Encryption key 1 (16 octets) [*] |
109 * | |
110 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111 * | Swap size (4 octets) |
112 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Vinczee2453472019-06-17 12:31:59 +0200113 * | Swap info | 0xff padding (7 octets) |
Christopher Collinsa1c12042019-05-23 14:00:28 -0700114 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115 * | Copy done | 0xff padding (7 octets) |
116 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
117 * | Image OK | 0xff padding (7 octets) |
118 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
119 * | MAGIC (16 octets) |
120 * | |
121 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
122 *
123 * [*]: Only present if the encryption option is enabled
124 * (`MCUBOOT_ENC_IMAGES`).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800125 */
126
127extern const uint32_t boot_img_magic[4];
128
129struct boot_swap_state {
Christopher Collinsa1c12042019-05-23 14:00:28 -0700130 uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */
131 uint8_t swap_type; /* One of the BOOT_SWAP_TYPE_[...] values. */
132 uint8_t copy_done; /* One of the BOOT_FLAG_[...] values. */
133 uint8_t image_ok; /* One of the BOOT_FLAG_[...] values. */
David Vinczee2453472019-06-17 12:31:59 +0200134 uint8_t image_num; /* Boot status belongs to this image */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800135};
136
David Vinczeb75c12a2019-03-22 14:58:33 +0100137#ifdef MCUBOOT_IMAGE_NUMBER
138#define BOOT_IMAGE_NUMBER MCUBOOT_IMAGE_NUMBER
139#else
140#define BOOT_IMAGE_NUMBER 1
141#endif
142
Marti Bolivarf9bfddd2018-04-24 14:28:33 -0400143#define BOOT_MAX_IMG_SECTORS MCUBOOT_MAX_IMG_SECTORS
Fabio Utziga1fae672018-03-30 10:52:38 -0300144
145/*
David Vinczee2453472019-06-17 12:31:59 +0200146 * Extract the swap type and image number from image trailers's swap_info
147 * filed.
148 */
149#define BOOT_GET_SWAP_TYPE(swap_info) ((swap_info) & 0x0F)
150#define BOOT_GET_IMAGE_NUM(swap_info) ((swap_info) >> 4)
151
152/* Construct the swap_info field from swap type and image number */
153#define BOOT_SET_SWAP_INFO(swap_info, image, type) { \
154 assert((image) < 0xF); \
155 assert((type) < 0xF); \
156 (swap_info) = (image) << 4 \
157 | (type); \
158 }
159
160/*
Fabio Utziga1fae672018-03-30 10:52:38 -0300161 * The current flashmap API does not check the amount of space allocated when
162 * loading sector data from the flash device, allowing for smaller counts here
163 * would most surely incur in overruns.
164 *
165 * TODO: make flashmap API receive the current sector array size.
166 */
167#if BOOT_MAX_IMG_SECTORS < 32
168#error "Too few sectors, please increase BOOT_MAX_IMG_SECTORS to at least 32"
169#endif
170
171/** Number of image slots in flash; currently limited to two. */
David Vincze2d736ad2019-02-18 11:50:22 +0100172#define BOOT_NUM_SLOTS 2
Fabio Utziga1fae672018-03-30 10:52:38 -0300173
174/** Maximum number of image sectors supported by the bootloader. */
David Vincze2d736ad2019-02-18 11:50:22 +0100175#define BOOT_STATUS_STATE_COUNT 3
176#define BOOT_STATUS_MAX_ENTRIES BOOT_MAX_IMG_SECTORS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177
David Vincze2d736ad2019-02-18 11:50:22 +0100178#define BOOT_PRIMARY_SLOT 0
179#define BOOT_SECONDARY_SLOT 1
Christopher Collins92ea77f2016-12-12 15:59:26 -0800180
David Vincze2d736ad2019-02-18 11:50:22 +0100181#define BOOT_STATUS_SOURCE_NONE 0
182#define BOOT_STATUS_SOURCE_SCRATCH 1
183#define BOOT_STATUS_SOURCE_PRIMARY_SLOT 2
184
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300185extern const uint32_t BOOT_MAGIC_SZ;
186
Marti Bolivarc50926f2017-06-14 09:35:40 -0400187/**
188 * Compatibility shim for flash sector type.
189 *
190 * This can be deleted when flash_area_to_sectors() is removed.
191 */
192#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
193typedef struct flash_sector boot_sector_t;
194#else
195typedef struct flash_area boot_sector_t;
196#endif
197
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400198/** Private state maintained during boot. */
199struct boot_loader_state {
200 struct {
201 struct image_header hdr;
Marti Bolivarc0b47912017-06-13 17:18:09 -0400202 const struct flash_area *area;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400203 boot_sector_t *sectors;
Marti Bolivar84898652017-06-13 17:20:22 -0400204 size_t num_sectors;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400205 } imgs[BOOT_NUM_SLOTS];
206
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200207 struct {
208 const struct flash_area *area;
209 boot_sector_t *sectors;
210 size_t num_sectors;
211 } scratch;
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -0400212
213 uint8_t write_sz;
214};
215
Fabio Utzig1a927dd2017-12-05 10:30:26 -0200216int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig,
217 size_t slen, uint8_t key_id);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800218
Christopher Collinsa1c12042019-05-23 14:00:28 -0700219int boot_magic_compatible_check(uint8_t tbl_val, uint8_t val);
Christopher Collins2adef702019-05-22 14:37:31 -0700220uint32_t boot_trailer_sz(uint8_t min_write_sz);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400221int boot_status_entries(const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800222uint32_t boot_status_off(const struct flash_area *fap);
David Vinczee2453472019-06-17 12:31:59 +0200223uint32_t boot_swap_info_off(const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800224int boot_read_swap_state(const struct flash_area *fap,
225 struct boot_swap_state *state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300226int boot_read_swap_state_by_id(int flash_area_id,
227 struct boot_swap_state *state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800228int boot_write_magic(const struct flash_area *fap);
229int boot_write_status(struct boot_status *bs);
230int boot_schedule_test_swap(void);
231int boot_write_copy_done(const struct flash_area *fap);
232int boot_write_image_ok(const struct flash_area *fap);
David Vinczee2453472019-06-17 12:31:59 +0200233int boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type,
234 uint8_t image_num);
Fabio Utzig46490722017-09-04 15:34:32 -0300235int boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size);
236int boot_read_swap_size(uint32_t *swap_size);
Fabio Utzigba829042018-09-18 08:29:34 -0300237#ifdef MCUBOOT_ENC_IMAGES
238int boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
239 const uint8_t *enckey);
240int boot_read_enc_key(uint8_t slot, uint8_t *enckey);
241#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800242
Marti Bolivarf804f622017-06-12 15:41:48 -0400243/*
244 * Accessors for the contents of struct boot_loader_state.
245 */
246
Marti Bolivarc0b47912017-06-13 17:18:09 -0400247/* These are macros so they can be used as lvalues. */
248#define BOOT_IMG_AREA(state, slot) ((state)->imgs[(slot)].area)
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200249#define BOOT_SCRATCH_AREA(state) ((state)->scratch.area)
Marti Bolivare10a7392017-06-14 16:20:07 -0400250#define BOOT_WRITE_SZ(state) ((state)->write_sz)
Marti Bolivarc0b47912017-06-13 17:18:09 -0400251
Marti Bolivarf804f622017-06-12 15:41:48 -0400252static inline struct image_header*
253boot_img_hdr(struct boot_loader_state *state, size_t slot)
254{
255 return &state->imgs[slot].hdr;
256}
257
Marti Bolivard3269fd2017-06-12 16:31:12 -0400258static inline size_t
259boot_img_num_sectors(struct boot_loader_state *state, size_t slot)
260{
261 return state->imgs[slot].num_sectors;
262}
263
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200264static inline size_t
265boot_scratch_num_sectors(struct boot_loader_state *state)
266{
267 return state->scratch.num_sectors;
268}
269
Marti Bolivar135b8f62017-06-13 17:22:13 -0400270/*
271 * Offset of the slot from the beginning of the flash device.
272 */
273static inline uint32_t
274boot_img_slot_off(struct boot_loader_state *state, size_t slot)
275{
276 return state->imgs[slot].area->fa_off;
277}
278
279static inline size_t boot_scratch_area_size(struct boot_loader_state *state)
280{
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200281 return BOOT_SCRATCH_AREA(state)->fa_size;
Marti Bolivar135b8f62017-06-13 17:22:13 -0400282}
283
Marti Bolivarc50926f2017-06-14 09:35:40 -0400284#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
285
Marti Bolivard3269fd2017-06-12 16:31:12 -0400286static inline size_t
287boot_img_sector_size(struct boot_loader_state *state,
288 size_t slot, size_t sector)
289{
290 return state->imgs[slot].sectors[sector].fa_size;
291}
292
Marti Bolivarea088872017-06-12 17:10:49 -0400293/*
294 * Offset of the sector from the beginning of the image, NOT the flash
295 * device.
296 */
297static inline uint32_t
298boot_img_sector_off(struct boot_loader_state *state, size_t slot,
299 size_t sector)
300{
301 return state->imgs[slot].sectors[sector].fa_off -
302 state->imgs[slot].sectors[0].fa_off;
303}
304
Marti Bolivarcca28a92017-06-12 16:52:22 -0400305static inline int
306boot_initialize_area(struct boot_loader_state *state, int flash_area)
307{
Marti Bolivarcca28a92017-06-12 16:52:22 -0400308 int num_sectors = BOOT_MAX_IMG_SECTORS;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400309 int rc;
310
David Vinczeb75c12a2019-03-22 14:58:33 +0100311 if (flash_area == FLASH_AREA_IMAGE_PRIMARY) {
David Vincze2d736ad2019-02-18 11:50:22 +0100312 rc = flash_area_to_sectors(flash_area, &num_sectors,
313 state->imgs[BOOT_PRIMARY_SLOT].sectors);
314 state->imgs[BOOT_PRIMARY_SLOT].num_sectors = (size_t)num_sectors;
David Vinczeb75c12a2019-03-22 14:58:33 +0100315
316 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY) {
David Vincze2d736ad2019-02-18 11:50:22 +0100317 rc = flash_area_to_sectors(flash_area, &num_sectors,
318 state->imgs[BOOT_SECONDARY_SLOT].sectors);
319 state->imgs[BOOT_SECONDARY_SLOT].num_sectors = (size_t)num_sectors;
David Vinczeb75c12a2019-03-22 14:58:33 +0100320
321 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
David Vincze2d736ad2019-02-18 11:50:22 +0100322 rc = flash_area_to_sectors(flash_area, &num_sectors,
323 state->scratch.sectors);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200324 state->scratch.num_sectors = (size_t)num_sectors;
David Vinczeb75c12a2019-03-22 14:58:33 +0100325 } else {
Marti Bolivarcca28a92017-06-12 16:52:22 -0400326 return BOOT_EFLASH;
327 }
328
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200329 return rc;
Marti Bolivarcca28a92017-06-12 16:52:22 -0400330}
331
Marti Bolivarc50926f2017-06-14 09:35:40 -0400332#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
333
334static inline size_t
335boot_img_sector_size(struct boot_loader_state *state,
336 size_t slot, size_t sector)
337{
338 return state->imgs[slot].sectors[sector].fs_size;
339}
340
341static inline uint32_t
342boot_img_sector_off(struct boot_loader_state *state, size_t slot,
343 size_t sector)
344{
345 return state->imgs[slot].sectors[sector].fs_off -
346 state->imgs[slot].sectors[0].fs_off;
347}
348
349static inline int
350boot_initialize_area(struct boot_loader_state *state, int flash_area)
351{
352 uint32_t num_sectors;
353 struct flash_sector *out_sectors;
354 size_t *out_num_sectors;
355 int rc;
356
David Vinczeb75c12a2019-03-22 14:58:33 +0100357 num_sectors = BOOT_MAX_IMG_SECTORS;
358
359 if (flash_area == FLASH_AREA_IMAGE_PRIMARY) {
David Vincze2d736ad2019-02-18 11:50:22 +0100360 out_sectors = state->imgs[BOOT_PRIMARY_SLOT].sectors;
361 out_num_sectors = &state->imgs[BOOT_PRIMARY_SLOT].num_sectors;
David Vinczeb75c12a2019-03-22 14:58:33 +0100362 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY) {
David Vincze2d736ad2019-02-18 11:50:22 +0100363 out_sectors = state->imgs[BOOT_SECONDARY_SLOT].sectors;
364 out_num_sectors = &state->imgs[BOOT_SECONDARY_SLOT].num_sectors;
David Vinczeb75c12a2019-03-22 14:58:33 +0100365 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200366 out_sectors = state->scratch.sectors;
367 out_num_sectors = &state->scratch.num_sectors;
David Vinczeb75c12a2019-03-22 14:58:33 +0100368 } else {
369 return BOOT_EFLASH;
Marti Bolivarc50926f2017-06-14 09:35:40 -0400370 }
371
372 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
373 if (rc != 0) {
374 return rc;
375 }
376 *out_num_sectors = num_sectors;
377 return 0;
378}
379
380#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
381
Christopher Collins92ea77f2016-12-12 15:59:26 -0800382#ifdef __cplusplus
383}
384#endif
385
386#endif