mac_addr.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. /* esp_system.h APIs relating to MAC addresses */
  13. #if CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_FOUR || \
  14. CONFIG_ESP32S3_UNIVERSAL_MAC_ADDRESSES_FOUR || \
  15. CONFIG_ESP32C2_UNIVERSAL_MAC_ADDRESSES_FOUR || \
  16. CONFIG_ESP32C3_UNIVERSAL_MAC_ADDRESSES_FOUR
  17. #define MAC_ADDR_UNIVERSE_BT_OFFSET 2
  18. #else
  19. #define MAC_ADDR_UNIVERSE_BT_OFFSET 1
  20. #endif
  21. #if CONFIG_IEEE802154_ENABLED
  22. #define ESP_MAC_ADDRESS_LEN 8
  23. #else
  24. #define ESP_MAC_ADDRESS_LEN 6
  25. #endif
  26. static const char *TAG = "system_api";
  27. static uint8_t base_mac_addr[ESP_MAC_ADDRESS_LEN] = { 0 };
  28. esp_err_t esp_base_mac_addr_set(const uint8_t *mac)
  29. {
  30. if (mac == NULL) {
  31. ESP_LOGE(TAG, "Base MAC address is NULL");
  32. return ESP_ERR_INVALID_ARG;
  33. }
  34. if (mac[0] & 0x01) {
  35. ESP_LOGE(TAG, "Base MAC must be a unicast MAC");
  36. return ESP_ERR_INVALID_ARG;
  37. }
  38. memcpy(base_mac_addr, mac, ESP_MAC_ADDRESS_LEN);
  39. return ESP_OK;
  40. }
  41. esp_err_t esp_base_mac_addr_get(uint8_t *mac)
  42. {
  43. if (mac == NULL) {
  44. return ESP_ERR_INVALID_ARG;
  45. }
  46. if (base_mac_addr[0] == 0 && memcmp(base_mac_addr, &base_mac_addr[1], ESP_MAC_ADDRESS_LEN - 1) == 0) {
  47. ESP_LOGI(TAG, "Base MAC address is not set");
  48. return ESP_ERR_INVALID_MAC;
  49. }
  50. memcpy(mac, base_mac_addr, ESP_MAC_ADDRESS_LEN);
  51. return ESP_OK;
  52. }
  53. esp_err_t esp_efuse_mac_get_custom(uint8_t *mac)
  54. {
  55. #if !CONFIG_IDF_TARGET_ESP32
  56. size_t size_bits = esp_efuse_get_field_size(ESP_EFUSE_USER_DATA_MAC_CUSTOM);
  57. assert((size_bits % 8) == 0);
  58. esp_err_t err = esp_efuse_read_field_blob(ESP_EFUSE_USER_DATA_MAC_CUSTOM, mac, size_bits);
  59. if (err != ESP_OK) {
  60. return err;
  61. }
  62. size_t size = size_bits / 8;
  63. if (mac[0] == 0 && memcmp(mac, &mac[1], size - 1) == 0) {
  64. ESP_LOGE(TAG, "eFuse MAC_CUSTOM is empty");
  65. return ESP_ERR_INVALID_MAC;
  66. }
  67. #if (ESP_MAC_ADDRESS_LEN == 8)
  68. err = esp_efuse_read_field_blob(ESP_EFUSE_MAC_EXT, &mac[6], ESP_MAC_ADDRESS_LEN * 8 - size_bits);
  69. if (err != ESP_OK) {
  70. ESP_LOGE(TAG, "Reading MAC_EXT failed, error=%d", err);
  71. return err;
  72. }
  73. #endif
  74. return ESP_OK;
  75. #else
  76. uint8_t version;
  77. esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM_VER, &version, 8);
  78. if (version != 1) {
  79. // version 0 means has not been setup
  80. if (version == 0) {
  81. ESP_LOGD(TAG, "No base MAC address in eFuse (version=0)");
  82. } else if (version != 1) {
  83. ESP_LOGE(TAG, "Base MAC address version error, version = %d", version);
  84. }
  85. return ESP_ERR_INVALID_VERSION;
  86. }
  87. uint8_t efuse_crc;
  88. esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48);
  89. esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM_CRC, &efuse_crc, 8);
  90. uint8_t calc_crc = esp_rom_efuse_mac_address_crc8(mac, 6);
  91. if (efuse_crc != calc_crc) {
  92. ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
  93. return ESP_ERR_INVALID_CRC;
  94. }
  95. return ESP_OK;
  96. #endif
  97. }
  98. esp_err_t esp_efuse_mac_get_default(uint8_t *mac)
  99. {
  100. size_t size_bits = esp_efuse_get_field_size(ESP_EFUSE_MAC_FACTORY);
  101. assert((size_bits % 8) == 0);
  102. esp_err_t err = esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, size_bits);
  103. if (err != ESP_OK) {
  104. return err;
  105. }
  106. #if (ESP_MAC_ADDRESS_LEN == 8)
  107. err = esp_efuse_read_field_blob(ESP_EFUSE_MAC_EXT, &mac[6], ESP_MAC_ADDRESS_LEN * 8 - size_bits);
  108. if (err != ESP_OK) {
  109. ESP_LOGE(TAG, "Reading MAC_EXT failed, error=%d", err);
  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. }