esp_efuse_fields.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. const static char *TAG = "efuse";
  25. // Contains functions that provide access to efuse fields which are often used in IDF.
  26. // Returns chip version from efuse
  27. uint8_t esp_efuse_get_chip_ver(void)
  28. {
  29. // should return the same value as bootloader_common_get_chip_revision()
  30. uint32_t chip_ver = 0;
  31. // TODO: ESP32S2 does not have this field
  32. return chip_ver;
  33. }
  34. // Returns chip package from efuse
  35. uint32_t esp_efuse_get_pkg_ver(void)
  36. {
  37. uint32_t pkg_ver = 0;
  38. esp_efuse_read_field_blob(ESP_EFUSE_PKG_VERSION, &pkg_ver, 4);
  39. return pkg_ver;
  40. }
  41. void esp_efuse_write_random_key(uint32_t blk_wdata0_reg)
  42. {
  43. uint32_t buf[8];
  44. uint8_t raw[24];
  45. bootloader_fill_random(buf, sizeof(buf));
  46. ESP_LOGV(TAG, "Writing random values to address 0x%08x", blk_wdata0_reg);
  47. for (int i = 0; i < 8; i++) {
  48. ESP_LOGV(TAG, "EFUSE_BLKx_WDATA%d_REG = 0x%08x", i, buf[i]);
  49. REG_WRITE(blk_wdata0_reg + 4 * i, buf[i]);
  50. }
  51. bzero(buf, sizeof(buf));
  52. bzero(raw, sizeof(raw));
  53. }
  54. esp_err_t esp_efuse_disable_rom_download_mode(void)
  55. {
  56. return esp_efuse_write_field_bit(ESP_EFUSE_DIS_DOWNLOAD_MODE);
  57. }
  58. esp_err_t esp_efuse_set_rom_log_scheme(esp_efuse_rom_log_scheme_t log_scheme)
  59. {
  60. int cur_log_scheme = 0;
  61. esp_efuse_read_field_blob(ESP_EFUSE_UART_PRINT_CONTROL, &cur_log_scheme, 2);
  62. if (!cur_log_scheme) { // not burned yet
  63. return esp_efuse_write_field_blob(ESP_EFUSE_UART_PRINT_CONTROL, &log_scheme, 2);
  64. } else {
  65. return ESP_ERR_INVALID_STATE;
  66. }
  67. }
  68. esp_err_t esp_efuse_enable_rom_secure_download_mode(void)
  69. {
  70. if (esp_efuse_read_field_bit(ESP_EFUSE_DIS_DOWNLOAD_MODE)) {
  71. return ESP_ERR_INVALID_STATE;
  72. }
  73. return esp_efuse_write_field_bit(ESP_EFUSE_ENABLE_SECURITY_DOWNLOAD);
  74. }