• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015-2019 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 #include <string.h>
15 #include "sdkconfig.h"
16 #include "esp_rom_efuse.h"
17 #include "esp_system.h"
18 #include "esp_efuse.h"
19 #include "esp_efuse_table.h"
20 
21 /* esp_system.h APIs relating to MAC addresses */
22 
23 #if CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_FOUR   || \
24     CONFIG_ESP32S3_UNIVERSAL_MAC_ADDRESSES_FOUR || \
25     CONFIG_ESP32C3_UNIVERSAL_MAC_ADDRESSES_FOUR
26 #define MAC_ADDR_UNIVERSE_BT_OFFSET 2
27 #else
28 #define MAC_ADDR_UNIVERSE_BT_OFFSET 1
29 #endif
30 
31 static const char* TAG = "system_api";
32 
33 static uint8_t base_mac_addr[6] = { 0 };
34 
esp_base_mac_addr_set(const uint8_t * mac)35 esp_err_t esp_base_mac_addr_set(const uint8_t *mac)
36 {
37     if (mac == NULL) {
38         ESP_LOGE(TAG, "Base MAC address is NULL");
39         return ESP_ERR_INVALID_ARG;
40     }
41     if (mac[0] & 0x01) {
42         ESP_LOGE(TAG, "Base MAC must be a unicast MAC");
43         return ESP_ERR_INVALID_ARG;
44     }
45 
46     memcpy(base_mac_addr, mac, 6);
47 
48     return ESP_OK;
49 }
50 
esp_base_mac_addr_get(uint8_t * mac)51 esp_err_t esp_base_mac_addr_get(uint8_t *mac)
52 {
53     uint8_t null_mac[6] = {0};
54 
55     if (memcmp(base_mac_addr, null_mac, 6) == 0) {
56         ESP_LOGI(TAG, "Base MAC address is not set");
57         return ESP_ERR_INVALID_MAC;
58     }
59 
60     memcpy(mac, base_mac_addr, 6);
61 
62     return ESP_OK;
63 }
64 
esp_efuse_mac_get_custom(uint8_t * mac)65 esp_err_t esp_efuse_mac_get_custom(uint8_t *mac)
66 {
67 #if !CONFIG_IDF_TARGET_ESP32
68     return ESP_ERR_NOT_SUPPORTED; // TODO IDF-1326
69 #else
70     uint8_t version;
71     esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM_VER, &version, 8);
72     if (version != 1) {
73         ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE version error, version = %d", version);
74         return ESP_ERR_INVALID_VERSION;
75     }
76 
77     uint8_t efuse_crc;
78     esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48);
79     esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM_CRC, &efuse_crc, 8);
80     uint8_t calc_crc = esp_rom_efuse_mac_address_crc8(mac, 6);
81 
82     if (efuse_crc != calc_crc) {
83         ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
84         return ESP_ERR_INVALID_CRC;
85     }
86     return ESP_OK;
87 #endif
88 }
89 
esp_efuse_mac_get_default(uint8_t * mac)90 esp_err_t esp_efuse_mac_get_default(uint8_t* mac)
91 {
92     esp_err_t err = esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, 48);
93     if (err != ESP_OK) {
94         return err;
95     }
96 #ifdef CONFIG_IDF_TARGET_ESP32
97 // Only ESP32 has MAC CRC in efuse
98     uint8_t efuse_crc;
99     esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY_CRC, &efuse_crc, 8);
100     uint8_t calc_crc = esp_rom_efuse_mac_address_crc8(mac, 6);
101 
102     if (efuse_crc != calc_crc) {
103          // Small range of MAC addresses are accepted even if CRC is invalid.
104          // These addresses are reserved for Espressif internal use.
105         uint32_t mac_high = ((uint32_t)mac[0] << 8) | mac[1];
106         uint32_t mac_low = ((uint32_t)mac[2] << 24) | ((uint32_t)mac[3] << 16) | ((uint32_t)mac[4] << 8) | mac[5];
107         if (((mac_high & 0xFFFF) == 0x18fe) && (mac_low >= 0x346a85c7) && (mac_low <= 0x346a85f8)) {
108             return ESP_OK;
109         } else {
110             ESP_LOGE(TAG, "Base MAC address from BLK0 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
111             abort();
112         }
113     }
114 #endif // CONFIG_IDF_TARGET_ESP32
115     return ESP_OK;
116 }
117 
esp_derive_local_mac(uint8_t * local_mac,const uint8_t * universal_mac)118 esp_err_t esp_derive_local_mac(uint8_t* local_mac, const uint8_t* universal_mac)
119 {
120     uint8_t idx;
121 
122     if (local_mac == NULL || universal_mac == NULL) {
123         ESP_LOGE(TAG, "mac address param is NULL");
124         return ESP_ERR_INVALID_ARG;
125     }
126 
127     memcpy(local_mac, universal_mac, 6);
128     for (idx = 0; idx < 64; idx++) {
129         local_mac[0] = universal_mac[0] | 0x02;
130         local_mac[0] ^= idx << 2;
131 
132         if (memcmp(local_mac, universal_mac, 6)) {
133             break;
134         }
135     }
136 
137     return ESP_OK;
138 }
139 
esp_read_mac(uint8_t * mac,esp_mac_type_t type)140 esp_err_t esp_read_mac(uint8_t* mac, esp_mac_type_t type)
141 {
142     uint8_t efuse_mac[6];
143 
144     if (mac == NULL) {
145         ESP_LOGE(TAG, "mac address param is NULL");
146         return ESP_ERR_INVALID_ARG;
147     }
148 
149     if (type < ESP_MAC_WIFI_STA || type > ESP_MAC_ETH) {
150         ESP_LOGE(TAG, "mac type is incorrect");
151         return ESP_ERR_INVALID_ARG;
152     }
153 
154     // if base mac address is not set, read one from EFUSE and then write back
155     if (esp_base_mac_addr_get(efuse_mac) != ESP_OK) {
156         ESP_LOGI(TAG, "read default base MAC address from EFUSE");
157         esp_efuse_mac_get_default(efuse_mac);
158         esp_base_mac_addr_set(efuse_mac);
159     }
160 
161     switch (type) {
162     case ESP_MAC_WIFI_STA:
163         memcpy(mac, efuse_mac, 6);
164         break;
165     case ESP_MAC_WIFI_SOFTAP:
166 #if CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP
167         memcpy(mac, efuse_mac, 6);
168     // as a result of some esp32s2 chips burned with one MAC address by mistake,
169     // there are some MAC address are reserved for this bug fix.
170     // related mistake MAC address is 0x7cdfa1003000~0x7cdfa1005fff,
171     // reserved MAC address is 0x7cdfa1020000~0x7cdfa1022fff (MAC address + 0x1d000).
172 #ifdef CONFIG_IDF_TARGET_ESP32S2
173         uint8_t mac_begin[6] = { 0x7c, 0xdf, 0xa1, 0x00, 0x30, 0x00 };
174         uint8_t mac_end[6]   = { 0x7c, 0xdf, 0xa1, 0x00, 0x5f, 0xff };
175         if(memcmp(mac,mac_begin,6) >= 0 && memcmp(mac_end,mac,6) >=0 ){
176             mac[3] += 0x02; // contain carry bit
177             mac[4] += 0xd0;
178         } else {
179             mac[5] += 1;
180         }
181 #else
182         mac[5] += 1;
183 #endif // IDF_TARGET_ESP32S2
184 #else
185         esp_derive_local_mac(mac, efuse_mac);
186 #endif
187         break;
188     case ESP_MAC_BT:
189 #if CONFIG_ESP_MAC_ADDR_UNIVERSE_BT
190         memcpy(mac, efuse_mac, 6);
191         mac[5] += MAC_ADDR_UNIVERSE_BT_OFFSET;
192 #endif
193         break;
194     case ESP_MAC_ETH:
195 #if CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH
196         memcpy(mac, efuse_mac, 6);
197         mac[5] += 3;
198 #else
199         efuse_mac[5] += 1;
200         esp_derive_local_mac(mac, efuse_mac);
201 #endif
202         break;
203     default:
204         ESP_LOGE(TAG, "unsupported mac type");
205         break;
206     }
207 
208     return ESP_OK;
209 }
210