flash_encryption_secure_features.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 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_BOOT_ALLOW_JTAG
  23. ESP_LOGI(TAG, "Disable JTAG...");
  24. esp_efuse_write_field_bit(ESP_EFUSE_DIS_PAD_JTAG);
  25. esp_efuse_write_field_bit(ESP_EFUSE_DIS_USB_JTAG);
  26. #else
  27. ESP_LOGW(TAG, "Not disabling JTAG - SECURITY COMPROMISED");
  28. #endif
  29. esp_efuse_write_field_bit(ESP_EFUSE_DIS_DIRECT_BOOT);
  30. #if defined(CONFIG_SECURE_BOOT_V2_ENABLED) && !defined(CONFIG_SECURE_BOOT_V2_ALLOW_EFUSE_RD_DIS)
  31. // This bit is set when enabling Secure Boot V2, but we can't enable it until this later point in the first boot
  32. // otherwise the Flash Encryption key cannot be read protected
  33. esp_efuse_write_field_bit(ESP_EFUSE_WR_DIS_RD_DIS);
  34. #endif
  35. #ifdef CONFIG_SECURE_FLASH_ENCRYPTION_MODE_RELEASE
  36. // Set write-protection for DIS_ICACHE to prevent bricking chip in case it will be set accidentally.
  37. // esp32h2 has DIS_ICACHE. Write-protection bit = 2.
  38. // List of eFuses with the same write protection bit:
  39. // DIS_ICACHE, DIS_USB_JTAG, POWERGLITCH_EN, DIS_FORCE_DOWNLOAD, SPI_DOWNLOAD_MSPI_DIS,
  40. // DIS_TWAI, JTAG_SEL_ENABLE, DIS_PAD_JTAG, DIS_DOWNLOAD_MANUAL_ENCRYPT
  41. esp_efuse_write_field_bit(ESP_EFUSE_WR_DIS_DIS_ICACHE);
  42. #endif
  43. return ESP_OK;
  44. }