blob: 083efc55ec74dab30e818729ef2a93993c165b37 [file] [log] [blame]
Shubham Kulkarni052561d2021-07-20 11:42:44 +05301/*
Gustavo Henrique Nihei523ef3f2021-11-12 17:53:18 -03002 * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
Shubham Kulkarni052561d2021-07-20 11:42:44 +05303 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#include <bootutil/bootutil.h>
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -03008#include <bootutil/bootutil_log.h>
9#include <bootutil/fault_injection_hardening.h>
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053010#include <bootutil/image.h>
11
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -030012#include "bootloader_init.h"
Almir Okato14763b12021-11-25 00:45:26 -030013#include "bootloader_utility.h"
14#include "bootloader_random.h"
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053015
Gustavo Henrique Nihei523ef3f2021-11-12 17:53:18 -030016#if defined(CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH) || defined(CONFIG_SECURE_BOOT)
17#include "esp_efuse.h"
18#endif
19#ifdef CONFIG_SECURE_BOOT
20#include "esp_secure_boot.h"
21#endif
Almir Okato14763b12021-11-25 00:45:26 -030022#ifdef CONFIG_SECURE_FLASH_ENC_ENABLED
23#include "esp_flash_encrypt.h"
24#endif
Gustavo Henrique Nihei523ef3f2021-11-12 17:53:18 -030025
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -030026#include "esp_loader.h"
27#include "os/os_malloc.h"
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053028
Gustavo Henrique Nihei523ef3f2021-11-12 17:53:18 -030029#ifdef CONFIG_SECURE_BOOT
30extern esp_err_t check_and_generate_secure_boot_keys(void);
31#endif
32
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053033void do_boot(struct boot_rsp *rsp)
34{
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -030035 BOOT_LOG_INF("br_image_off = 0x%x", rsp->br_image_off);
36 BOOT_LOG_INF("ih_hdr_size = 0x%x", rsp->br_hdr->ih_hdr_size);
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053037 int slot = (rsp->br_image_off == CONFIG_ESP_APPLICATION_PRIMARY_START_ADDRESS) ? 0 : 1;
38 esp_app_image_load(slot, rsp->br_hdr->ih_hdr_size);
39}
Shubham Kulkarni052561d2021-07-20 11:42:44 +053040
41int main()
42{
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053043 bootloader_init();
Gustavo Henrique Nihei523ef3f2021-11-12 17:53:18 -030044
Almir Okato14763b12021-11-25 00:45:26 -030045 BOOT_LOG_INF("Enabling RNG early entropy source...");
46 bootloader_random_enable();
47
48 /* Rough steps for a first boot when Secure Boot and/or Flash Encryption are still disabled on device:
49 * Secure Boot:
50 * 1) Calculate the SHA-256 hash digest of the public key and write to EFUSE.
51 * 2) Validate the application images and prepare the booting process.
52 * 3) Burn EFUSE to enable Secure Boot V2 (ABS_DONE_0).
53 * Flash Encryption:
54 * 4) Generate Flash Encryption key and write to EFUSE.
55 * 5) Encrypt flash in-place including bootloader, image primary/secondary slot and scratch.
56 * 6) Burn EFUSE to enable Flash Encryption.
57 * 7) Reset system to ensure Flash Encryption cache resets properly.
58 */
59
Gustavo Henrique Nihei523ef3f2021-11-12 17:53:18 -030060#ifdef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
61 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!");
62 esp_efuse_init_virtual_mode_in_flash(CONFIG_EFUSE_VIRTUAL_OFFSET, CONFIG_EFUSE_VIRTUAL_SIZE);
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053063#endif
Shubham Kulkarni052561d2021-07-20 11:42:44 +053064
Gustavo Henrique Nihei523ef3f2021-11-12 17:53:18 -030065#ifdef CONFIG_SECURE_BOOT
Almir Okato14763b12021-11-25 00:45:26 -030066 /* Steps 1 (see above for full description):
67 * 1) Compute digest of the public key.
68 */
69
Gustavo Henrique Nihei523ef3f2021-11-12 17:53:18 -030070 BOOT_LOG_INF("enabling secure boot v2...");
71
72 bool sb_hw_enabled = esp_secure_boot_enabled();
73
74 if (sb_hw_enabled) {
75 BOOT_LOG_INF("secure boot v2 is already enabled, continuing..");
76 } else {
77 esp_efuse_batch_write_begin(); /* Batch all efuse writes at the end of this function */
78
79 esp_err_t err;
80 err = check_and_generate_secure_boot_keys();
81 if (err != ESP_OK) {
82 esp_efuse_batch_write_cancel();
83 FIH_PANIC;
84 }
85 }
86#endif
87
88 BOOT_LOG_INF("*** Booting MCUboot build %s ***", MCUBOOT_VER);
89
Almir Okatoeb6b7bf2021-09-07 17:06:35 -030090 os_heap_init();
91
Gustavo Henrique Nihei523ef3f2021-11-12 17:53:18 -030092 struct boot_rsp rsp;
Almir Okato14763b12021-11-25 00:45:26 -030093
Shubham Kulkarni8787bb02021-07-20 11:46:03 +053094 fih_int fih_rc = FIH_FAILURE;
Gustavo Henrique Nihei523ef3f2021-11-12 17:53:18 -030095
Almir Okato14763b12021-11-25 00:45:26 -030096 /* Step 2 (see above for full description):
97 * 2) MCUboot validates the application images and prepares the booting process.
98 */
99
Shubham Kulkarni8787bb02021-07-20 11:46:03 +0530100 FIH_CALL(boot_go, fih_rc, &rsp);
Gustavo Henrique Nihei523ef3f2021-11-12 17:53:18 -0300101
Shubham Kulkarni8787bb02021-07-20 11:46:03 +0530102 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -0300103 BOOT_LOG_ERR("Unable to find bootable image");
Gustavo Henrique Nihei523ef3f2021-11-12 17:53:18 -0300104#ifdef CONFIG_SECURE_BOOT
105 esp_efuse_batch_write_cancel();
106#endif
Shubham Kulkarni8787bb02021-07-20 11:46:03 +0530107 FIH_PANIC;
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530108 }
Gustavo Henrique Nihei523ef3f2021-11-12 17:53:18 -0300109
110#ifdef CONFIG_SECURE_BOOT
Almir Okato14763b12021-11-25 00:45:26 -0300111 /* Step 3 (see above for full description):
112 * 3) Burn EFUSE to enable Secure Boot V2.
113 */
114
Gustavo Henrique Nihei523ef3f2021-11-12 17:53:18 -0300115 if (!sb_hw_enabled) {
116 BOOT_LOG_INF("blowing secure boot efuse...");
117 esp_err_t err;
118 err = esp_secure_boot_enable_secure_features();
119 if (err != ESP_OK) {
120 esp_efuse_batch_write_cancel();
121 FIH_PANIC;
122 }
123
124 err = esp_efuse_batch_write_commit();
125 if (err != ESP_OK) {
126 BOOT_LOG_ERR("Error programming security eFuses (err=0x%x).", err);
127 FIH_PANIC;
128 }
129
130#ifdef CONFIG_SECURE_BOOT_ENABLE_AGGRESSIVE_KEY_REVOKE
131 assert(esp_efuse_read_field_bit(ESP_EFUSE_SECURE_BOOT_AGGRESSIVE_REVOKE));
132#endif
133
134 assert(esp_secure_boot_enabled());
135 BOOT_LOG_INF("Secure boot permanently enabled");
136 }
137#endif
138
Almir Okato14763b12021-11-25 00:45:26 -0300139#ifdef CONFIG_SECURE_FLASH_ENC_ENABLED
140 /* Step 4, 5 & 6 (see above for full description):
141 * 4) Generate Flash Encryption key and write to EFUSE.
142 * 5) Encrypt flash in-place including bootloader, image primary/secondary slot and scratch.
143 * 6) Burn EFUSE to enable flash encryption
144 */
145
146 int rc;
147
148 BOOT_LOG_INF("Checking flash encryption...");
149 bool flash_encryption_enabled = esp_flash_encryption_enabled();
150 rc = esp_flash_encrypt_check_and_update();
151 if (rc != ESP_OK) {
152 BOOT_LOG_ERR("Flash encryption check failed (%d).", rc);
153 FIH_PANIC;
154 }
155
156 /* Step 7 (see above for full description):
157 * 7) Reset system to ensure flash encryption cache resets properly.
158 */
159 if (!flash_encryption_enabled && esp_flash_encryption_enabled()) {
160 BOOT_LOG_INF("Resetting with flash encryption enabled...");
161 bootloader_reset();
162 }
163#endif
164
165 BOOT_LOG_INF("Disabling RNG early entropy source...");
166 bootloader_random_disable();
167
Shubham Kulkarni8787bb02021-07-20 11:46:03 +0530168 do_boot(&rsp);
Gustavo Henrique Nihei523ef3f2021-11-12 17:53:18 -0300169
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530170 while(1);
171}