1 /*
2  * SPDX-FileCopyrightText: 2015-2023 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_mac.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_ESP_MAC_UNIVERSAL_MAC_ADDRESSES_FOUR
16 #define MAC_ADDR_UNIVERSE_BT_OFFSET 2
17 #else
18 #define MAC_ADDR_UNIVERSE_BT_OFFSET 1
19 #endif
20 
21 #if CONFIG_SOC_IEEE802154_SUPPORTED
22 #define ESP_MAC_ADDRESS_LEN 8
23 #else
24 #define ESP_MAC_ADDRESS_LEN 6
25 #endif
26 
27 static const char *TAG = "system_api";
28 
29 typedef enum {
30     STATE_INIT      = 0,
31     STATE_SET       = (1 << 0),
32 } state_t;
33 
34 typedef struct {
35     esp_mac_type_t type: 4;
36     state_t state: 4;
37     uint8_t len;
38     uint8_t mac[ESP_MAC_ADDRESS_LEN];
39 } mac_t;
40 
41 static mac_t s_mac_table[] = {
42 #if CONFIG_WIFI_ESP32
43     {ESP_MAC_WIFI_STA,      STATE_INIT, 6, {0}},
44     {ESP_MAC_WIFI_SOFTAP,   STATE_INIT, 6, {0}},
45 #endif
46 
47 #ifdef CONFIG_ESP_MAC_ADDR_UNIVERSE_BT
48     {ESP_MAC_BT,            STATE_INIT, 6, {0}},
49 #endif
50 
51     {ESP_MAC_ETH,           STATE_INIT, 6, {0}},
52 
53 #ifdef CONFIG_SOC_IEEE802154_SUPPORTED
54     {ESP_MAC_IEEE802154,    STATE_INIT, ESP_MAC_ADDRESS_LEN, {0}},
55     {ESP_MAC_EFUSE_EXT,     STATE_INIT, 2, {0}},
56 #endif
57 
58     {ESP_MAC_BASE,          STATE_INIT, 6, {0}},
59     {ESP_MAC_EFUSE_FACTORY, STATE_INIT, 6, {0}},
60     {ESP_MAC_EFUSE_CUSTOM,  STATE_INIT, 6, {0}},
61 };
62 
63 #define ITEMS_IN_MAC_TABLE (sizeof(s_mac_table) / sizeof(mac_t))
64 
65 static esp_err_t generate_mac(uint8_t *mac, uint8_t *base_mac_addr, esp_mac_type_t type);
66 static esp_err_t get_efuse_factory_mac(uint8_t *mac);
67 static esp_err_t get_efuse_mac_custom(uint8_t *mac);
68 #if CONFIG_SOC_IEEE802154_SUPPORTED
69 static esp_err_t get_efuse_mac_ext(uint8_t *mac);
70 #endif
71 
get_idx(esp_mac_type_t type)72 static int get_idx(esp_mac_type_t type)
73 {
74     for (int idx = 0; idx < ITEMS_IN_MAC_TABLE; idx++) {
75         if (s_mac_table[idx].type == type) {
76             return idx;
77         }
78     }
79     ESP_LOGE(TAG, "%d mac type is incorrect (not found)", type);
80     return -1;
81 }
82 
get_mac_addr_from_mac_table(uint8_t * mac,int idx,bool silent)83 static esp_err_t get_mac_addr_from_mac_table(uint8_t *mac, int idx, bool silent)
84 {
85     if (idx == -1) {
86         return ESP_ERR_NOT_SUPPORTED;
87     }
88     if (!(s_mac_table[idx].state & STATE_SET)) {
89         esp_mac_type_t type = s_mac_table[idx].type;
90         if (ESP_MAC_BASE <= type && type <= ESP_MAC_EFUSE_EXT) {
91             esp_err_t err = ESP_OK;
92             if (type == ESP_MAC_EFUSE_FACTORY
93 #ifndef CONFIG_ESP_MAC_USE_CUSTOM_MAC_AS_BASE_MAC
94                     || type == ESP_MAC_BASE
95 #endif
96              ) {
97                 err = get_efuse_factory_mac(s_mac_table[idx].mac);
98              } else if (type == ESP_MAC_EFUSE_CUSTOM
99 #ifdef CONFIG_ESP_MAC_USE_CUSTOM_MAC_AS_BASE_MAC
100                             || type == ESP_MAC_BASE
101 #endif
102              ) {
103                  err = get_efuse_mac_custom(s_mac_table[idx].mac);
104              }
105 #if CONFIG_SOC_IEEE802154_SUPPORTED
106             else if (type == ESP_MAC_EFUSE_EXT) {
107                 err = get_efuse_mac_ext(s_mac_table[idx].mac);
108             }
109 #endif
110             if (err != ESP_OK) {
111                 return err;
112             }
113             s_mac_table[idx].state = STATE_SET;
114         } else {
115             if (!silent) {
116                 ESP_LOGE(TAG, "MAC address (type %d) is not set in mac table", type);
117             }
118             return ESP_ERR_INVALID_MAC;
119         }
120     }
121     memcpy(mac, s_mac_table[idx].mac, s_mac_table[idx].len);
122     return ESP_OK;
123 }
124 
esp_mac_addr_len_get(esp_mac_type_t type)125 size_t esp_mac_addr_len_get(esp_mac_type_t type)
126 {
127     for (int idx = 0; idx < ITEMS_IN_MAC_TABLE; idx++) {
128         if (s_mac_table[idx].type == type) {
129             return s_mac_table[idx].len;
130         }
131     }
132     return 0;
133 }
134 
esp_iface_mac_addr_set(const uint8_t * mac,esp_mac_type_t type)135 esp_err_t esp_iface_mac_addr_set(const uint8_t *mac, esp_mac_type_t type)
136 {
137     if (mac == NULL) {
138         ESP_LOGE(TAG, "mac address param is NULL");
139         return ESP_ERR_INVALID_ARG;
140     }
141     int idx = get_idx(type);
142     if (idx == -1) {
143         return ESP_ERR_NOT_SUPPORTED;
144     }
145 
146     if (type == ESP_MAC_EFUSE_FACTORY || type == ESP_MAC_EFUSE_CUSTOM) {
147         ESP_LOGE(TAG, "EFUSE MAC can not be set using this API");
148         return ESP_ERR_INVALID_ARG;
149     }
150 
151     if (type == ESP_MAC_BASE) {
152         if (mac[0] & 0x01) {
153             ESP_LOGE(TAG, "Base MAC must be a unicast MAC");
154             return ESP_ERR_INVALID_ARG;
155         }
156     }
157 
158     memcpy(s_mac_table[idx].mac, mac, s_mac_table[idx].len);
159     s_mac_table[idx].state = STATE_SET;
160     return ESP_OK;
161 }
162 
esp_base_mac_addr_set(const uint8_t * mac)163 esp_err_t esp_base_mac_addr_set(const uint8_t *mac)
164 {
165     return esp_iface_mac_addr_set(mac, ESP_MAC_BASE);
166 }
167 
esp_base_mac_addr_get(uint8_t * mac)168 esp_err_t esp_base_mac_addr_get(uint8_t *mac)
169 {
170     return esp_read_mac(mac, ESP_MAC_BASE);
171 }
172 
173 #if CONFIG_SOC_IEEE802154_SUPPORTED
get_efuse_mac_ext(uint8_t * mac)174 static esp_err_t get_efuse_mac_ext(uint8_t *mac)
175 {
176     // ff:fe
177     esp_err_t err = esp_efuse_read_field_blob(ESP_EFUSE_MAC_EXT, mac, 16);
178     if (err != ESP_OK) {
179         ESP_LOGE(TAG, "Reading MAC_EXT failed, error=%d", err);
180         return err;
181     }
182     return ESP_OK;
183 }
184 
insert_mac_ext_into_mac(uint8_t * mac)185 static esp_err_t insert_mac_ext_into_mac(uint8_t *mac)
186 {
187     uint8_t mac_tmp[3];
188     memcpy(mac_tmp, &mac[3], 3);
189     esp_err_t err = get_efuse_mac_ext(&mac[3]);
190     if (err != ESP_OK) {
191         return err;
192     }
193     memcpy(&mac[5], mac_tmp, 3);
194     return err;
195 }
196 #endif
197 
esp_efuse_mac_get_custom(uint8_t * mac)198 esp_err_t esp_efuse_mac_get_custom(uint8_t *mac)
199 {
200     esp_err_t err = get_efuse_mac_custom(mac);
201     if (err != ESP_OK) {
202         return err;
203     }
204 #if CONFIG_SOC_IEEE802154_SUPPORTED
205     return insert_mac_ext_into_mac(mac);
206 #else
207     return ESP_OK;
208 #endif
209 }
210 
get_efuse_mac_custom(uint8_t * mac)211 static esp_err_t get_efuse_mac_custom(uint8_t *mac)
212 {
213 #if !CONFIG_IDF_TARGET_ESP32
214     size_t size_bits = esp_efuse_get_field_size(ESP_EFUSE_USER_DATA_MAC_CUSTOM);
215     assert((size_bits % 8) == 0);
216     esp_err_t err = esp_efuse_read_field_blob(ESP_EFUSE_USER_DATA_MAC_CUSTOM, mac, size_bits);
217     if (err != ESP_OK) {
218         return err;
219     }
220     size_t size = size_bits / 8;
221     if (mac[0] == 0 && memcmp(mac, &mac[1], size - 1) == 0) {
222         ESP_LOGE(TAG, "eFuse MAC_CUSTOM is empty");
223         return ESP_ERR_INVALID_MAC;
224     }
225 #else
226     uint8_t version;
227     esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM_VER, &version, 8);
228     if (version != 1) {
229         // version 0 means has not been setup
230         if (version == 0) {
231             ESP_LOGD(TAG, "No base MAC address in eFuse (version=0)");
232         } else if (version != 1) {
233             ESP_LOGE(TAG, "Base MAC address version error, version = %d", version);
234         }
235         return ESP_ERR_INVALID_VERSION;
236     }
237 
238     uint8_t efuse_crc;
239     esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48);
240     esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM_CRC, &efuse_crc, 8);
241     uint8_t calc_crc = esp_rom_efuse_mac_address_crc8(mac, 6);
242 
243     if (efuse_crc != calc_crc) {
244         ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
245 #ifdef CONFIG_ESP_MAC_IGNORE_MAC_CRC_ERROR
246         ESP_LOGW(TAG, "Ignore MAC CRC error");
247 #else
248         return ESP_ERR_INVALID_CRC;
249 #endif
250     }
251 #endif
252     return ESP_OK;
253 }
254 
esp_efuse_mac_get_default(uint8_t * mac)255 esp_err_t esp_efuse_mac_get_default(uint8_t *mac)
256 {
257     esp_err_t err = get_efuse_factory_mac(mac);
258     if (err != ESP_OK) {
259         return err;
260     }
261 #if CONFIG_SOC_IEEE802154_SUPPORTED
262     return insert_mac_ext_into_mac(mac);
263 #else
264     return ESP_OK;
265 #endif
266 }
267 
get_efuse_factory_mac(uint8_t * mac)268 static esp_err_t get_efuse_factory_mac(uint8_t *mac)
269 {
270     size_t size_bits = esp_efuse_get_field_size(ESP_EFUSE_MAC_FACTORY);
271     assert((size_bits % 8) == 0);
272     esp_err_t err = esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, size_bits);
273     if (err != ESP_OK) {
274         return err;
275     }
276 #ifdef CONFIG_IDF_TARGET_ESP32
277 // Only ESP32 has MAC CRC in efuse
278     uint8_t efuse_crc;
279     esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY_CRC, &efuse_crc, 8);
280     uint8_t calc_crc = esp_rom_efuse_mac_address_crc8(mac, 6);
281 
282     if (efuse_crc != calc_crc) {
283         // Small range of MAC addresses are accepted even if CRC is invalid.
284         // These addresses are reserved for Espressif internal use.
285         uint32_t mac_high = ((uint32_t)mac[0] << 8) | mac[1];
286         uint32_t mac_low = ((uint32_t)mac[2] << 24) | ((uint32_t)mac[3] << 16) | ((uint32_t)mac[4] << 8) | mac[5];
287         if (((mac_high & 0xFFFF) == 0x18fe) && (mac_low >= 0x346a85c7) && (mac_low <= 0x346a85f8)) {
288             return ESP_OK;
289         } else {
290             ESP_LOGE(TAG, "Base MAC address from BLK0 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
291 #ifdef CONFIG_ESP_MAC_IGNORE_MAC_CRC_ERROR
292             ESP_LOGW(TAG, "Ignore MAC CRC error");
293 #else
294             return ESP_ERR_INVALID_CRC;
295 #endif
296         }
297     }
298 #endif // CONFIG_IDF_TARGET_ESP32
299     return ESP_OK;
300 }
301 
esp_derive_local_mac(uint8_t * local_mac,const uint8_t * universal_mac)302 esp_err_t esp_derive_local_mac(uint8_t *local_mac, const uint8_t *universal_mac)
303 {
304     if (local_mac == NULL || universal_mac == NULL) {
305         ESP_LOGE(TAG, "mac address param is NULL");
306         return ESP_ERR_INVALID_ARG;
307     }
308 
309     memcpy(local_mac, universal_mac, 6);
310 
311     const unsigned UL_BIT = 0x2;
312     local_mac[0] |= UL_BIT;
313 
314     if (local_mac[0] == universal_mac[0]) {
315         // universal_mac was already local, so flip this bit instead
316         // (this is kept to be compatible with the previous behaviour of this function)
317         local_mac[0] ^= 0x4;
318     }
319 
320     return ESP_OK;
321 }
322 
esp_read_mac(uint8_t * mac,esp_mac_type_t type)323 esp_err_t esp_read_mac(uint8_t *mac, esp_mac_type_t type)
324 {
325     if (mac == NULL) {
326         ESP_LOGE(TAG, "mac address param is NULL");
327         return ESP_ERR_INVALID_ARG;
328     }
329     int idx = get_idx(type);
330     if (idx == -1) {
331         return ESP_ERR_NOT_SUPPORTED;
332     }
333 
334     if (get_mac_addr_from_mac_table(mac, idx, true) == ESP_OK) {
335         return ESP_OK;
336     }
337     // A MAC with a specific type has not yet been set (or generated)
338 
339     // then go ahead and generate it based on the base mac
340     uint8_t base_mac_addr[ESP_MAC_ADDRESS_LEN];
341     esp_err_t err = get_mac_addr_from_mac_table(base_mac_addr, get_idx(ESP_MAC_BASE), false);
342     if (err) {
343         ESP_LOGE(TAG, "Error reading BASE MAC address");
344         return ESP_FAIL;
345     }
346 
347     err = generate_mac(mac, base_mac_addr, type);
348     if (err) {
349         ESP_LOGE(TAG, "MAC address generation error");
350         return err;
351     }
352     // MAC was generated. We write it into the s_mac_table
353     s_mac_table[idx].state = STATE_SET;
354     memcpy(s_mac_table[idx].mac, mac, s_mac_table[idx].len);
355     return err;
356 }
357 
generate_mac(uint8_t * mac,uint8_t * base_mac_addr,esp_mac_type_t type)358 static esp_err_t generate_mac(uint8_t *mac, uint8_t *base_mac_addr, esp_mac_type_t type)
359 {
360     switch (type) {
361     case ESP_MAC_WIFI_STA:
362         memcpy(mac, base_mac_addr, 6);
363         break;
364     case ESP_MAC_WIFI_SOFTAP:
365 #if CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP
366         memcpy(mac, base_mac_addr, 6);
367         // as a result of some esp32s2 chips burned with one MAC address by mistake,
368         // there are some MAC address are reserved for this bug fix.
369         // related mistake MAC address is 0x7cdfa1003000~0x7cdfa1005fff,
370         // reserved MAC address is 0x7cdfa1020000~0x7cdfa1022fff (MAC address + 0x1d000).
371 #ifdef CONFIG_IDF_TARGET_ESP32S2
372         uint8_t mac_begin[6] = { 0x7c, 0xdf, 0xa1, 0x00, 0x30, 0x00 };
373         uint8_t mac_end[6]   = { 0x7c, 0xdf, 0xa1, 0x00, 0x5f, 0xff };
374         if (memcmp(mac, mac_begin, 6) >= 0 && memcmp(mac_end, mac, 6) >= 0 ) {
375             mac[3] += 0x02; // contain carry bit
376             mac[4] += 0xd0;
377         } else {
378             mac[5] += 1;
379         }
380 #else
381         mac[5] += 1;
382 #endif // IDF_TARGET_ESP32S2
383 #else
384         esp_derive_local_mac(mac, base_mac_addr);
385 #endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP
386         break;
387     case ESP_MAC_BT:
388 #if CONFIG_ESP_MAC_ADDR_UNIVERSE_BT
389         memcpy(mac, base_mac_addr, 6);
390 #if CONFIG_WIFI_ESP32
391         // If the chips do not have wifi module, the mac address do not need to add the BT offset
392         mac[5] += MAC_ADDR_UNIVERSE_BT_OFFSET;
393 #endif //CONFIG_WIFI_ESP32
394 #else
395         return ESP_ERR_NOT_SUPPORTED;
396 #endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_BT
397         break;
398     case ESP_MAC_ETH:
399 #if CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH
400         memcpy(mac, base_mac_addr, 6);
401         mac[5] += 3;
402 #else
403         base_mac_addr[5] += 1;
404         esp_derive_local_mac(mac, base_mac_addr);
405 #endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH
406         break;
407 #if CONFIG_SOC_IEEE802154_SUPPORTED
408     case ESP_MAC_IEEE802154:
409         // 60:55:f9
410         memcpy(mac, base_mac_addr, 3);
411         // 60:55:f9 + ff:fe
412         esp_read_mac(&mac[3], ESP_MAC_EFUSE_EXT);
413         // 60:55:f9:ff:fe + f7:2c:a2
414         memcpy(&mac[5], &base_mac_addr[3], 3);
415         // 60:55:f9:ff:fe:f7:2c:a2
416         break;
417 #endif
418     default:
419         ESP_LOGE(TAG, "unsupported mac type");
420         return ESP_ERR_NOT_SUPPORTED;
421     }
422     return ESP_OK;
423 }
424