bootloader_random.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2010-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. //
  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 "bootloader_random.h"
  15. #include "soc/cpu.h"
  16. #include "soc/wdev_reg.h"
  17. #include "soc/rtc_cntl_reg.h"
  18. #include "soc/sens_reg.h"
  19. #include "soc/syscon_reg.h"
  20. #include "soc/dport_reg.h"
  21. #include "soc/i2s_reg.h"
  22. #include "esp_log.h"
  23. #ifndef BOOTLOADER_BUILD
  24. #include "esp_system.h"
  25. #endif
  26. const char *TAG = "boot_rng";
  27. void bootloader_fill_random(void *buffer, size_t length)
  28. {
  29. uint8_t *buffer_bytes = (uint8_t *)buffer;
  30. uint32_t random;
  31. #ifdef BOOTLOADER_BUILD
  32. uint32_t start, now;
  33. #endif
  34. for (int i = 0; i < length; i++) {
  35. if (i == 0 || i % 4 == 0) { /* redundant check is for a compiler warning */
  36. #ifdef BOOTLOADER_BUILD
  37. /* in bootloader with ADC feeding HWRNG, we accumulate 1
  38. bit of entropy per 40 APB cycles (==80 CPU cycles.)
  39. To avoid reading the entire RNG hardware state out
  40. as-is, we repeatedly read the RNG register and XOR all
  41. values.
  42. */
  43. random = REG_READ(WDEV_RND_REG);
  44. RSR(CCOUNT, start);
  45. do {
  46. random ^= REG_READ(WDEV_RND_REG);
  47. RSR(CCOUNT, now);
  48. } while(now - start < 80*32*2); /* extra factor of 2 is precautionary */
  49. #else
  50. random = esp_random();
  51. #endif
  52. }
  53. buffer_bytes[i] = random >> ((i % 4) * 8);
  54. }
  55. }
  56. void bootloader_random_enable(void)
  57. {
  58. /* Ensure the hardware RNG is enabled following a soft reset. This should always be the case already (this clock is
  59. never disabled while the CPU is running), this is a "belts and braces" type check.
  60. */
  61. SET_PERI_REG_MASK(DPORT_WIFI_CLK_EN_REG, DPORT_WIFI_CLK_RNG_EN);
  62. /* Enable SAR ADC in test mode to feed ADC readings of the 1.1V
  63. reference via I2S into the RNG entropy input.
  64. Note: I2S requires the PLL to be running, so the call to rtc_set_cpu_freq(CPU_80M)
  65. in early bootloader startup must have been made.
  66. */
  67. SET_PERI_REG_BITS(RTC_CNTL_TEST_MUX_REG, RTC_CNTL_DTEST_RTC, 2, RTC_CNTL_DTEST_RTC_S);
  68. SET_PERI_REG_MASK(RTC_CNTL_TEST_MUX_REG, RTC_CNTL_ENT_RTC);
  69. SET_PERI_REG_MASK(SENS_SAR_START_FORCE_REG, SENS_SAR2_EN_TEST);
  70. SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_I2S0_CLK_EN);
  71. CLEAR_PERI_REG_MASK(SENS_SAR_START_FORCE_REG, SENS_ULP_CP_FORCE_START_TOP);
  72. CLEAR_PERI_REG_MASK(SENS_SAR_START_FORCE_REG, SENS_ULP_CP_START_TOP);
  73. // Test pattern configuration byte 0xAD:
  74. //--[7:4] channel_sel: 10-->en_test
  75. //--[3:2] bit_width : 3-->12bit
  76. //--[1:0] atten : 1-->3dB attenuation
  77. WRITE_PERI_REG(SYSCON_SARADC_SAR2_PATT_TAB1_REG, 0xADADADAD);
  78. WRITE_PERI_REG(SYSCON_SARADC_SAR2_PATT_TAB2_REG, 0xADADADAD);
  79. WRITE_PERI_REG(SYSCON_SARADC_SAR2_PATT_TAB3_REG, 0xADADADAD);
  80. WRITE_PERI_REG(SYSCON_SARADC_SAR2_PATT_TAB4_REG, 0xADADADAD);
  81. SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR, 3, SENS_FORCE_XPD_SAR_S);
  82. SET_PERI_REG_MASK(SENS_SAR_READ_CTRL_REG, SENS_SAR1_DIG_FORCE);
  83. SET_PERI_REG_MASK(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_DIG_FORCE);
  84. SET_PERI_REG_MASK(SYSCON_SARADC_CTRL_REG, SYSCON_SARADC_SAR2_MUX);
  85. SET_PERI_REG_BITS(SYSCON_SARADC_CTRL_REG, SYSCON_SARADC_SAR_CLK_DIV, 4, SYSCON_SARADC_SAR_CLK_DIV_S);
  86. SET_PERI_REG_BITS(SYSCON_SARADC_FSM_REG, SYSCON_SARADC_RSTB_WAIT, 8, SYSCON_SARADC_RSTB_WAIT_S); /* was 1 */
  87. SET_PERI_REG_BITS(SYSCON_SARADC_FSM_REG, SYSCON_SARADC_START_WAIT, 10, SYSCON_SARADC_START_WAIT_S);
  88. SET_PERI_REG_BITS(SYSCON_SARADC_CTRL_REG, SYSCON_SARADC_WORK_MODE, 0, SYSCON_SARADC_WORK_MODE_S);
  89. SET_PERI_REG_MASK(SYSCON_SARADC_CTRL_REG, SYSCON_SARADC_SAR_SEL);
  90. CLEAR_PERI_REG_MASK(SYSCON_SARADC_CTRL_REG, SYSCON_SARADC_DATA_SAR_SEL);
  91. SET_PERI_REG_BITS(I2S_SAMPLE_RATE_CONF_REG(0), I2S_RX_BCK_DIV_NUM, 20, I2S_RX_BCK_DIV_NUM_S);
  92. SET_PERI_REG_MASK(SYSCON_SARADC_CTRL_REG,SYSCON_SARADC_DATA_TO_I2S);
  93. CLEAR_PERI_REG_MASK(I2S_CONF2_REG(0), I2S_CAMERA_EN);
  94. SET_PERI_REG_MASK(I2S_CONF2_REG(0), I2S_LCD_EN);
  95. SET_PERI_REG_MASK(I2S_CONF2_REG(0), I2S_DATA_ENABLE);
  96. SET_PERI_REG_MASK(I2S_CONF2_REG(0), I2S_DATA_ENABLE_TEST_EN);
  97. SET_PERI_REG_MASK(I2S_CONF_REG(0), I2S_RX_START);
  98. }
  99. void bootloader_random_disable(void)
  100. {
  101. /* Disable i2s clock */
  102. CLEAR_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_I2S0_CLK_EN);
  103. /* Reset some i2s configuration (possibly redundant as we reset entire
  104. I2S peripheral further down). */
  105. CLEAR_PERI_REG_MASK(I2S_CONF2_REG(0), I2S_CAMERA_EN);
  106. CLEAR_PERI_REG_MASK(I2S_CONF2_REG(0), I2S_LCD_EN);
  107. CLEAR_PERI_REG_MASK(I2S_CONF2_REG(0), I2S_DATA_ENABLE_TEST_EN);
  108. CLEAR_PERI_REG_MASK(I2S_CONF2_REG(0), I2S_DATA_ENABLE);
  109. CLEAR_PERI_REG_MASK(I2S_CONF_REG(0), I2S_RX_START);
  110. /* Restore SYSCON mode registers */
  111. CLEAR_PERI_REG_MASK(SENS_SAR_READ_CTRL_REG, SENS_SAR1_DIG_FORCE);
  112. CLEAR_PERI_REG_MASK(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_DIG_FORCE);
  113. /* Restore SAR ADC mode */
  114. CLEAR_PERI_REG_MASK(SENS_SAR_START_FORCE_REG, SENS_SAR2_EN_TEST);
  115. CLEAR_PERI_REG_MASK(SYSCON_SARADC_CTRL_REG, SYSCON_SARADC_SAR2_MUX
  116. | SYSCON_SARADC_SAR_SEL | SYSCON_SARADC_DATA_TO_I2S);
  117. SET_PERI_REG_BITS(SENS_SAR_MEAS_WAIT2_REG, SENS_FORCE_XPD_SAR, 0, SENS_FORCE_XPD_SAR_S);
  118. SET_PERI_REG_BITS(SYSCON_SARADC_FSM_REG, SYSCON_SARADC_START_WAIT, 8, SYSCON_SARADC_START_WAIT_S);
  119. /* Reset i2s peripheral */
  120. SET_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_I2S0_RST);
  121. CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_I2S0_RST);
  122. /* Disable pull supply voltage to SAR ADC */
  123. CLEAR_PERI_REG_MASK(RTC_CNTL_TEST_MUX_REG, RTC_CNTL_ENT_RTC);
  124. SET_PERI_REG_BITS(RTC_CNTL_TEST_MUX_REG, RTC_CNTL_DTEST_RTC, 0, RTC_CNTL_DTEST_RTC_S);
  125. }