mac_addr.c 7.7 KB

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