1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <string.h>
7 #include "sdkconfig.h"
8 #include "esp_rom_efuse.h"
9 #include "esp_system.h"
10 #include "esp_efuse.h"
11 #include "esp_efuse_table.h"
12 
13 /* esp_system.h APIs relating to MAC addresses */
14 
15 #if CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_FOUR   || \
16     CONFIG_ESP32S3_UNIVERSAL_MAC_ADDRESSES_FOUR || \
17     CONFIG_ESP32C3_UNIVERSAL_MAC_ADDRESSES_FOUR
18 #define MAC_ADDR_UNIVERSE_BT_OFFSET 2
19 #else
20 #define MAC_ADDR_UNIVERSE_BT_OFFSET 1
21 #endif
22 
23 #if CONFIG_IEEE802154_ENABLED
24 #define ESP_MAC_ADDRESS_LEN 8
25 #else
26 #define ESP_MAC_ADDRESS_LEN 6
27 #endif
28 static const char *TAG = "system_api";
29 
30 static uint8_t base_mac_addr[ESP_MAC_ADDRESS_LEN] = { 0 };
31 
esp_base_mac_addr_set(const uint8_t * mac)32 esp_err_t esp_base_mac_addr_set(const uint8_t *mac)
33 {
34     if (mac == NULL) {
35         ESP_LOGE(TAG, "Base MAC address is NULL");
36         return ESP_ERR_INVALID_ARG;
37     }
38     if (mac[0] & 0x01) {
39         ESP_LOGE(TAG, "Base MAC must be a unicast MAC");
40         return ESP_ERR_INVALID_ARG;
41     }
42 
43     memcpy(base_mac_addr, mac, ESP_MAC_ADDRESS_LEN);
44 
45     return ESP_OK;
46 }
47 
esp_base_mac_addr_get(uint8_t * mac)48 esp_err_t esp_base_mac_addr_get(uint8_t *mac)
49 {
50     if (mac == NULL) {
51         return ESP_ERR_INVALID_ARG;
52     }
53     if (base_mac_addr[0] == 0 && memcmp(base_mac_addr, &base_mac_addr[1], ESP_MAC_ADDRESS_LEN - 1) == 0) {
54         ESP_LOGI(TAG, "Base MAC address is not set");
55         return ESP_ERR_INVALID_MAC;
56     }
57 
58     memcpy(mac, base_mac_addr, ESP_MAC_ADDRESS_LEN);
59 
60     return ESP_OK;
61 }
62 
esp_efuse_mac_get_custom(uint8_t * mac)63 esp_err_t esp_efuse_mac_get_custom(uint8_t *mac)
64 {
65 #if !CONFIG_IDF_TARGET_ESP32
66     size_t size_bits = esp_efuse_get_field_size(ESP_EFUSE_USER_DATA_MAC_CUSTOM);
67     assert((size_bits % 8) == 0);
68     esp_err_t err = esp_efuse_read_field_blob(ESP_EFUSE_USER_DATA_MAC_CUSTOM, mac, size_bits);
69     if (err != ESP_OK) {
70         return err;
71     }
72     size_t size = size_bits / 8;
73     if (mac[0] == 0 && memcmp(mac, &mac[1], size - 1) == 0) {
74         ESP_LOGE(TAG, "eFuse MAC_CUSTOM is empty");
75         return ESP_ERR_INVALID_MAC;
76     }
77 #if (ESP_MAC_ADDRESS_LEN == 8)
78     err = esp_efuse_read_field_blob(ESP_EFUSE_MAC_EXT, &mac[6], ESP_MAC_ADDRESS_LEN * 8 - size_bits);
79     if (err != ESP_OK) {
80         ESP_LOGE(TAG, "Reading MAC_EXT failed, error=%d", err);
81         return err;
82     }
83 #endif
84     return ESP_OK;
85 #else
86     uint8_t version;
87     esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM_VER, &version, 8);
88     if (version != 1) {
89         ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE version error, version = %d", version);
90         return ESP_ERR_INVALID_VERSION;
91     }
92 
93     uint8_t efuse_crc;
94     esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48);
95     esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM_CRC, &efuse_crc, 8);
96     uint8_t calc_crc = esp_rom_efuse_mac_address_crc8(mac, 6);
97 
98     if (efuse_crc != calc_crc) {
99         ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
100         return ESP_ERR_INVALID_CRC;
101     }
102     return ESP_OK;
103 #endif
104 }
105 
esp_efuse_mac_get_default(uint8_t * mac)106 esp_err_t esp_efuse_mac_get_default(uint8_t *mac)
107 {
108     size_t size_bits = esp_efuse_get_field_size(ESP_EFUSE_MAC_FACTORY);
109     assert((size_bits % 8) == 0);
110     esp_err_t err = esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, size_bits);
111     if (err != ESP_OK) {
112         return err;
113     }
114 #if (ESP_MAC_ADDRESS_LEN == 8)
115     err = esp_efuse_read_field_blob(ESP_EFUSE_MAC_EXT, &mac[6], ESP_MAC_ADDRESS_LEN * 8 - size_bits);
116     if (err != ESP_OK) {
117         ESP_LOGE(TAG, "Reading MAC_EXT failed, error=%d", err);
118         return err;
119     }
120 #endif
121 #ifdef CONFIG_IDF_TARGET_ESP32
122 // Only ESP32 has MAC CRC in efuse
123     uint8_t efuse_crc;
124     esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY_CRC, &efuse_crc, 8);
125     uint8_t calc_crc = esp_rom_efuse_mac_address_crc8(mac, 6);
126 
127     if (efuse_crc != calc_crc) {
128         // Small range of MAC addresses are accepted even if CRC is invalid.
129         // These addresses are reserved for Espressif internal use.
130         uint32_t mac_high = ((uint32_t)mac[0] << 8) | mac[1];
131         uint32_t mac_low = ((uint32_t)mac[2] << 24) | ((uint32_t)mac[3] << 16) | ((uint32_t)mac[4] << 8) | mac[5];
132         if (((mac_high & 0xFFFF) == 0x18fe) && (mac_low >= 0x346a85c7) && (mac_low <= 0x346a85f8)) {
133             return ESP_OK;
134         } else {
135             ESP_LOGE(TAG, "Base MAC address from BLK0 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
136             abort();
137         }
138     }
139 #endif // CONFIG_IDF_TARGET_ESP32
140     return ESP_OK;
141 }
142 
esp_derive_local_mac(uint8_t * local_mac,const uint8_t * universal_mac)143 esp_err_t esp_derive_local_mac(uint8_t *local_mac, const uint8_t *universal_mac)
144 {
145     if (local_mac == NULL || universal_mac == NULL) {
146         ESP_LOGE(TAG, "mac address param is NULL");
147         return ESP_ERR_INVALID_ARG;
148     }
149 
150     memcpy(local_mac, universal_mac, 6);
151 
152     const unsigned UL_BIT = 0x2;
153     local_mac[0] |= UL_BIT;
154 
155     if (local_mac[0] == universal_mac[0]) {
156         // universal_mac was already local, so flip this bit instead
157         // (this is kept to be compatible with the previous behaviour of this function)
158         local_mac[0] ^= 0x4;
159     }
160 
161     return ESP_OK;
162 }
163 
esp_read_mac(uint8_t * mac,esp_mac_type_t type)164 esp_err_t esp_read_mac(uint8_t *mac, esp_mac_type_t type)
165 {
166     uint8_t efuse_mac[ESP_MAC_ADDRESS_LEN];
167 
168     if (mac == NULL) {
169         ESP_LOGE(TAG, "mac address param is NULL");
170         return ESP_ERR_INVALID_ARG;
171     }
172 #if CONFIG_IEEE802154_ENABLED
173     if (type < ESP_MAC_WIFI_STA || type > ESP_MAC_IEEE802154) {
174 #else
175     if (type < ESP_MAC_WIFI_STA || type > ESP_MAC_ETH) {
176 #endif
177         ESP_LOGE(TAG, "mac type is incorrect");
178         return ESP_ERR_INVALID_ARG;
179     }
180 
181     // if base mac address is not set, read one from EFUSE and then write back
182     if (esp_base_mac_addr_get(efuse_mac) != ESP_OK) {
183         ESP_LOGI(TAG, "read default base MAC address from EFUSE");
184         esp_efuse_mac_get_default(efuse_mac);
185         esp_base_mac_addr_set(efuse_mac);
186     }
187 
188     switch (type) {
189     case ESP_MAC_WIFI_STA:
190         memcpy(mac, efuse_mac, 6);
191         break;
192     case ESP_MAC_WIFI_SOFTAP:
193 #if CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP
194         memcpy(mac, efuse_mac, 6);
195         // as a result of some esp32s2 chips burned with one MAC address by mistake,
196         // there are some MAC address are reserved for this bug fix.
197         // related mistake MAC address is 0x7cdfa1003000~0x7cdfa1005fff,
198         // reserved MAC address is 0x7cdfa1020000~0x7cdfa1022fff (MAC address + 0x1d000).
199 #ifdef CONFIG_IDF_TARGET_ESP32S2
200         uint8_t mac_begin[6] = { 0x7c, 0xdf, 0xa1, 0x00, 0x30, 0x00 };
201         uint8_t mac_end[6]   = { 0x7c, 0xdf, 0xa1, 0x00, 0x5f, 0xff };
202         if (memcmp(mac, mac_begin, 6) >= 0 && memcmp(mac_end, mac, 6) >= 0 ) {
203             mac[3] += 0x02; // contain carry bit
204             mac[4] += 0xd0;
205         } else {
206             mac[5] += 1;
207         }
208 #else
209         mac[5] += 1;
210 #endif // IDF_TARGET_ESP32S2
211 #else
212         esp_derive_local_mac(mac, efuse_mac);
213 #endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP
214         break;
215     case ESP_MAC_BT:
216 #if CONFIG_ESP_MAC_ADDR_UNIVERSE_BT
217         memcpy(mac, efuse_mac, 6);
218 #if !CONFIG_IDF_TARGET_ESP32H2
219         // esp32h2 chips do not have wifi module, so the mac address do not need to add the BT offset
220         mac[5] += MAC_ADDR_UNIVERSE_BT_OFFSET;
221 #endif //!CONFIG_IDF_TARGET_ESP32H2
222 #else
223         return ESP_ERR_NOT_SUPPORTED;
224 #endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_BT
225         break;
226     case ESP_MAC_ETH:
227 #if CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH
228         memcpy(mac, efuse_mac, 6);
229         mac[5] += 3;
230 #else
231         efuse_mac[5] += 1;
232         esp_derive_local_mac(mac, efuse_mac);
233 #endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH
234         break;
235 #if CONFIG_IEEE802154_ENABLED
236     case ESP_MAC_IEEE802154:
237         memcpy(mac, efuse_mac, 8);
238         break;
239 #endif
240     default:
241         ESP_LOGE(TAG, "unsupported mac type");
242         break;
243     }
244 
245     return ESP_OK;
246 }
247