mac_addr.c 6.1 KB

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