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