-
Notifications
You must be signed in to change notification settings - Fork 678
/
main.c
298 lines (245 loc) · 8.93 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <bootutil/bootutil.h>
#include <bootutil/bootutil_log.h>
#include <bootutil/fault_injection_hardening.h>
#include <bootutil/image.h>
#include "bootloader_init.h"
#include "bootloader_utility.h"
#include "bootloader_random.h"
#include "bootloader_soc.h"
#include "esp_assert.h"
#ifdef CONFIG_MCUBOOT_SERIAL
#include "boot_serial/boot_serial.h"
#include "serial_adapter/serial_adapter.h"
const struct boot_uart_funcs boot_funcs = {
.read = console_read,
.write = console_write
};
#endif
#if defined(CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH) || defined(CONFIG_SECURE_BOOT)
#include "esp_efuse.h"
#endif
#ifdef CONFIG_SECURE_BOOT
#include "esp_secure_boot.h"
#endif
#ifdef CONFIG_SECURE_FLASH_ENC_ENABLED
#include "esp_flash_encrypt.h"
#endif
#include "esp_loader.h"
#include "os/os_malloc.h"
#define IMAGE_INDEX_0 0
#define IMAGE_INDEX_1 1
#define PRIMARY_SLOT 0
#define SECONDARY_SLOT 1
#ifdef CONFIG_SECURE_BOOT
extern esp_err_t check_and_generate_secure_boot_keys(void);
#endif
void do_boot(struct boot_rsp *rsp)
{
BOOT_LOG_INF("br_image_off = 0x%x", rsp->br_image_off);
BOOT_LOG_INF("ih_hdr_size = 0x%x", rsp->br_hdr->ih_hdr_size);
int slot = (rsp->br_image_off == CONFIG_ESP_IMAGE0_PRIMARY_START_ADDRESS) ? PRIMARY_SLOT : SECONDARY_SLOT;
start_cpu0_image(IMAGE_INDEX_0, slot, rsp->br_hdr->ih_hdr_size);
}
#ifdef CONFIG_ESP_MULTI_PROCESSOR_BOOT
int read_image_header(uint32_t img_index, uint32_t slot, struct image_header *img_header)
{
const struct flash_area *fap;
int area_id;
int rc = 0;
area_id = flash_area_id_from_multi_image_slot(img_index, slot);
rc = flash_area_open(area_id, &fap);
if (rc != 0) {
rc = BOOT_EFLASH;
goto done;
}
if (flash_area_read(fap, 0, img_header, sizeof(struct image_header))) {
rc = BOOT_EFLASH;
goto done;
}
BOOT_LOG_INF("Image offset = 0x%x", fap->fa_off);
BOOT_LOG_INF("Image header size = 0x%x", img_header->ih_hdr_size);
done:
flash_area_close(fap);
return rc;
}
void do_boot_appcpu(uint32_t img_index, uint32_t slot)
{
struct image_header img_header;
if (read_image_header(img_index, slot, &img_header) != 0) {
FIH_PANIC;
}
start_cpu1_image(img_index, slot, img_header.ih_hdr_size);
}
#endif
int main()
{
if (bootloader_init() != ESP_OK) {
FIH_PANIC;
}
/* Rough steps for a first boot when Secure Boot and/or Flash Encryption are still disabled on device:
* Secure Boot:
* 1) Calculate the SHA-256 hash digest of the public key and write to EFUSE.
* 2) Validate the application images and prepare the booting process.
* 3) Burn EFUSE to enable Secure Boot V2 (ABS_DONE_0).
* Flash Encryption:
* 4) Generate Flash Encryption key and write to EFUSE.
* 5) Encrypt flash in-place including bootloader, image primary/secondary slot and scratch.
* 6) Burn EFUSE to enable Flash Encryption.
* 7) Reset system to ensure Flash Encryption cache resets properly.
*/
#ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
BOOT_LOG_WRN("eFuse virtual mode is enabled. If Secure boot or Flash encryption is enabled then it does not provide any security. FOR TESTING ONLY!");
esp_efuse_init_virtual_mode_in_flash(CONFIG_EFUSE_VIRTUAL_OFFSET, CONFIG_EFUSE_VIRTUAL_SIZE);
#endif
#if defined(CONFIG_SECURE_BOOT) || defined(CONFIG_SECURE_FLASH_ENC_ENABLED)
esp_err_t err;
#endif
#ifdef CONFIG_SECURE_BOOT_FLASH_ENC_KEYS_BURN_TOGETHER
if (esp_secure_boot_enabled() ^ esp_flash_encrypt_initialized_once()) {
BOOT_LOG_ERR("Secure Boot and Flash Encryption cannot be enabled separately, only together (their keys go into one eFuse key block)");
FIH_PANIC;
}
if (!esp_secure_boot_enabled() || !esp_flash_encryption_enabled()) {
esp_efuse_batch_write_begin();
}
#endif // CONFIG_SECURE_BOOT_FLASH_ENC_KEYS_BURN_TOGETHER
#ifdef CONFIG_SECURE_BOOT
/* Steps 1 (see above for full description):
* 1) Compute digest of the public key.
*/
BOOT_LOG_INF("enabling secure boot v2...");
bool sb_hw_enabled = esp_secure_boot_enabled();
if (sb_hw_enabled) {
BOOT_LOG_INF("secure boot v2 is already enabled, continuing..");
} else {
esp_efuse_batch_write_begin(); /* Batch all efuse writes at the end of this function */
err = check_and_generate_secure_boot_keys();
if (err != ESP_OK) {
esp_efuse_batch_write_cancel();
FIH_PANIC;
}
}
#endif
os_heap_init();
struct boot_rsp rsp;
FIH_DECLARE(fih_rc, FIH_FAILURE);
#ifdef CONFIG_MCUBOOT_SERIAL
boot_console_init();
if (boot_serial_detect_pin()) {
BOOT_LOG_INF("Enter the serial recovery mode");
boot_serial_start(&boot_funcs);
}
#endif
/* Step 2 (see above for full description):
* 2) MCUboot validates the application images and prepares the booting process.
*/
/* MCUboot's boot_go validates and checks all images for update and returns
* the load information for booting the main image
*/
FIH_CALL(boot_go, fih_rc, &rsp);
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
BOOT_LOG_ERR("Unable to find bootable image");
#ifdef CONFIG_SECURE_BOOT
esp_efuse_batch_write_cancel();
#endif
FIH_PANIC;
}
#ifdef CONFIG_SECURE_BOOT
/* Step 3 (see above for full description):
* 3) Burn EFUSE to enable Secure Boot V2.
*/
if (!sb_hw_enabled) {
BOOT_LOG_INF("blowing secure boot efuse...");
err = esp_secure_boot_enable_secure_features();
if (err != ESP_OK) {
esp_efuse_batch_write_cancel();
FIH_PANIC;
}
err = esp_efuse_batch_write_commit();
if (err != ESP_OK) {
BOOT_LOG_ERR("Error programming security eFuses (err=0x%x).", err);
FIH_PANIC;
}
#ifdef CONFIG_SECURE_BOOT_ENABLE_AGGRESSIVE_KEY_REVOKE
assert(esp_efuse_read_field_bit(ESP_EFUSE_SECURE_BOOT_AGGRESSIVE_REVOKE));
#endif
#ifndef CONFIG_SECURE_BOOT_FLASH_ENC_KEYS_BURN_TOGETHER
assert(esp_secure_boot_enabled());
BOOT_LOG_INF("Secure boot permanently enabled");
#endif
}
#endif
#ifdef CONFIG_SECURE_FLASH_ENC_ENABLED
/* Step 4, 5 & 6 (see above for full description):
* 4) Generate Flash Encryption key and write to EFUSE.
* 5) Encrypt flash in-place including bootloader, image primary/secondary slot and scratch.
* 6) Burn EFUSE to enable flash encryption
*/
BOOT_LOG_INF("Checking flash encryption...");
bool flash_encryption_enabled = esp_flash_encrypt_state();
if (!flash_encryption_enabled) {
#ifdef CONFIG_SECURE_FLASH_REQUIRE_ALREADY_ENABLED
BOOT_LOG_ERR("flash encryption is not enabled, and SECURE_FLASH_REQUIRE_ALREADY_ENABLED is set, refusing to boot.");
FIH_PANIC;
#endif // CONFIG_SECURE_FLASH_REQUIRE_ALREADY_ENABLED
if (esp_flash_encrypt_is_write_protected(true)) {
FIH_PANIC;
}
err = esp_flash_encrypt_init();
if (err != ESP_OK) {
BOOT_LOG_ERR("Initialization of Flash Encryption key failed (%d)", err);
FIH_PANIC;
}
}
if (!flash_encryption_enabled) {
err = esp_flash_encrypt_contents();
if (err != ESP_OK) {
BOOT_LOG_ERR("Encryption flash contents failed (%d)", err);
FIH_PANIC;
}
err = esp_flash_encrypt_enable();
if (err != ESP_OK) {
BOOT_LOG_ERR("Enabling of Flash encryption failed (%d)", err);
FIH_PANIC;
}
}
#ifdef CONFIG_SECURE_BOOT_FLASH_ENC_KEYS_BURN_TOGETHER
if (!esp_secure_boot_enabled() || !flash_encryption_enabled) {
err = esp_efuse_batch_write_commit();
if (err != ESP_OK) {
BOOT_LOG_ERR("Error programming eFuses (err=0x%x).", err);
FIH_PANIC;
}
assert(esp_secure_boot_enabled());
BOOT_LOG_INF("Secure boot permanently enabled");
}
#endif // CONFIG_SECURE_BOOT_FLASH_ENC_KEYS_BURN_TOGETHER
/* Step 7 (see above for full description):
* 7) Reset system to ensure flash encryption cache resets properly.
*/
if (!flash_encryption_enabled && esp_flash_encryption_enabled()) {
BOOT_LOG_INF("Resetting with flash encryption enabled...");
bootloader_reset();
}
#endif
BOOT_LOG_INF("Disabling RNG early entropy source...");
bootloader_random_disable();
/* Disable glitch reset after all the security checks are completed.
* Glitch detection can be falsely triggered by EMI interference (high RF TX power, etc)
* and to avoid such false alarms, disable it.
*/
bootloader_ana_clock_glitch_reset_config(false);
#ifdef CONFIG_ESP_MULTI_PROCESSOR_BOOT
/* Multi image independent boot
* Boot on the second processor happens before the image0 boot
*/
do_boot_appcpu(IMAGE_INDEX_1, PRIMARY_SLOT);
#endif
do_boot(&rsp);
while(1);
}