esp_flash_encrypt.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #ifndef __ESP32_FLASH_ENCRYPT_H
  14. #define __ESP32_FLASH_ENCRYPT_H
  15. #include <stdbool.h>
  16. #include "esp_attr.h"
  17. #include "esp_err.h"
  18. #ifndef BOOTLOADER_BUILD
  19. #include "esp_spi_flash.h"
  20. #endif
  21. #include "soc/efuse_reg.h"
  22. /**
  23. * @file esp_partition.h
  24. * @brief Support functions for flash encryption features
  25. *
  26. * Can be compiled as part of app or bootloader code.
  27. */
  28. /** @brief Is flash encryption currently enabled in hardware?
  29. *
  30. * Flash encryption is enabled if the FLASH_CRYPT_CNT efuse has an odd number of bits set.
  31. *
  32. * @return true if flash encryption is enabled.
  33. */
  34. static inline /** @cond */ IRAM_ATTR /** @endcond */ bool esp_flash_encryption_enabled(void) {
  35. uint32_t flash_crypt_cnt = REG_GET_FIELD(EFUSE_BLK0_RDATA0_REG, EFUSE_RD_FLASH_CRYPT_CNT);
  36. /* __builtin_parity is in flash, so we calculate parity inline */
  37. bool enabled = false;
  38. while(flash_crypt_cnt) {
  39. if (flash_crypt_cnt & 1) {
  40. enabled = !enabled;
  41. }
  42. flash_crypt_cnt >>= 1;
  43. }
  44. return enabled;
  45. }
  46. /* @brief Update on-device flash encryption
  47. *
  48. * Intended to be called as part of the bootloader process if flash
  49. * encryption is enabled in device menuconfig.
  50. *
  51. * If FLASH_CRYPT_CNT efuse parity is 1 (ie odd number of bits set),
  52. * then return ESP_OK immediately (indicating flash encryption is enabled
  53. * and functional).
  54. *
  55. * If FLASH_CRYPT_CNT efuse parity is 0 (ie even number of bits set),
  56. * assume the flash has just been written with plaintext that needs encrypting.
  57. *
  58. * The following regions of flash are encrypted in place:
  59. *
  60. * - The bootloader image, if a valid plaintext image is found.[*]
  61. * - The partition table, if a valid plaintext table is found.
  62. * - Any app partition that contains a valid plaintext app image.
  63. * - Any other partitions with the "encrypt" flag set. [**]
  64. *
  65. * After the re-encryption process completes, a '1' bit is added to the
  66. * FLASH_CRYPT_CNT value (setting the parity to 1) and the EFUSE is re-burned.
  67. *
  68. * [*] If reflashing bootloader with secure boot enabled, pre-encrypt
  69. * the bootloader before writing it to flash or secure boot will fail.
  70. *
  71. * [**] For this reason, if serial re-flashing a previous flashed
  72. * device with secure boot enabled and using FLASH_CRYPT_CNT to
  73. * trigger re-encryption, you must simultaneously re-flash plaintext
  74. * content to all partitions with the "encrypt" flag set or this
  75. * data will be corrupted (encrypted twice).
  76. *
  77. * @note The post-condition of this function is that all
  78. * partitions that should be encrypted are encrypted.
  79. *
  80. * @note Take care not to power off the device while this function
  81. * is running, or the partition currently being encrypted will be lost.
  82. *
  83. * @note RTC_WDT will reset while encryption operations will be performed (if RTC_WDT is configured).
  84. *
  85. * @return ESP_OK if all operations succeeded, ESP_ERR_INVALID_STATE
  86. * if a fatal error occured during encryption of all partitions.
  87. */
  88. esp_err_t esp_flash_encrypt_check_and_update(void);
  89. /** @brief Encrypt-in-place a block of flash sectors
  90. *
  91. * @note This function resets RTC_WDT between operations with sectors.
  92. * @param src_addr Source offset in flash. Should be multiple of 4096 bytes.
  93. * @param data_length Length of data to encrypt in bytes. Will be rounded up to next multiple of 4096 bytes.
  94. *
  95. * @return ESP_OK if all operations succeeded, ESP_ERR_FLASH_OP_FAIL
  96. * if SPI flash fails, ESP_ERR_FLASH_OP_TIMEOUT if flash times out.
  97. */
  98. esp_err_t esp_flash_encrypt_region(uint32_t src_addr, size_t data_length);
  99. /** @brief Write protect FLASH_CRYPT_CNT
  100. *
  101. * Intended to be called as a part of boot process if flash encryption
  102. * is enabled but secure boot is not used. This should protect against
  103. * serial re-flashing of an unauthorised code in absence of secure boot.
  104. *
  105. * @note To support disabling UART Download Mode on ESP32 V3 only, this function
  106. * doesn't write protect FLASH_CRYPT_CNT but instead sets it to the max value
  107. * (effectively the same result but allows burning the UART_DL_DIS efuse later on,
  108. * as this is otherwise also disabled if FLASH_CRYPT_CNT is write protected.)
  109. *
  110. */
  111. void esp_flash_write_protect_crypt_cnt();
  112. #endif