flash_encrypt.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <strings.h>
  14. #include "sdkconfig.h"
  15. #include "esp_log.h"
  16. #include "esp_efuse.h"
  17. #include "esp_efuse_table.h"
  18. #include "esp_flash_encrypt.h"
  19. #include "esp_secure_boot.h"
  20. #if CONFIG_IDF_TARGET_ESP32
  21. #define CRYPT_CNT ESP_EFUSE_FLASH_CRYPT_CNT
  22. #define WR_DIS_CRYPT_CNT ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT
  23. #elif CONFIG_IDF_TARGET_ESP32S2
  24. #define CRYPT_CNT ESP_EFUSE_SPI_BOOT_CRYPT_CNT
  25. #define WR_DIS_CRYPT_CNT ESP_EFUSE_WR_DIS_SPI_BOOT_CRYPT_CNT
  26. #endif
  27. #ifndef BOOTLOADER_BUILD
  28. static const char *TAG = "flash_encrypt";
  29. void esp_flash_encryption_init_checks()
  30. {
  31. esp_flash_enc_mode_t mode;
  32. // First check is: if Release mode flash encryption & secure boot are enabled then
  33. // FLASH_CRYPT_CNT *must* be write protected. This will have happened automatically
  34. // if bootloader is IDF V4.0 or newer but may not have happened for previous ESP-IDF bootloaders.
  35. #ifdef CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE
  36. #ifdef CONFIG_SECURE_BOOT
  37. if (esp_secure_boot_enabled() && esp_flash_encryption_enabled()) {
  38. bool flash_crypt_cnt_wr_dis = esp_efuse_read_field_bit(WR_DIS_CRYPT_CNT);
  39. if (!flash_crypt_cnt_wr_dis) {
  40. uint8_t flash_crypt_cnt = 0;
  41. esp_efuse_read_field_blob(CRYPT_CNT, &flash_crypt_cnt, CRYPT_CNT[0]->bit_count);
  42. if (flash_crypt_cnt == (1<<(CRYPT_CNT[0]->bit_count))-1) {
  43. // If encryption counter is already max, no need to write protect it
  44. // (this distinction is important on ESP32 ECO3 where write-procted FLASH_CRYPT_CNT also write-protects UART_DL_DIS)
  45. return;
  46. }
  47. ESP_EARLY_LOGE(TAG, "Flash encryption & Secure Boot together requires FLASH_CRYPT_CNT efuse to be write protected. Fixing now...");
  48. esp_flash_write_protect_crypt_cnt();
  49. }
  50. }
  51. #endif // CONFIG_SECURE_BOOT
  52. #endif // CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE
  53. // Second check is to print a warning or error if the current running flash encryption mode
  54. // doesn't match the expectation from project config (due to mismatched bootloader and app, probably)
  55. mode = esp_get_flash_encryption_mode();
  56. if (mode == ESP_FLASH_ENC_MODE_DEVELOPMENT) {
  57. #ifdef CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE
  58. ESP_EARLY_LOGE(TAG, "Flash encryption settings error: app is configured for RELEASE but efuses are set for DEVELOPMENT");
  59. ESP_EARLY_LOGE(TAG, "Mismatch found in security options in bootloader menuconfig and efuse settings. Device is not secure.");
  60. #else
  61. ESP_EARLY_LOGW(TAG, "Flash encryption mode is DEVELOPMENT (not secure)");
  62. #endif
  63. } else if (mode == ESP_FLASH_ENC_MODE_RELEASE) {
  64. ESP_EARLY_LOGI(TAG, "Flash encryption mode is RELEASE");
  65. }
  66. }
  67. #endif
  68. void esp_flash_write_protect_crypt_cnt(void)
  69. {
  70. esp_efuse_write_field_bit(WR_DIS_CRYPT_CNT);
  71. }
  72. esp_flash_enc_mode_t esp_get_flash_encryption_mode(void)
  73. {
  74. uint8_t efuse_flash_crypt_cnt_wr_protected = 0;
  75. #if CONFIG_IDF_TARGET_ESP32
  76. uint8_t dis_dl_enc = 0, dis_dl_dec = 0, dis_dl_cache = 0;
  77. #elif CONFIG_IDF_TARGET_ESP32S2
  78. uint8_t dis_dl_enc = 0;
  79. uint8_t dis_dl_icache = 0;
  80. uint8_t dis_dl_dcache = 0;
  81. #endif
  82. esp_flash_enc_mode_t mode = ESP_FLASH_ENC_MODE_DEVELOPMENT;
  83. if (esp_flash_encryption_enabled()) {
  84. /* Check if FLASH CRYPT CNT is write protected */
  85. efuse_flash_crypt_cnt_wr_protected = esp_efuse_read_field_bit(WR_DIS_CRYPT_CNT);
  86. if (efuse_flash_crypt_cnt_wr_protected) {
  87. #if CONFIG_IDF_TARGET_ESP32
  88. dis_dl_cache = esp_efuse_read_field_bit(ESP_EFUSE_DISABLE_DL_CACHE);
  89. dis_dl_enc = esp_efuse_read_field_bit(ESP_EFUSE_DISABLE_DL_ENCRYPT);
  90. dis_dl_dec = esp_efuse_read_field_bit(ESP_EFUSE_DISABLE_DL_DECRYPT);
  91. /* Check if DISABLE_DL_DECRYPT, DISABLE_DL_ENCRYPT & DISABLE_DL_CACHE are set */
  92. if ( dis_dl_cache && dis_dl_enc && dis_dl_dec ) {
  93. mode = ESP_FLASH_ENC_MODE_RELEASE;
  94. }
  95. #elif CONFIG_IDF_TARGET_ESP32S2
  96. dis_dl_enc = esp_efuse_read_field_bit(ESP_EFUSE_DIS_DOWNLOAD_MANUAL_ENCRYPT);
  97. dis_dl_icache = esp_efuse_read_field_bit(ESP_EFUSE_DIS_DOWNLOAD_ICACHE);
  98. dis_dl_dcache = esp_efuse_read_field_bit(ESP_EFUSE_DIS_DOWNLOAD_DCACHE);
  99. if (dis_dl_enc && dis_dl_icache && dis_dl_dcache) {
  100. mode = ESP_FLASH_ENC_MODE_RELEASE;
  101. }
  102. #endif
  103. }
  104. } else {
  105. mode = ESP_FLASH_ENC_MODE_DISABLED;
  106. }
  107. return mode;
  108. }