mac_addr.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. static const char* TAG = "system_api";
  29. static uint8_t base_mac_addr[6] = { 0 };
  30. esp_err_t esp_base_mac_addr_set(const uint8_t *mac)
  31. {
  32. if (mac == NULL) {
  33. ESP_LOGE(TAG, "Base MAC address is NULL");
  34. return ESP_ERR_INVALID_ARG;
  35. }
  36. if (mac[0] & 0x01) {
  37. ESP_LOGE(TAG, "Base MAC must be a unicast MAC");
  38. return ESP_ERR_INVALID_ARG;
  39. }
  40. memcpy(base_mac_addr, mac, 6);
  41. return ESP_OK;
  42. }
  43. esp_err_t esp_base_mac_addr_get(uint8_t *mac)
  44. {
  45. uint8_t null_mac[6] = {0};
  46. if (memcmp(base_mac_addr, null_mac, 6) == 0) {
  47. ESP_LOGI(TAG, "Base MAC address is not set");
  48. return ESP_ERR_INVALID_MAC;
  49. }
  50. memcpy(mac, base_mac_addr, 6);
  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. return ESP_ERR_NOT_SUPPORTED; // TODO IDF-1326
  57. #else
  58. uint8_t version;
  59. esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM_VER, &version, 8);
  60. if (version != 1) {
  61. ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE version error, version = %d", version);
  62. return ESP_ERR_INVALID_VERSION;
  63. }
  64. uint8_t efuse_crc;
  65. esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48);
  66. esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM_CRC, &efuse_crc, 8);
  67. uint8_t calc_crc = esp_rom_efuse_mac_address_crc8(mac, 6);
  68. if (efuse_crc != calc_crc) {
  69. ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
  70. return ESP_ERR_INVALID_CRC;
  71. }
  72. return ESP_OK;
  73. #endif
  74. }
  75. esp_err_t esp_efuse_mac_get_default(uint8_t* mac)
  76. {
  77. esp_err_t err = esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, 48);
  78. if (err != ESP_OK) {
  79. return err;
  80. }
  81. #ifdef CONFIG_IDF_TARGET_ESP32
  82. // Only ESP32 has MAC CRC in efuse
  83. uint8_t efuse_crc;
  84. esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY_CRC, &efuse_crc, 8);
  85. uint8_t calc_crc = esp_rom_efuse_mac_address_crc8(mac, 6);
  86. if (efuse_crc != calc_crc) {
  87. // Small range of MAC addresses are accepted even if CRC is invalid.
  88. // These addresses are reserved for Espressif internal use.
  89. uint32_t mac_high = ((uint32_t)mac[0] << 8) | mac[1];
  90. uint32_t mac_low = ((uint32_t)mac[2] << 24) | ((uint32_t)mac[3] << 16) | ((uint32_t)mac[4] << 8) | mac[5];
  91. if (((mac_high & 0xFFFF) == 0x18fe) && (mac_low >= 0x346a85c7) && (mac_low <= 0x346a85f8)) {
  92. return ESP_OK;
  93. } else {
  94. ESP_LOGE(TAG, "Base MAC address from BLK0 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
  95. abort();
  96. }
  97. }
  98. #endif // CONFIG_IDF_TARGET_ESP32
  99. return ESP_OK;
  100. }
  101. esp_err_t esp_derive_local_mac(uint8_t* local_mac, const uint8_t* universal_mac)
  102. {
  103. uint8_t idx;
  104. if (local_mac == NULL || universal_mac == NULL) {
  105. ESP_LOGE(TAG, "mac address param is NULL");
  106. return ESP_ERR_INVALID_ARG;
  107. }
  108. memcpy(local_mac, universal_mac, 6);
  109. for (idx = 0; idx < 64; idx++) {
  110. local_mac[0] = universal_mac[0] | 0x02;
  111. local_mac[0] ^= idx << 2;
  112. if (memcmp(local_mac, universal_mac, 6)) {
  113. break;
  114. }
  115. }
  116. return ESP_OK;
  117. }
  118. esp_err_t esp_read_mac(uint8_t* mac, esp_mac_type_t type)
  119. {
  120. uint8_t efuse_mac[6];
  121. if (mac == NULL) {
  122. ESP_LOGE(TAG, "mac address param is NULL");
  123. return ESP_ERR_INVALID_ARG;
  124. }
  125. if (type < ESP_MAC_WIFI_STA || type > ESP_MAC_ETH) {
  126. ESP_LOGE(TAG, "mac type is incorrect");
  127. return ESP_ERR_INVALID_ARG;
  128. }
  129. // if base mac address is not set, read one from EFUSE and then write back
  130. if (esp_base_mac_addr_get(efuse_mac) != ESP_OK) {
  131. ESP_LOGI(TAG, "read default base MAC address from EFUSE");
  132. esp_efuse_mac_get_default(efuse_mac);
  133. esp_base_mac_addr_set(efuse_mac);
  134. }
  135. switch (type) {
  136. case ESP_MAC_WIFI_STA:
  137. memcpy(mac, efuse_mac, 6);
  138. break;
  139. case ESP_MAC_WIFI_SOFTAP:
  140. #if CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP
  141. memcpy(mac, efuse_mac, 6);
  142. // as a result of some esp32s2 chips burned with one MAC address by mistake,
  143. // there are some MAC address are reserved for this bug fix.
  144. // related mistake MAC address is 0x7cdfa1003000~0x7cdfa1005fff,
  145. // reserved MAC address is 0x7cdfa1020000~0x7cdfa1022fff (MAC address + 0x1d000).
  146. #ifdef CONFIG_IDF_TARGET_ESP32S2
  147. uint8_t mac_begin[6] = { 0x7c, 0xdf, 0xa1, 0x00, 0x30, 0x00 };
  148. uint8_t mac_end[6] = { 0x7c, 0xdf, 0xa1, 0x00, 0x5f, 0xff };
  149. if(memcmp(mac,mac_begin,6) >= 0 && memcmp(mac_end,mac,6) >=0 ){
  150. mac[3] += 0x02; // contain carry bit
  151. mac[4] += 0xd0;
  152. } else {
  153. mac[5] += 1;
  154. }
  155. #else
  156. mac[5] += 1;
  157. #endif // IDF_TARGET_ESP32S2
  158. #else
  159. esp_derive_local_mac(mac, efuse_mac);
  160. #endif
  161. break;
  162. case ESP_MAC_BT:
  163. #if CONFIG_ESP_MAC_ADDR_UNIVERSE_BT
  164. memcpy(mac, efuse_mac, 6);
  165. mac[5] += MAC_ADDR_UNIVERSE_BT_OFFSET;
  166. #endif
  167. break;
  168. case ESP_MAC_ETH:
  169. #if CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH
  170. memcpy(mac, efuse_mac, 6);
  171. mac[5] += 3;
  172. #else
  173. efuse_mac[5] += 1;
  174. esp_derive_local_mac(mac, efuse_mac);
  175. #endif
  176. break;
  177. default:
  178. ESP_LOGE(TAG, "unsupported mac type");
  179. break;
  180. }
  181. return ESP_OK;
  182. }