esp_efuse_fields.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "esp_efuse.h"
  14. #include "esp_efuse_utility.h"
  15. #include "esp_efuse_table.h"
  16. #include "stdlib.h"
  17. #include "esp_types.h"
  18. #include "assert.h"
  19. #include "esp_err.h"
  20. #include "esp_log.h"
  21. #include "soc/efuse_periph.h"
  22. #include "bootloader_random.h"
  23. #include "sys/param.h"
  24. #include "soc/apb_ctrl_reg.h"
  25. const static char *TAG = "efuse";
  26. // Contains functions that provide access to efuse fields which are often used in IDF.
  27. // Returns chip version from efuse
  28. uint8_t esp_efuse_get_chip_ver(void)
  29. {
  30. uint8_t eco_bit0, eco_bit1, eco_bit2;
  31. esp_efuse_read_field_blob(ESP_EFUSE_CHIP_VER_REV1, &eco_bit0, 1);
  32. esp_efuse_read_field_blob(ESP_EFUSE_CHIP_VER_REV2, &eco_bit1, 1);
  33. eco_bit2 = (REG_READ(APB_CTRL_DATE_REG) & 0x80000000) >> 31;
  34. uint32_t combine_value = (eco_bit2 << 2) | (eco_bit1 << 1) | eco_bit0;
  35. uint8_t chip_ver = 0;
  36. switch (combine_value) {
  37. case 0:
  38. chip_ver = 0;
  39. break;
  40. case 1:
  41. chip_ver = 1;
  42. break;
  43. case 3:
  44. chip_ver = 2;
  45. break;
  46. case 7:
  47. chip_ver = 3;
  48. break;
  49. default:
  50. chip_ver = 0;
  51. break;
  52. }
  53. return chip_ver;
  54. }
  55. // Returns chip package from efuse
  56. uint32_t esp_efuse_get_pkg_ver(void)
  57. {
  58. uint32_t pkg_ver = 0;
  59. esp_efuse_read_field_blob(ESP_EFUSE_CHIP_VER_PKG, &pkg_ver, 4);
  60. return pkg_ver;
  61. }
  62. // Disable BASIC ROM Console via efuse
  63. void esp_efuse_disable_basic_rom_console(void)
  64. {
  65. if (!esp_efuse_read_field_bit(ESP_EFUSE_CONSOLE_DEBUG_DISABLE)) {
  66. esp_efuse_write_field_cnt(ESP_EFUSE_CONSOLE_DEBUG_DISABLE, 1);
  67. ESP_LOGI(TAG, "Disable BASIC ROM Console fallback via efuse...");
  68. }
  69. }
  70. esp_err_t esp_efuse_disable_rom_download_mode(void)
  71. {
  72. #ifndef CONFIG_ESP32_REV_MIN_3
  73. /* Check if we support this revision at all */
  74. if(esp_efuse_get_chip_ver() < 3) {
  75. return ESP_ERR_NOT_SUPPORTED;
  76. }
  77. #endif
  78. if (esp_efuse_read_field_bit(ESP_EFUSE_UART_DOWNLOAD_DIS)) {
  79. return ESP_OK;
  80. }
  81. /* WR_DIS_FLASH_CRYPT_CNT also covers UART_DOWNLOAD_DIS on ESP32 */
  82. if(esp_efuse_read_field_bit(ESP_EFUSE_WR_DIS_FLASH_CRYPT_CNT)) {
  83. return ESP_ERR_INVALID_STATE;
  84. }
  85. return esp_efuse_write_field_bit(ESP_EFUSE_UART_DOWNLOAD_DIS);
  86. }
  87. esp_err_t esp_efuse_set_rom_log_scheme(esp_efuse_rom_log_scheme_t log_scheme)
  88. {
  89. return ESP_ERR_NOT_SUPPORTED;
  90. }
  91. void esp_efuse_write_random_key(uint32_t blk_wdata0_reg)
  92. {
  93. uint32_t buf[8];
  94. uint8_t raw[24];
  95. if (esp_efuse_get_coding_scheme(EFUSE_BLK2) == EFUSE_CODING_SCHEME_NONE) {
  96. bootloader_fill_random(buf, sizeof(buf));
  97. } else { // 3/4 Coding Scheme
  98. bootloader_fill_random(raw, sizeof(raw));
  99. esp_err_t r = esp_efuse_utility_apply_34_encoding(raw, buf, sizeof(raw));
  100. (void) r;
  101. assert(r == ESP_OK);
  102. }
  103. ESP_LOGV(TAG, "Writing random values to address 0x%08x", blk_wdata0_reg);
  104. for (int i = 0; i < 8; i++) {
  105. ESP_LOGV(TAG, "EFUSE_BLKx_WDATA%d_REG = 0x%08x", i, buf[i]);
  106. REG_WRITE(blk_wdata0_reg + 4*i, buf[i]);
  107. }
  108. bzero(buf, sizeof(buf));
  109. bzero(raw, sizeof(raw));
  110. }