esp_flash_encrypt.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2015-2016 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. #pragma once
  14. #include <stdbool.h>
  15. #include "esp_attr.h"
  16. #include "esp_err.h"
  17. #ifndef BOOTLOADER_BUILD
  18. #include "esp_spi_flash.h"
  19. #endif
  20. #include "soc/efuse_periph.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /* @brief Flash encryption mode based on efuse values
  25. */
  26. typedef enum {
  27. ESP_FLASH_ENC_MODE_DISABLED, // flash encryption is not enabled (flash crypt cnt=0)
  28. ESP_FLASH_ENC_MODE_DEVELOPMENT, // flash encryption is enabled but for Development (reflash over UART allowed)
  29. ESP_FLASH_ENC_MODE_RELEASE // flash encryption is enabled for Release (reflash over UART disabled)
  30. } esp_flash_enc_mode_t;
  31. /**
  32. * @file esp_partition.h
  33. * @brief Support functions for flash encryption features
  34. *
  35. * Can be compiled as part of app or bootloader code.
  36. */
  37. /** @brief Is flash encryption currently enabled in hardware?
  38. *
  39. * Flash encryption is enabled if the FLASH_CRYPT_CNT efuse has an odd number of bits set.
  40. *
  41. * @return true if flash encryption is enabled.
  42. */
  43. static inline /** @cond */ IRAM_ATTR /** @endcond */ bool esp_flash_encryption_enabled(void) {
  44. uint32_t flash_crypt_cnt = REG_GET_FIELD(EFUSE_BLK0_RDATA0_REG, EFUSE_RD_FLASH_CRYPT_CNT);
  45. /* __builtin_parity is in flash, so we calculate parity inline */
  46. bool enabled = false;
  47. while(flash_crypt_cnt) {
  48. if (flash_crypt_cnt & 1) {
  49. enabled = !enabled;
  50. }
  51. flash_crypt_cnt >>= 1;
  52. }
  53. return enabled;
  54. }
  55. /* @brief Update on-device flash encryption
  56. *
  57. * Intended to be called as part of the bootloader process if flash
  58. * encryption is enabled in device menuconfig.
  59. *
  60. * If FLASH_CRYPT_CNT efuse parity is 1 (ie odd number of bits set),
  61. * then return ESP_OK immediately (indicating flash encryption is enabled
  62. * and functional).
  63. *
  64. * If FLASH_CRYPT_CNT efuse parity is 0 (ie even number of bits set),
  65. * assume the flash has just been written with plaintext that needs encrypting.
  66. *
  67. * The following regions of flash are encrypted in place:
  68. *
  69. * - The bootloader image, if a valid plaintext image is found.[*]
  70. * - The partition table, if a valid plaintext table is found.
  71. * - Any app partition that contains a valid plaintext app image.
  72. * - Any other partitions with the "encrypt" flag set. [**]
  73. *
  74. * After the re-encryption process completes, a '1' bit is added to the
  75. * FLASH_CRYPT_CNT value (setting the parity to 1) and the EFUSE is re-burned.
  76. *
  77. * [*] If reflashing bootloader with secure boot enabled, pre-encrypt
  78. * the bootloader before writing it to flash or secure boot will fail.
  79. *
  80. * [**] For this reason, if serial re-flashing a previous flashed
  81. * device with secure boot enabled and using FLASH_CRYPT_CNT to
  82. * trigger re-encryption, you must simultaneously re-flash plaintext
  83. * content to all partitions with the "encrypt" flag set or this
  84. * data will be corrupted (encrypted twice).
  85. *
  86. * @note The post-condition of this function is that all
  87. * partitions that should be encrypted are encrypted.
  88. *
  89. * @note Take care not to power off the device while this function
  90. * is running, or the partition currently being encrypted will be lost.
  91. *
  92. * @note RTC_WDT will reset while encryption operations will be performed (if RTC_WDT is configured).
  93. *
  94. * @return ESP_OK if all operations succeeded, ESP_ERR_INVALID_STATE
  95. * if a fatal error occured during encryption of all partitions.
  96. */
  97. esp_err_t esp_flash_encrypt_check_and_update(void);
  98. /** @brief Encrypt-in-place a block of flash sectors
  99. *
  100. * @note This function resets RTC_WDT between operations with sectors.
  101. * @param src_addr Source offset in flash. Should be multiple of 4096 bytes.
  102. * @param data_length Length of data to encrypt in bytes. Will be rounded up to next multiple of 4096 bytes.
  103. *
  104. * @return ESP_OK if all operations succeeded, ESP_ERR_FLASH_OP_FAIL
  105. * if SPI flash fails, ESP_ERR_FLASH_OP_TIMEOUT if flash times out.
  106. */
  107. esp_err_t esp_flash_encrypt_region(uint32_t src_addr, size_t data_length);
  108. /** @brief Write protect FLASH_CRYPT_CNT
  109. *
  110. * Intended to be called as a part of boot process if flash encryption
  111. * is enabled but secure boot is not used. This should protect against
  112. * serial re-flashing of an unauthorised code in absence of secure boot.
  113. *
  114. * @note On ESP32 V3 only, write protecting FLASH_CRYPT_CNT will also prevent
  115. * disabling UART Download Mode. If both are wanted, call
  116. * esp_efuse_disable_rom_download_mode() before calling this function.
  117. *
  118. */
  119. void esp_flash_write_protect_crypt_cnt();
  120. /** @brief Return the flash encryption mode
  121. *
  122. * The API is called during boot process but can also be called by
  123. * application to check the current flash encryption mode of ESP32
  124. *
  125. * @return
  126. */
  127. esp_flash_enc_mode_t esp_get_flash_encryption_mode();
  128. /** @brief Check the flash encryption mode during startup
  129. *
  130. * @note This function is called automatically during app startup,
  131. * it doesn't need to be called from the app.
  132. *
  133. * Verifies the flash encryption config during startup:
  134. *
  135. * - Correct any insecure flash encryption settings if hardware
  136. * Secure Boot is enabled.
  137. * - Log warnings if the efuse config doesn't match the project
  138. * config in any way
  139. */
  140. void esp_flash_encryption_init_checks(void);
  141. #ifdef __cplusplus
  142. }
  143. #endif