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 <stdlib.h>
16 #include "sdkconfig.h"
17 #include "esp_efuse.h"
18 #include "esp_efuse_utility.h"
19 #include "esp_efuse_table.h"
20 #include "esp_types.h"
21 #include "assert.h"
22 #include "esp_err.h"
23 #include "esp_log.h"
24 #include "soc/efuse_periph.h"
25 #include "bootloader_random.h"
26 #include "soc/apb_ctrl_reg.h"
27 #include "sys/param.h"
28
29 static __attribute__((unused)) const char *TAG = "efuse";
30 // Permanently update values written to the efuse write registers
esp_efuse_burn_new_values(void)31 void esp_efuse_burn_new_values(void)
32 {
33 esp_efuse_utility_burn_efuses();
34 }
35
36 // Reset efuse write registers
esp_efuse_reset(void)37 void esp_efuse_reset(void)
38 {
39 esp_efuse_utility_reset();
40 }
41
42 #ifdef CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE
43
44 #include "../include_bootloader/bootloader_flash_priv.h"
45 #include "esp_flash_encrypt.h"
46
47 static uint32_t esp_efuse_flash_offset = 0;
48 static uint32_t esp_efuse_flash_size = 0;
49
esp_efuse_init(uint32_t offset,uint32_t size)50 void esp_efuse_init(uint32_t offset, uint32_t size)
51 {
52 esp_efuse_flash_offset = offset;
53 esp_efuse_flash_size = size;
54 }
55
emulate_secure_version_read(void)56 static uint32_t emulate_secure_version_read(void)
57 {
58 uint32_t secure_version;
59 uint32_t offset = esp_efuse_flash_offset;
60 if (offset == 0) {
61 ESP_LOGE(TAG, "emulate secure_version can not be used");
62 return 0;
63 }
64 const uint32_t *efuse_place_in_flash = bootloader_mmap(offset, esp_efuse_flash_size);
65 if (!efuse_place_in_flash) {
66 ESP_LOGE(TAG, "secure_version can not be read from (0x%x, 0x%x) flash", offset, esp_efuse_flash_size);
67 return 0;
68 }
69 memcpy(&secure_version, efuse_place_in_flash, sizeof(uint32_t));
70 bootloader_munmap(efuse_place_in_flash);
71 secure_version = ~secure_version;
72 ESP_LOGV(TAG, "Read 0x%08x secure_version from flash", secure_version);
73 return secure_version;
74 }
75
emulate_secure_version_write(uint32_t secure_version)76 static void emulate_secure_version_write(uint32_t secure_version)
77 {
78 uint32_t secure_version_wr = ~secure_version;
79 uint32_t offset = esp_efuse_flash_offset;
80 if (offset == 0) {
81 ESP_LOGE(TAG, "emulate secure_version can not be used");
82 return;
83 }
84 esp_err_t err = bootloader_flash_write(offset, &secure_version_wr, sizeof(secure_version_wr), false);
85 if (err != ESP_OK) {
86 ESP_LOGE(TAG, "secure_version can not be written to flash. err = 0x%x", err);
87 }
88 ESP_LOGV(TAG, "Write 0x%08x secure_version into flash", secure_version);
89 }
90 #endif // CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE
91
esp_efuse_read_secure_version(void)92 uint32_t esp_efuse_read_secure_version(void)
93 {
94 #ifdef CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK
95 uint32_t secure_version = 0;
96 int size = esp_efuse_get_field_size(ESP_EFUSE_SECURE_VERSION);
97 size = MIN(CONFIG_BOOTLOADER_APP_SEC_VER_SIZE_EFUSE_FIELD, size);
98 #ifdef CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE
99 secure_version = emulate_secure_version_read();
100 #else
101 esp_efuse_read_field_blob(ESP_EFUSE_SECURE_VERSION, &secure_version, size);
102 #endif // CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE
103
104 return __builtin_popcount(secure_version & ((1ULL << size) - 1));
105 #else
106 return 0;
107 #endif
108 }
109
110 #ifdef CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK
write_anti_rollback(uint32_t new_bits)111 static void write_anti_rollback(uint32_t new_bits)
112 {
113 int size = esp_efuse_get_field_size(ESP_EFUSE_SECURE_VERSION);
114 size = MIN(CONFIG_BOOTLOADER_APP_SEC_VER_SIZE_EFUSE_FIELD, size);
115 #ifdef CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE
116 emulate_secure_version_write(new_bits);
117 #else
118 esp_efuse_write_field_blob(ESP_EFUSE_SECURE_VERSION, &new_bits, size);
119 #endif
120 }
121 #endif
122
esp_efuse_check_secure_version(uint32_t secure_version)123 bool esp_efuse_check_secure_version(uint32_t secure_version)
124 {
125 uint32_t sec_ver_hw = esp_efuse_read_secure_version();
126 return secure_version >= sec_ver_hw;
127 }
128
esp_efuse_update_secure_version(uint32_t secure_version)129 esp_err_t esp_efuse_update_secure_version(uint32_t secure_version)
130 {
131 #ifdef CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK
132 if (CONFIG_BOOTLOADER_APP_SEC_VER_SIZE_EFUSE_FIELD < secure_version) {
133 ESP_LOGE(TAG, "Max secure version is %d. Given %d version can not be written.", CONFIG_BOOTLOADER_APP_SEC_VER_SIZE_EFUSE_FIELD, secure_version);
134 return ESP_ERR_INVALID_ARG;
135 }
136 #ifndef CONFIG_BOOTLOADER_EFUSE_SECURE_VERSION_EMULATE
137 esp_efuse_coding_scheme_t coding_scheme = esp_efuse_get_coding_scheme(ESP_EFUSE_SECURE_VERSION_NUM_BLOCK);
138 if (coding_scheme != EFUSE_CODING_SCHEME_NONE) {
139 ESP_LOGE(TAG, "Anti rollback is not supported with any coding scheme.");
140 return ESP_ERR_NOT_SUPPORTED;
141 }
142 #endif
143 uint32_t sec_ver_hw = esp_efuse_read_secure_version();
144 // If secure_version is the same as in eFuse field than it is ok just go out.
145 if (sec_ver_hw < secure_version) {
146 uint32_t num_bit_hw = (1ULL << sec_ver_hw) - 1;
147 uint32_t num_bit_app = (1ULL << secure_version) - 1;
148 // Repeated programming of programmed bits is strictly forbidden
149 uint32_t new_bits = num_bit_app - num_bit_hw; // get only new bits
150 write_anti_rollback(new_bits);
151 ESP_LOGI(TAG, "Anti-rollback is set. eFuse field is updated(%d).", secure_version);
152 } else if (sec_ver_hw > secure_version) {
153 ESP_LOGE(TAG, "Anti-rollback is not set. secure_version of app is lower that eFuse field(%d).", sec_ver_hw);
154 return ESP_FAIL;
155 }
156 #endif
157 return ESP_OK;
158 }
159