flash_encryption_secure_features.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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_ICACHE);
  25. #else
  26. ESP_LOGW(TAG, "Not disabling UART bootloader cache - SECURITY COMPROMISED");
  27. #endif
  28. #ifndef CONFIG_SECURE_BOOT_ALLOW_JTAG
  29. ESP_LOGI(TAG, "Disable JTAG...");
  30. esp_efuse_write_field_bit(ESP_EFUSE_DIS_PAD_JTAG);
  31. esp_efuse_write_field_bit(ESP_EFUSE_DIS_USB_JTAG);
  32. #else
  33. ESP_LOGW(TAG, "Not disabling JTAG - SECURITY COMPROMISED");
  34. #endif
  35. esp_efuse_write_field_bit(ESP_EFUSE_DIS_DIRECT_BOOT);
  36. #if defined(CONFIG_SECURE_BOOT_V2_ENABLED) && !defined(CONFIG_SECURE_BOOT_V2_ALLOW_EFUSE_RD_DIS)
  37. // This bit is set when enabling Secure Boot V2, but we can't enable it until this later point in the first boot
  38. // otherwise the Flash Encryption key cannot be read protected
  39. esp_efuse_write_field_bit(ESP_EFUSE_WR_DIS_RD_DIS);
  40. #endif
  41. return ESP_OK;
  42. }