flash_encryption_secure_features.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <strings.h>
  7. #include "esp_flash_encrypt.h"
  8. #include "esp_secure_boot.h"
  9. #include "esp_efuse.h"
  10. #include "esp_efuse_table.h"
  11. #include "esp_log.h"
  12. #include "sdkconfig.h"
  13. static __attribute__((unused)) const char *TAG = "flash_encrypt";
  14. esp_err_t esp_flash_encryption_enable_secure_features(void)
  15. {
  16. #ifndef CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_ENC
  17. ESP_LOGI(TAG, "Disable UART bootloader encryption...");
  18. esp_efuse_write_field_bit(ESP_EFUSE_DIS_DOWNLOAD_MANUAL_ENCRYPT);
  19. #else
  20. ESP_LOGW(TAG, "Not disabling UART bootloader encryption");
  21. #endif
  22. #ifndef CONFIG_SECURE_FLASH_UART_BOOTLOADER_ALLOW_CACHE
  23. ESP_LOGI(TAG, "Disable UART bootloader cache...");
  24. esp_efuse_write_field_bit(ESP_EFUSE_DIS_DOWNLOAD_DCACHE);
  25. esp_efuse_write_field_bit(ESP_EFUSE_DIS_DOWNLOAD_ICACHE);
  26. #else
  27. ESP_LOGW(TAG, "Not disabling UART bootloader cache - SECURITY COMPROMISED");
  28. #endif
  29. #ifndef CONFIG_SECURE_BOOT_ALLOW_JTAG
  30. ESP_LOGI(TAG, "Disable JTAG...");
  31. esp_efuse_write_field_bit(ESP_EFUSE_HARD_DIS_JTAG);
  32. esp_efuse_write_field_bit(ESP_EFUSE_DIS_USB_JTAG);
  33. #else
  34. ESP_LOGW(TAG, "Not disabling JTAG - SECURITY COMPROMISED");
  35. #endif
  36. esp_efuse_write_field_bit(ESP_EFUSE_DIS_LEGACY_SPI_BOOT);
  37. #if defined(CONFIG_SECURE_BOOT_V2_ENABLED) && !defined(CONFIG_SECURE_BOOT_V2_ALLOW_EFUSE_RD_DIS)
  38. // This bit is set when enabling Secure Boot V2, but we can't enable it until this later point in the first boot
  39. // otherwise the Flash Encryption key cannot be read protected
  40. esp_efuse_write_field_bit(ESP_EFUSE_WR_DIS_RD_DIS);
  41. #endif
  42. return ESP_OK;
  43. }