esp_efuse_fields.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2017-2018 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. #include <stdlib.h>
  14. #include "sdkconfig.h"
  15. #include "esp_efuse.h"
  16. #include "esp_efuse_utility.h"
  17. #include "esp_efuse_table.h"
  18. #include "esp_types.h"
  19. #include "assert.h"
  20. #include "esp_err.h"
  21. #include "esp_log.h"
  22. #include "soc/efuse_periph.h"
  23. #include "bootloader_random.h"
  24. #include "soc/apb_ctrl_reg.h"
  25. #include "sys/param.h"
  26. static __attribute__((unused)) const char *TAG = "efuse";
  27. // Permanently update values written to the efuse write registers
  28. void esp_efuse_burn_new_values(void)
  29. {
  30. esp_efuse_utility_burn_efuses();
  31. }
  32. // Reset efuse write registers
  33. void esp_efuse_reset(void)
  34. {
  35. esp_efuse_utility_reset();
  36. }
  37. #ifdef CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE
  38. #include "../include_bootloader/bootloader_flash_priv.h"
  39. #include "esp_flash_encrypt.h"
  40. static uint32_t esp_efuse_flash_offset = 0;
  41. static uint32_t esp_efuse_flash_size = 0;
  42. void esp_efuse_init(uint32_t offset, uint32_t size)
  43. {
  44. esp_efuse_flash_offset = offset;
  45. esp_efuse_flash_size = size;
  46. }
  47. static uint32_t emulate_secure_version_read(void)
  48. {
  49. uint32_t secure_version;
  50. uint32_t offset = esp_efuse_flash_offset;
  51. if (offset == 0) {
  52. ESP_LOGE(TAG, "emulate secure_version can not be used");
  53. return 0;
  54. }
  55. const uint32_t *efuse_place_in_flash = bootloader_mmap(offset, esp_efuse_flash_size);
  56. if (!efuse_place_in_flash) {
  57. ESP_LOGE(TAG, "secure_version can not be read from (0x%x, 0x%x) flash", offset, esp_efuse_flash_size);
  58. return 0;
  59. }
  60. memcpy(&secure_version, efuse_place_in_flash, sizeof(uint32_t));
  61. bootloader_munmap(efuse_place_in_flash);
  62. secure_version = ~secure_version;
  63. ESP_LOGV(TAG, "Read 0x%08x secure_version from flash", secure_version);
  64. return secure_version;
  65. }
  66. static void emulate_secure_version_write(uint32_t secure_version)
  67. {
  68. uint32_t secure_version_wr = ~secure_version;
  69. uint32_t offset = esp_efuse_flash_offset;
  70. if (offset == 0) {
  71. ESP_LOGE(TAG, "emulate secure_version can not be used");
  72. return;
  73. }
  74. esp_err_t err = bootloader_flash_write(offset, &secure_version_wr, sizeof(secure_version_wr), false);
  75. if (err != ESP_OK) {
  76. ESP_LOGE(TAG, "secure_version can not be written to flash. err = 0x%x", err);
  77. }
  78. ESP_LOGV(TAG, "Write 0x%08x secure_version into flash", secure_version);
  79. }
  80. #endif // CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE
  81. uint32_t esp_efuse_read_secure_version(void)
  82. {
  83. #ifdef CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK
  84. uint32_t secure_version = 0;
  85. int size = esp_efuse_get_field_size(ESP_EFUSE_SECURE_VERSION);
  86. size = MIN(CONFIG_BOOTLOADER_APP_SEC_VER_SIZE_EFUSE_FIELD, size);
  87. #ifdef CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE
  88. secure_version = emulate_secure_version_read();
  89. #else
  90. esp_efuse_read_field_blob(ESP_EFUSE_SECURE_VERSION, &secure_version, size);
  91. #endif // CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE
  92. return __builtin_popcount(secure_version & ((1ULL << size) - 1));
  93. #else
  94. return 0;
  95. #endif
  96. }
  97. #ifdef CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK
  98. static void write_anti_rollback(uint32_t new_bits)
  99. {
  100. int size = esp_efuse_get_field_size(ESP_EFUSE_SECURE_VERSION);
  101. size = MIN(CONFIG_BOOTLOADER_APP_SEC_VER_SIZE_EFUSE_FIELD, size);
  102. #ifdef CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE
  103. emulate_secure_version_write(new_bits);
  104. #else
  105. esp_efuse_write_field_blob(ESP_EFUSE_SECURE_VERSION, &new_bits, size);
  106. #endif
  107. }
  108. #endif
  109. bool esp_efuse_check_secure_version(uint32_t secure_version)
  110. {
  111. uint32_t sec_ver_hw = esp_efuse_read_secure_version();
  112. return secure_version >= sec_ver_hw;
  113. }
  114. esp_err_t esp_efuse_update_secure_version(uint32_t secure_version)
  115. {
  116. #ifdef CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK
  117. if (CONFIG_BOOTLOADER_APP_SEC_VER_SIZE_EFUSE_FIELD < secure_version) {
  118. ESP_LOGE(TAG, "Max secure version is %d. Given %d version can not be written.", CONFIG_BOOTLOADER_APP_SEC_VER_SIZE_EFUSE_FIELD, secure_version);
  119. return ESP_ERR_INVALID_ARG;
  120. }
  121. #ifndef CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE
  122. esp_efuse_coding_scheme_t coding_scheme = esp_efuse_get_coding_scheme(ESP_EFUSE_SECURE_VERSION_NUM_BLOCK);
  123. if (coding_scheme != EFUSE_CODING_SCHEME_NONE) {
  124. ESP_LOGE(TAG, "Anti rollback is not supported with any coding scheme.");
  125. return ESP_ERR_NOT_SUPPORTED;
  126. }
  127. #endif
  128. uint32_t sec_ver_hw = esp_efuse_read_secure_version();
  129. // If secure_version is the same as in eFuse field than it is ok just go out.
  130. if (sec_ver_hw < secure_version) {
  131. uint32_t num_bit_hw = (1ULL << sec_ver_hw) - 1;
  132. uint32_t num_bit_app = (1ULL << secure_version) - 1;
  133. // Repeated programming of programmed bits is strictly forbidden
  134. uint32_t new_bits = num_bit_app - num_bit_hw; // get only new bits
  135. write_anti_rollback(new_bits);
  136. ESP_LOGI(TAG, "Anti-rollback is set. eFuse field is updated(%d).", secure_version);
  137. } else if (sec_ver_hw > secure_version) {
  138. ESP_LOGE(TAG, "Anti-rollback is not set. secure_version of app is lower that eFuse field(%d).", sec_ver_hw);
  139. return ESP_FAIL;
  140. }
  141. #endif
  142. return ESP_OK;
  143. }