esp_flash_encrypt.h 4.6 KB

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