Shubham Kulkarni | 052561d | 2021-07-20 11:42:44 +0530 | [diff] [blame] | 1 | /* |
Gustavo Henrique Nihei | 523ef3f | 2021-11-12 17:53:18 -0300 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD |
Shubham Kulkarni | 052561d | 2021-07-20 11:42:44 +0530 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
| 7 | #include <bootutil/bootutil.h> |
Gustavo Henrique Nihei | d985d22 | 2021-11-12 14:21:12 -0300 | [diff] [blame] | 8 | #include <bootutil/bootutil_log.h> |
| 9 | #include <bootutil/fault_injection_hardening.h> |
Shubham Kulkarni | 8787bb0 | 2021-07-20 11:46:03 +0530 | [diff] [blame] | 10 | #include <bootutil/image.h> |
| 11 | |
Gustavo Henrique Nihei | d985d22 | 2021-11-12 14:21:12 -0300 | [diff] [blame] | 12 | #include "bootloader_init.h" |
Almir Okato | 14763b1 | 2021-11-25 00:45:26 -0300 | [diff] [blame^] | 13 | #include "bootloader_utility.h" |
| 14 | #include "bootloader_random.h" |
Shubham Kulkarni | 8787bb0 | 2021-07-20 11:46:03 +0530 | [diff] [blame] | 15 | |
Gustavo Henrique Nihei | 523ef3f | 2021-11-12 17:53:18 -0300 | [diff] [blame] | 16 | #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 Okato | 14763b1 | 2021-11-25 00:45:26 -0300 | [diff] [blame^] | 22 | #ifdef CONFIG_SECURE_FLASH_ENC_ENABLED |
| 23 | #include "esp_flash_encrypt.h" |
| 24 | #endif |
Gustavo Henrique Nihei | 523ef3f | 2021-11-12 17:53:18 -0300 | [diff] [blame] | 25 | |
Gustavo Henrique Nihei | d985d22 | 2021-11-12 14:21:12 -0300 | [diff] [blame] | 26 | #include "esp_loader.h" |
| 27 | #include "os/os_malloc.h" |
Shubham Kulkarni | 8787bb0 | 2021-07-20 11:46:03 +0530 | [diff] [blame] | 28 | |
Gustavo Henrique Nihei | 523ef3f | 2021-11-12 17:53:18 -0300 | [diff] [blame] | 29 | #ifdef CONFIG_SECURE_BOOT |
| 30 | extern esp_err_t check_and_generate_secure_boot_keys(void); |
| 31 | #endif |
| 32 | |
Shubham Kulkarni | 8787bb0 | 2021-07-20 11:46:03 +0530 | [diff] [blame] | 33 | void do_boot(struct boot_rsp *rsp) |
| 34 | { |
Gustavo Henrique Nihei | d985d22 | 2021-11-12 14:21:12 -0300 | [diff] [blame] | 35 | 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 Kulkarni | 8787bb0 | 2021-07-20 11:46:03 +0530 | [diff] [blame] | 37 | 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 Kulkarni | 052561d | 2021-07-20 11:42:44 +0530 | [diff] [blame] | 40 | |
| 41 | int main() |
| 42 | { |
Shubham Kulkarni | 8787bb0 | 2021-07-20 11:46:03 +0530 | [diff] [blame] | 43 | bootloader_init(); |
Gustavo Henrique Nihei | 523ef3f | 2021-11-12 17:53:18 -0300 | [diff] [blame] | 44 | |
Almir Okato | 14763b1 | 2021-11-25 00:45:26 -0300 | [diff] [blame^] | 45 | 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 Nihei | 523ef3f | 2021-11-12 17:53:18 -0300 | [diff] [blame] | 60 | #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 Kulkarni | 8787bb0 | 2021-07-20 11:46:03 +0530 | [diff] [blame] | 63 | #endif |
Shubham Kulkarni | 052561d | 2021-07-20 11:42:44 +0530 | [diff] [blame] | 64 | |
Gustavo Henrique Nihei | 523ef3f | 2021-11-12 17:53:18 -0300 | [diff] [blame] | 65 | #ifdef CONFIG_SECURE_BOOT |
Almir Okato | 14763b1 | 2021-11-25 00:45:26 -0300 | [diff] [blame^] | 66 | /* Steps 1 (see above for full description): |
| 67 | * 1) Compute digest of the public key. |
| 68 | */ |
| 69 | |
Gustavo Henrique Nihei | 523ef3f | 2021-11-12 17:53:18 -0300 | [diff] [blame] | 70 | 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 Okato | eb6b7bf | 2021-09-07 17:06:35 -0300 | [diff] [blame] | 90 | os_heap_init(); |
| 91 | |
Gustavo Henrique Nihei | 523ef3f | 2021-11-12 17:53:18 -0300 | [diff] [blame] | 92 | struct boot_rsp rsp; |
Almir Okato | 14763b1 | 2021-11-25 00:45:26 -0300 | [diff] [blame^] | 93 | |
Shubham Kulkarni | 8787bb0 | 2021-07-20 11:46:03 +0530 | [diff] [blame] | 94 | fih_int fih_rc = FIH_FAILURE; |
Gustavo Henrique Nihei | 523ef3f | 2021-11-12 17:53:18 -0300 | [diff] [blame] | 95 | |
Almir Okato | 14763b1 | 2021-11-25 00:45:26 -0300 | [diff] [blame^] | 96 | /* Step 2 (see above for full description): |
| 97 | * 2) MCUboot validates the application images and prepares the booting process. |
| 98 | */ |
| 99 | |
Shubham Kulkarni | 8787bb0 | 2021-07-20 11:46:03 +0530 | [diff] [blame] | 100 | FIH_CALL(boot_go, fih_rc, &rsp); |
Gustavo Henrique Nihei | 523ef3f | 2021-11-12 17:53:18 -0300 | [diff] [blame] | 101 | |
Shubham Kulkarni | 8787bb0 | 2021-07-20 11:46:03 +0530 | [diff] [blame] | 102 | if (fih_not_eq(fih_rc, FIH_SUCCESS)) { |
Gustavo Henrique Nihei | d985d22 | 2021-11-12 14:21:12 -0300 | [diff] [blame] | 103 | BOOT_LOG_ERR("Unable to find bootable image"); |
Gustavo Henrique Nihei | 523ef3f | 2021-11-12 17:53:18 -0300 | [diff] [blame] | 104 | #ifdef CONFIG_SECURE_BOOT |
| 105 | esp_efuse_batch_write_cancel(); |
| 106 | #endif |
Shubham Kulkarni | 8787bb0 | 2021-07-20 11:46:03 +0530 | [diff] [blame] | 107 | FIH_PANIC; |
Shubham Kulkarni | 052561d | 2021-07-20 11:42:44 +0530 | [diff] [blame] | 108 | } |
Gustavo Henrique Nihei | 523ef3f | 2021-11-12 17:53:18 -0300 | [diff] [blame] | 109 | |
| 110 | #ifdef CONFIG_SECURE_BOOT |
Almir Okato | 14763b1 | 2021-11-25 00:45:26 -0300 | [diff] [blame^] | 111 | /* Step 3 (see above for full description): |
| 112 | * 3) Burn EFUSE to enable Secure Boot V2. |
| 113 | */ |
| 114 | |
Gustavo Henrique Nihei | 523ef3f | 2021-11-12 17:53:18 -0300 | [diff] [blame] | 115 | 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 Okato | 14763b1 | 2021-11-25 00:45:26 -0300 | [diff] [blame^] | 139 | #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 Kulkarni | 8787bb0 | 2021-07-20 11:46:03 +0530 | [diff] [blame] | 168 | do_boot(&rsp); |
Gustavo Henrique Nihei | 523ef3f | 2021-11-12 17:53:18 -0300 | [diff] [blame] | 169 | |
Shubham Kulkarni | 052561d | 2021-07-20 11:42:44 +0530 | [diff] [blame] | 170 | while(1); |
| 171 | } |