mac_addr.c 7.6 KB

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