blob: ddfdebd1c7469bf03e2b6cce8e38db337133af94 [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 Bolivar9b1f8bb2017-06-12 15:24:13 -040023#include "flash_map/flash_map.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080024#include "bootutil/image.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30struct flash_area;
31
32#define BOOT_EFLASH 1
33#define BOOT_EFILE 2
34#define BOOT_EBADIMAGE 3
35#define BOOT_EBADVECT 4
36#define BOOT_EBADSTATUS 5
37#define BOOT_ENOMEM 6
38#define BOOT_EBADARGS 7
39
40#define BOOT_TMPBUF_SZ 256
41
Fabio Utzig644b8d42017-04-20 07:56:05 -030042#define BOOT_MAX_ALIGN 8
43
Christopher Collins92ea77f2016-12-12 15:59:26 -080044/*
45 * Maintain state of copy progress.
46 */
47struct boot_status {
Fabio Utzig2473ac02017-05-02 12:45:02 -030048 uint32_t idx; /* Which area we're operating on */
49 uint8_t state; /* Which part of the swapping process are we at */
50 uint8_t use_scratch; /* Are status bytes ever written to scratch? */
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 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 * ~ MAGIC (16 octets) ~
64 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65 * ~ ~
66 * ~ Swap status (variable, aligned) ~
67 * ~ ~
68 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69 * | Copy done | 0xff padding (up to min-write-sz - 1) ~
70 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71 * | Image OK | 0xff padding (up to min-write-sz - 1) ~
72 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73 */
74
75extern const uint32_t boot_img_magic[4];
76
77struct boot_swap_state {
78 uint8_t magic; /* One of the BOOT_MAGIC_[...] values. */
79 uint8_t copy_done;
80 uint8_t image_ok;
81};
82
83#define BOOT_STATUS_STATE_COUNT 3
84#define BOOT_STATUS_MAX_ENTRIES 128
85
86#define BOOT_STATUS_SOURCE_NONE 0
87#define BOOT_STATUS_SOURCE_SCRATCH 1
88#define BOOT_STATUS_SOURCE_SLOT0 2
89
Fabio Utzig2473ac02017-05-02 12:45:02 -030090#define BOOT_FLAG_IMAGE_OK 0
91#define BOOT_FLAG_COPY_DONE 1
92
Fabio Utzigde8a38a2017-05-23 11:15:01 -040093#define BOOT_FLAG_SET 0x01
94#define BOOT_FLAG_UNSET 0xff
Fabio Utzigba49f842017-05-22 10:52:38 -040095
Fabio Utzig7ebb7c22017-04-26 10:59:31 -030096extern const uint32_t BOOT_MAGIC_SZ;
97
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040098/** Number of image slots in flash; currently limited to two. */
99#define BOOT_NUM_SLOTS 2
100
101/** Private state maintained during boot. */
102struct boot_loader_state {
103 struct {
104 struct image_header hdr;
105 struct flash_area *sectors;
106 int num_sectors;
107 } imgs[BOOT_NUM_SLOTS];
108
109 struct flash_area scratch_sector;
110
111 uint8_t write_sz;
112};
113
Christopher Collins92ea77f2016-12-12 15:59:26 -0800114int bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen,
115 uint8_t key_id);
116
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300117uint32_t boot_slots_trailer_sz(uint8_t min_write_sz);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400118int boot_status_entries(const struct flash_area *fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800119uint32_t boot_status_off(const struct flash_area *fap);
120int boot_read_swap_state(const struct flash_area *fap,
121 struct boot_swap_state *state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300122int boot_read_swap_state_by_id(int flash_area_id,
123 struct boot_swap_state *state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800124int boot_write_magic(const struct flash_area *fap);
125int boot_write_status(struct boot_status *bs);
126int boot_schedule_test_swap(void);
127int boot_write_copy_done(const struct flash_area *fap);
128int boot_write_image_ok(const struct flash_area *fap);
129
Marti Bolivarf804f622017-06-12 15:41:48 -0400130/*
131 * Accessors for the contents of struct boot_loader_state.
132 */
133
134static inline struct image_header*
135boot_img_hdr(struct boot_loader_state *state, size_t slot)
136{
137 return &state->imgs[slot].hdr;
138}
139
Marti Bolivare2587152017-06-12 15:52:05 -0400140static inline uint8_t
141boot_img_fa_device_id(struct boot_loader_state *state, size_t slot)
142{
143 return state->imgs[slot].sectors[0].fa_device_id;
144}
145
146static inline uint8_t
147boot_scratch_fa_device_id(struct boot_loader_state *state)
148{
149 return state->scratch_sector.fa_device_id;
150}
151
Marti Bolivard3269fd2017-06-12 16:31:12 -0400152static inline size_t
153boot_img_num_sectors(struct boot_loader_state *state, size_t slot)
154{
155 return state->imgs[slot].num_sectors;
156}
157
158static inline void
159boot_img_set_num_sectors(struct boot_loader_state *state, size_t slot,
160 size_t num_sectors)
161{
162 state->imgs[slot].num_sectors = num_sectors;
163}
164
165static inline size_t
166boot_img_sector_size(struct boot_loader_state *state,
167 size_t slot, size_t sector)
168{
169 return state->imgs[slot].sectors[sector].fa_size;
170}
171
172static inline size_t boot_scratch_area_size(struct boot_loader_state *state)
173{
174 return state->scratch_sector.fa_size;
175}
176
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177#ifdef __cplusplus
178}
179#endif
180
181#endif