esp_flash_encrypt.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_err.h>
  17. #include "esp_spi_flash.h"
  18. #include "soc/efuse_reg.h"
  19. /* Support functions for flash encryption features.
  20. Can be compiled as part of app or bootloader code.
  21. */
  22. /** @brief Is flash encryption currently enabled in hardware?
  23. *
  24. * Flash encryption is enabled if the FLASH_CRYPT_CNT efuse has an odd number of bits set.
  25. *
  26. * @return true if flash encryption is enabled.
  27. */
  28. static inline bool esp_flash_encryption_enabled(void) {
  29. uint32_t flash_crypt_cnt = REG_GET_FIELD(EFUSE_BLK0_RDATA0_REG, EFUSE_RD_FLASH_CRYPT_CNT);
  30. return __builtin_parity(flash_crypt_cnt) == 1;
  31. }
  32. /* @brief Update on-device flash encryption
  33. *
  34. * Intended to be called as part of the bootloader process if flash
  35. * encryption is enabled in device menuconfig.
  36. *
  37. * If FLASH_CRYPT_CNT efuse parity is 1 (ie odd number of bits set),
  38. * then return ESP_OK immediately (indicating flash encryption is enabled
  39. * and functional).
  40. *
  41. * If FLASH_CRYPT_CNT efuse parity is 0 (ie even number of bits set),
  42. * assume the flash has just been written with plaintext that needs encrypting.
  43. *
  44. * The following regions of flash are encrypted in place:
  45. *
  46. * - The bootloader image, if a valid plaintext image is found.[*]
  47. * - The partition table, if a valid plaintext table is found.
  48. * - Any app partition that contains a valid plaintext app image.
  49. * - Any other partitions with the "encrypt" flag set. [**]
  50. *
  51. * After the re-encryption process completes, a '1' bit is added to the
  52. * FLASH_CRYPT_CNT value (setting the parity to 1) and the EFUSE is re-burned.
  53. *
  54. * [*] If reflashing bootloader with secure boot enabled, pre-encrypt
  55. * the bootloader before writing it to flash or secure boot will fail.
  56. *
  57. * [**] For this reason, if serial re-flashing a previous flashed
  58. * device with secure boot enabled and using FLASH_CRYPT_CNT to
  59. * trigger re-encryption, you must simultaneously re-flash plaintext
  60. * content to all partitions with the "encrypt" flag set or this
  61. * data will be corrupted (encrypted twice).
  62. *
  63. * @note The post-condition of this function is that all
  64. * partitions that should be encrypted are encrypted.
  65. *
  66. * @note Take care not to power off the device while this function
  67. * is running, or the partition currently being encrypted will be lost.
  68. *
  69. * @return ESP_OK if all operations succeeded, ESP_ERR_INVALID_STATE
  70. * if a fatal error occured during encryption of all partitions.
  71. */
  72. esp_err_t esp_flash_encrypt_check_and_update(void);
  73. /** @brief Encrypt-in-place a block of flash sectors
  74. *
  75. * @param src_addr Source offset in flash. Should be multiple of 4096 bytes.
  76. * @param data_length Length of data to encrypt in bytes. Will be rounded up to next multiple of 4096 bytes.
  77. *
  78. * @return ESP_OK if all operations succeeded, ESP_ERR_FLASH_OP_FAIL
  79. * if SPI flash fails, ESP_ERR_FLASH_OP_TIMEOUT if flash times out.
  80. */
  81. esp_err_t esp_flash_encrypt_region(uint32_t src_addr, size_t data_length);
  82. #endif