mac_addr.c 7.7 KB

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