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