flash_encrypt.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 "esp_efuse.h"
  15. #include "esp_efuse_table.h"
  16. #include "esp_flash_encrypt.h"
  17. void esp_flash_write_protect_crypt_cnt()
  18. {
  19. uint8_t flash_crypt_cnt_wr_dis = 0;
  20. esp_efuse_read_field_blob(ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT, &flash_crypt_cnt_wr_dis, 1);
  21. if (!flash_crypt_cnt_wr_dis) {
  22. esp_efuse_write_field_cnt(ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT, 1);
  23. }
  24. }
  25. esp_flash_enc_mode_t esp_get_flash_encryption_mode()
  26. {
  27. uint8_t efuse_flash_crypt_cnt_wr_protected = 0;
  28. uint8_t dis_dl_enc = 0, dis_dl_dec = 0, dis_dl_cache = 0;
  29. esp_flash_enc_mode_t mode = ESP_FLASH_ENC_MODE_DEVELOPMENT;
  30. if (esp_flash_encryption_enabled()) {
  31. /* Check if FLASH CRYPT CNT is write protected */
  32. esp_efuse_read_field_blob(ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT, &efuse_flash_crypt_cnt_wr_protected, 1);
  33. if (efuse_flash_crypt_cnt_wr_protected) {
  34. esp_efuse_read_field_blob(ESP_EFUSE_DISABLE_DL_CACHE, &dis_dl_cache, 1);
  35. esp_efuse_read_field_blob(ESP_EFUSE_DISABLE_DL_ENCRYPT, &dis_dl_enc, 1);
  36. esp_efuse_read_field_blob(ESP_EFUSE_DISABLE_DL_DECRYPT, &dis_dl_dec, 1);
  37. /* Check if DISABLE_DL_DECRYPT, DISABLE_DL_ENCRYPT & DISABLE_DL_CACHE are set */
  38. if ( dis_dl_cache && dis_dl_enc && dis_dl_dec ) {
  39. mode = ESP_FLASH_ENC_MODE_RELEASE;
  40. }
  41. }
  42. } else {
  43. mode = ESP_FLASH_ENC_MODE_DISABLED;
  44. }
  45. return mode;
  46. }