flash_encrypt_main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Flash encryption Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "soc/efuse_reg.h"
  11. #include "esp_efuse.h"
  12. #include "esp_chip_info.h"
  13. #include "esp_flash.h"
  14. #include "esp_partition.h"
  15. #include "esp_flash_encrypt.h"
  16. #include "esp_efuse_table.h"
  17. #include "nvs_flash.h"
  18. static void example_print_chip_info(void);
  19. static void example_print_flash_encryption_status(void);
  20. static void example_read_write_flash(void);
  21. #define CUSTOM_NVS_PART_NAME "custom_nvs"
  22. static const char* TAG = "example";
  23. #if CONFIG_IDF_TARGET_ESP32
  24. #define TARGET_CRYPT_CNT_EFUSE ESP_EFUSE_FLASH_CRYPT_CNT
  25. #define TARGET_CRYPT_CNT_WIDTH 7
  26. #else
  27. #define TARGET_CRYPT_CNT_EFUSE ESP_EFUSE_SPI_BOOT_CRYPT_CNT
  28. #define TARGET_CRYPT_CNT_WIDTH 3
  29. #endif
  30. static esp_err_t example_custom_nvs_part_init(const char *name)
  31. {
  32. #if CONFIG_NVS_ENCRYPTION
  33. esp_err_t ret = ESP_FAIL;
  34. const esp_partition_t *key_part = esp_partition_find_first(
  35. ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS_KEYS, NULL);
  36. if (key_part == NULL) {
  37. ESP_LOGE(TAG, "CONFIG_NVS_ENCRYPTION is enabled, but no partition with subtype nvs_keys found in the partition table.");
  38. return ret;
  39. }
  40. nvs_sec_cfg_t cfg = {};
  41. ret = nvs_flash_read_security_cfg(key_part, &cfg);
  42. if (ret != ESP_OK) {
  43. /* We shall not generate keys here as that must have been done in default NVS partition initialization case */
  44. ESP_LOGE(TAG, "Failed to read NVS security cfg: [0x%02X] (%s)", ret, esp_err_to_name(ret));
  45. return ret;
  46. }
  47. ret = nvs_flash_secure_init_partition(name, &cfg);
  48. if (ret == ESP_OK) {
  49. ESP_LOGI(TAG, "NVS partition \"%s\" is encrypted.", name);
  50. }
  51. return ret;
  52. #else
  53. return nvs_flash_init_partition(name);
  54. #endif
  55. }
  56. void app_main(void)
  57. {
  58. printf("\nExample to check Flash Encryption status\n");
  59. example_print_chip_info();
  60. example_print_flash_encryption_status();
  61. example_read_write_flash();
  62. /* Initialize the default NVS partition */
  63. esp_err_t ret = nvs_flash_init();
  64. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  65. ESP_ERROR_CHECK(nvs_flash_erase());
  66. ret = nvs_flash_init();
  67. }
  68. ESP_ERROR_CHECK(ret);
  69. /* Initialize the custom NVS partition */
  70. ret = example_custom_nvs_part_init(CUSTOM_NVS_PART_NAME);
  71. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  72. ESP_ERROR_CHECK(nvs_flash_erase_partition(CUSTOM_NVS_PART_NAME));
  73. ret = example_custom_nvs_part_init(CUSTOM_NVS_PART_NAME);
  74. }
  75. ESP_ERROR_CHECK(ret);
  76. }
  77. static void example_print_chip_info(void)
  78. {
  79. /* Print chip information */
  80. esp_chip_info_t chip_info;
  81. uint32_t flash_size;
  82. esp_chip_info(&chip_info);
  83. printf("This is %s chip with %d CPU core(s), WiFi%s%s, ",
  84. CONFIG_IDF_TARGET,
  85. chip_info.cores,
  86. (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
  87. (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
  88. printf("silicon revision %d, ", chip_info.revision);
  89. if(esp_flash_get_size(NULL, &flash_size) != ESP_OK) {
  90. printf("Get flash size failed");
  91. return;
  92. }
  93. printf("%dMB %s flash\n", flash_size / (1024 * 1024),
  94. (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
  95. }
  96. static void example_print_flash_encryption_status(void)
  97. {
  98. uint32_t flash_crypt_cnt = 0;
  99. esp_efuse_read_field_blob(TARGET_CRYPT_CNT_EFUSE, &flash_crypt_cnt, TARGET_CRYPT_CNT_WIDTH);
  100. printf("FLASH_CRYPT_CNT eFuse value is %d\n", flash_crypt_cnt);
  101. esp_flash_enc_mode_t mode = esp_get_flash_encryption_mode();
  102. if (mode == ESP_FLASH_ENC_MODE_DISABLED) {
  103. printf("Flash encryption feature is disabled\n");
  104. } else {
  105. printf("Flash encryption feature is enabled in %s mode\n",
  106. mode == ESP_FLASH_ENC_MODE_DEVELOPMENT ? "DEVELOPMENT" : "RELEASE");
  107. }
  108. }
  109. static void example_read_write_flash(void)
  110. {
  111. const esp_partition_t* partition = esp_partition_find_first(
  112. ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage");
  113. assert(partition);
  114. printf("Erasing partition \"%s\" (0x%x bytes)\n", partition->label, partition->size);
  115. ESP_ERROR_CHECK(esp_partition_erase_range(partition, 0, partition->size));
  116. /* Generate the data which will be written */
  117. const size_t data_size = 32;
  118. uint8_t plaintext_data[data_size];
  119. for (uint8_t i = 0; i < data_size; ++i) {
  120. plaintext_data[i] = i;
  121. }
  122. printf("Writing data with esp_partition_write:\n");
  123. ESP_LOG_BUFFER_HEXDUMP(TAG, plaintext_data, data_size, ESP_LOG_INFO);
  124. ESP_ERROR_CHECK(esp_partition_write(partition, 0, plaintext_data, data_size));
  125. uint8_t read_data[data_size];
  126. printf("Reading with esp_partition_read:\n");
  127. ESP_ERROR_CHECK(esp_partition_read(partition, 0, read_data, data_size));
  128. ESP_LOG_BUFFER_HEXDUMP(TAG, read_data, data_size, ESP_LOG_INFO);
  129. printf("Reading with esp_flash_read:\n");
  130. ESP_ERROR_CHECK(esp_flash_read(NULL, read_data, partition->address, data_size));
  131. ESP_LOG_BUFFER_HEXDUMP(TAG, read_data, data_size, ESP_LOG_INFO);
  132. }