mac_addr.c 8.0 KB

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