esp_efuse_fields.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2017-2020 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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "esp_efuse.h"
  15. #include "esp_efuse_utility.h"
  16. #include "esp_efuse_table.h"
  17. #include "stdlib.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 "sys/param.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. // should return the same value as bootloader_common_get_chip_revision()
  31. uint32_t chip_ver = 0;
  32. // TODO: ESP32S2 does not have this field
  33. return chip_ver;
  34. }
  35. // Returns chip package from efuse
  36. uint32_t esp_efuse_get_pkg_ver(void)
  37. {
  38. uint32_t pkg_ver = 0;
  39. esp_efuse_read_field_blob(ESP_EFUSE_PKG_VERSION, &pkg_ver, 4);
  40. return pkg_ver;
  41. }
  42. void esp_efuse_write_random_key(uint32_t blk_wdata0_reg)
  43. {
  44. uint32_t buf[8];
  45. uint8_t raw[24];
  46. bootloader_fill_random(buf, sizeof(buf));
  47. ESP_LOGV(TAG, "Writing random values to address 0x%08x", blk_wdata0_reg);
  48. for (int i = 0; i < 8; i++) {
  49. ESP_LOGV(TAG, "EFUSE_BLKx_WDATA%d_REG = 0x%08x", i, buf[i]);
  50. REG_WRITE(blk_wdata0_reg + 4 * i, buf[i]);
  51. }
  52. bzero(buf, sizeof(buf));
  53. bzero(raw, sizeof(raw));
  54. }
  55. esp_err_t esp_efuse_disable_rom_download_mode(void)
  56. {
  57. return esp_efuse_write_field_bit(ESP_EFUSE_DIS_DOWNLOAD_MODE);
  58. }
  59. esp_err_t esp_efuse_set_rom_log_scheme(esp_efuse_rom_log_scheme_t log_scheme)
  60. {
  61. int cur_log_scheme = 0;
  62. esp_efuse_read_field_blob(ESP_EFUSE_UART_PRINT_CONTROL, &cur_log_scheme, 2);
  63. if (!cur_log_scheme) { // not burned yet
  64. return esp_efuse_write_field_blob(ESP_EFUSE_UART_PRINT_CONTROL, &log_scheme, 2);
  65. } else {
  66. return ESP_ERR_INVALID_STATE;
  67. }
  68. }
  69. esp_err_t esp_efuse_enable_rom_secure_download_mode(void)
  70. {
  71. if (esp_efuse_read_field_bit(ESP_EFUSE_DIS_DOWNLOAD_MODE)) {
  72. return ESP_ERR_INVALID_STATE;
  73. }
  74. return esp_efuse_write_field_bit(ESP_EFUSE_ENABLE_SECURITY_DOWNLOAD);
  75. }