mac_addr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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_ESP_MAC_UNIVERSAL_MAC_ADDRESSES_FOUR
  14. #define MAC_ADDR_UNIVERSE_BT_OFFSET 2
  15. #else
  16. #define MAC_ADDR_UNIVERSE_BT_OFFSET 1
  17. #endif
  18. #if CONFIG_IEEE802154_ENABLED
  19. #define ESP_MAC_ADDRESS_LEN 8
  20. #else
  21. #define ESP_MAC_ADDRESS_LEN 6
  22. #endif
  23. static const char *TAG = "system_api";
  24. typedef enum {
  25. STATE_INIT = 0,
  26. STATE_SET = (1 << 0),
  27. } state_t;
  28. typedef struct {
  29. esp_mac_type_t type: 4;
  30. state_t state: 4;
  31. uint8_t len;
  32. uint8_t mac[ESP_MAC_ADDRESS_LEN];
  33. } mac_t;
  34. static mac_t s_mac_table[] = {
  35. #ifdef CONFIG_ESP_WIFI_ENABLED
  36. {ESP_MAC_WIFI_STA, STATE_INIT, 6, {0}},
  37. {ESP_MAC_WIFI_SOFTAP, STATE_INIT, 6, {0}},
  38. #endif
  39. #ifdef CONFIG_ESP_MAC_ADDR_UNIVERSE_BT
  40. {ESP_MAC_BT, STATE_INIT, 6, {0}},
  41. #endif
  42. {ESP_MAC_ETH, STATE_INIT, 6, {0}},
  43. #ifdef CONFIG_ESP_MAC_ADDR_UNIVERSE_IEEE802154
  44. {ESP_MAC_IEEE802154, STATE_INIT, 8, {0}},
  45. #endif
  46. {ESP_MAC_BASE, STATE_INIT, ESP_MAC_ADDRESS_LEN, {0}},
  47. {ESP_MAC_EFUSE_FACTORY, STATE_INIT, ESP_MAC_ADDRESS_LEN, {0}},
  48. {ESP_MAC_EFUSE_CUSTOM, STATE_INIT, ESP_MAC_ADDRESS_LEN, {0}},
  49. };
  50. #define ITEMS_IN_MAC_TABLE (sizeof(s_mac_table) / sizeof(mac_t))
  51. static esp_err_t generate_mac(uint8_t *mac, uint8_t *base_mac_addr, esp_mac_type_t type);
  52. static int get_idx(esp_mac_type_t type)
  53. {
  54. for (int idx = 0; idx < ITEMS_IN_MAC_TABLE; idx++) {
  55. if (s_mac_table[idx].type == type) {
  56. return idx;
  57. }
  58. }
  59. ESP_LOGE(TAG, "mac type is incorrect (not found)");
  60. return -1;
  61. }
  62. static esp_err_t get_mac_addr_from_mac_table(uint8_t *mac, int idx, bool silent)
  63. {
  64. if (idx == -1) {
  65. return ESP_ERR_NOT_SUPPORTED;
  66. }
  67. if (!(s_mac_table[idx].state & STATE_SET)) {
  68. esp_mac_type_t type = s_mac_table[idx].type;
  69. if (type == ESP_MAC_BASE || type == ESP_MAC_EFUSE_FACTORY || type == ESP_MAC_EFUSE_CUSTOM) {
  70. esp_err_t err = ESP_OK;
  71. if (type == ESP_MAC_BASE || type == ESP_MAC_EFUSE_FACTORY) {
  72. err = esp_efuse_mac_get_default(s_mac_table[idx].mac);
  73. } else if (type == ESP_MAC_EFUSE_CUSTOM) {
  74. err = esp_efuse_mac_get_custom(s_mac_table[idx].mac);
  75. }
  76. if (err != ESP_OK) {
  77. return err;
  78. }
  79. s_mac_table[idx].state = STATE_SET;
  80. } else {
  81. if (!silent) {
  82. ESP_LOGE(TAG, "MAC address (type %d) is not set in mac table", type);
  83. }
  84. return ESP_ERR_INVALID_MAC;
  85. }
  86. }
  87. memcpy(mac, s_mac_table[idx].mac, s_mac_table[idx].len);
  88. return ESP_OK;
  89. }
  90. size_t esp_mac_addr_len_get(esp_mac_type_t type)
  91. {
  92. for (int idx = 0; idx < ITEMS_IN_MAC_TABLE; idx++) {
  93. if (s_mac_table[idx].type == type) {
  94. return s_mac_table[idx].len;
  95. }
  96. }
  97. return 0;
  98. }
  99. esp_err_t esp_iface_mac_addr_set(const uint8_t *mac, esp_mac_type_t type)
  100. {
  101. if (mac == NULL) {
  102. ESP_LOGE(TAG, "mac address param is NULL");
  103. return ESP_ERR_INVALID_ARG;
  104. }
  105. int idx = get_idx(type);
  106. if (idx == -1) {
  107. return ESP_ERR_NOT_SUPPORTED;
  108. }
  109. if (type == ESP_MAC_EFUSE_FACTORY || type == ESP_MAC_EFUSE_CUSTOM) {
  110. ESP_LOGE(TAG, "EFUSE MAC can not be set using this API");
  111. return ESP_ERR_INVALID_ARG;
  112. }
  113. if (type == ESP_MAC_BASE) {
  114. if (mac[0] & 0x01) {
  115. ESP_LOGE(TAG, "Base MAC must be a unicast MAC");
  116. return ESP_ERR_INVALID_ARG;
  117. }
  118. }
  119. memcpy(s_mac_table[idx].mac, mac, s_mac_table[idx].len);
  120. s_mac_table[idx].state = STATE_SET;
  121. return ESP_OK;
  122. }
  123. esp_err_t esp_base_mac_addr_set(const uint8_t *mac)
  124. {
  125. return esp_iface_mac_addr_set(mac, ESP_MAC_BASE);
  126. }
  127. esp_err_t esp_base_mac_addr_get(uint8_t *mac)
  128. {
  129. return esp_read_mac(mac, ESP_MAC_BASE);
  130. }
  131. esp_err_t esp_efuse_mac_get_custom(uint8_t *mac)
  132. {
  133. #if !CONFIG_IDF_TARGET_ESP32
  134. size_t size_bits = esp_efuse_get_field_size(ESP_EFUSE_USER_DATA_MAC_CUSTOM);
  135. assert((size_bits % 8) == 0);
  136. esp_err_t err = esp_efuse_read_field_blob(ESP_EFUSE_USER_DATA_MAC_CUSTOM, mac, size_bits);
  137. if (err != ESP_OK) {
  138. return err;
  139. }
  140. size_t size = size_bits / 8;
  141. if (mac[0] == 0 && memcmp(mac, &mac[1], size - 1) == 0) {
  142. ESP_LOGE(TAG, "eFuse MAC_CUSTOM is empty");
  143. return ESP_ERR_INVALID_MAC;
  144. }
  145. #if (ESP_MAC_ADDRESS_LEN == 8)
  146. err = esp_efuse_read_field_blob(ESP_EFUSE_MAC_EXT, &mac[6], ESP_MAC_ADDRESS_LEN * 8 - size_bits);
  147. if (err != ESP_OK) {
  148. ESP_LOGE(TAG, "Reading MAC_EXT failed, error=%d", err);
  149. return err;
  150. }
  151. #endif
  152. return ESP_OK;
  153. #else
  154. uint8_t version;
  155. esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM_VER, &version, 8);
  156. if (version != 1) {
  157. // version 0 means has not been setup
  158. if (version == 0) {
  159. ESP_LOGD(TAG, "No base MAC address in eFuse (version=0)");
  160. } else if (version != 1) {
  161. ESP_LOGE(TAG, "Base MAC address version error, version = %d", version);
  162. }
  163. return ESP_ERR_INVALID_VERSION;
  164. }
  165. uint8_t efuse_crc;
  166. esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48);
  167. esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM_CRC, &efuse_crc, 8);
  168. uint8_t calc_crc = esp_rom_efuse_mac_address_crc8(mac, 6);
  169. if (efuse_crc != calc_crc) {
  170. ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
  171. #ifdef CONFIG_ESP_MAC_IGNORE_MAC_CRC_ERROR
  172. ESP_LOGW(TAG, "Ignore MAC CRC error");
  173. #else
  174. return ESP_ERR_INVALID_CRC;
  175. #endif
  176. }
  177. return ESP_OK;
  178. #endif
  179. }
  180. esp_err_t esp_efuse_mac_get_default(uint8_t *mac)
  181. {
  182. size_t size_bits = esp_efuse_get_field_size(ESP_EFUSE_MAC_FACTORY);
  183. assert((size_bits % 8) == 0);
  184. esp_err_t err = esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, size_bits);
  185. if (err != ESP_OK) {
  186. return err;
  187. }
  188. #if (ESP_MAC_ADDRESS_LEN == 8)
  189. err = esp_efuse_read_field_blob(ESP_EFUSE_MAC_EXT, &mac[6], ESP_MAC_ADDRESS_LEN * 8 - size_bits);
  190. if (err != ESP_OK) {
  191. ESP_LOGE(TAG, "Reading MAC_EXT failed, error=%d", err);
  192. return err;
  193. }
  194. #endif
  195. #ifdef CONFIG_IDF_TARGET_ESP32
  196. // Only ESP32 has MAC CRC in efuse
  197. uint8_t efuse_crc;
  198. esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY_CRC, &efuse_crc, 8);
  199. uint8_t calc_crc = esp_rom_efuse_mac_address_crc8(mac, 6);
  200. if (efuse_crc != calc_crc) {
  201. // Small range of MAC addresses are accepted even if CRC is invalid.
  202. // These addresses are reserved for Espressif internal use.
  203. uint32_t mac_high = ((uint32_t)mac[0] << 8) | mac[1];
  204. uint32_t mac_low = ((uint32_t)mac[2] << 24) | ((uint32_t)mac[3] << 16) | ((uint32_t)mac[4] << 8) | mac[5];
  205. if (((mac_high & 0xFFFF) == 0x18fe) && (mac_low >= 0x346a85c7) && (mac_low <= 0x346a85f8)) {
  206. return ESP_OK;
  207. } else {
  208. ESP_LOGE(TAG, "Base MAC address from BLK0 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
  209. #ifdef CONFIG_ESP_MAC_IGNORE_MAC_CRC_ERROR
  210. ESP_LOGW(TAG, "Ignore MAC CRC error");
  211. #else
  212. return ESP_ERR_INVALID_CRC;
  213. #endif
  214. }
  215. }
  216. #endif // CONFIG_IDF_TARGET_ESP32
  217. return ESP_OK;
  218. }
  219. esp_err_t esp_derive_local_mac(uint8_t *local_mac, const uint8_t *universal_mac)
  220. {
  221. if (local_mac == NULL || universal_mac == NULL) {
  222. ESP_LOGE(TAG, "mac address param is NULL");
  223. return ESP_ERR_INVALID_ARG;
  224. }
  225. memcpy(local_mac, universal_mac, 6);
  226. const unsigned UL_BIT = 0x2;
  227. local_mac[0] |= UL_BIT;
  228. if (local_mac[0] == universal_mac[0]) {
  229. // universal_mac was already local, so flip this bit instead
  230. // (this is kept to be compatible with the previous behaviour of this function)
  231. local_mac[0] ^= 0x4;
  232. }
  233. return ESP_OK;
  234. }
  235. esp_err_t esp_read_mac(uint8_t *mac, esp_mac_type_t type)
  236. {
  237. if (mac == NULL) {
  238. ESP_LOGE(TAG, "mac address param is NULL");
  239. return ESP_ERR_INVALID_ARG;
  240. }
  241. int idx = get_idx(type);
  242. if (idx == -1) {
  243. return ESP_ERR_NOT_SUPPORTED;
  244. }
  245. if (get_mac_addr_from_mac_table(mac, idx, true) == ESP_OK) {
  246. return ESP_OK;
  247. }
  248. // A MAC with a specific type has not yet been set (or generated)
  249. // then go ahead and generate it based on the base mac
  250. uint8_t base_mac_addr[ESP_MAC_ADDRESS_LEN];
  251. esp_err_t err = get_mac_addr_from_mac_table(base_mac_addr, get_idx(ESP_MAC_BASE), false);
  252. if (err) {
  253. ESP_LOGE(TAG, "Error reading BASE MAC address");
  254. return ESP_FAIL;
  255. }
  256. err = generate_mac(mac, base_mac_addr, type);
  257. if (err) {
  258. ESP_LOGE(TAG, "MAC address generation error");
  259. return err;
  260. }
  261. // MAC was generated. We write it into the s_mac_table
  262. s_mac_table[idx].state = STATE_SET;
  263. memcpy(s_mac_table[idx].mac, mac, s_mac_table[idx].len);
  264. return err;
  265. }
  266. static esp_err_t generate_mac(uint8_t *mac, uint8_t *base_mac_addr, esp_mac_type_t type)
  267. {
  268. switch (type) {
  269. case ESP_MAC_WIFI_STA:
  270. memcpy(mac, base_mac_addr, 6);
  271. break;
  272. case ESP_MAC_WIFI_SOFTAP:
  273. #if CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP
  274. memcpy(mac, base_mac_addr, 6);
  275. // as a result of some esp32s2 chips burned with one MAC address by mistake,
  276. // there are some MAC address are reserved for this bug fix.
  277. // related mistake MAC address is 0x7cdfa1003000~0x7cdfa1005fff,
  278. // reserved MAC address is 0x7cdfa1020000~0x7cdfa1022fff (MAC address + 0x1d000).
  279. #ifdef CONFIG_IDF_TARGET_ESP32S2
  280. uint8_t mac_begin[6] = { 0x7c, 0xdf, 0xa1, 0x00, 0x30, 0x00 };
  281. uint8_t mac_end[6] = { 0x7c, 0xdf, 0xa1, 0x00, 0x5f, 0xff };
  282. if (memcmp(mac, mac_begin, 6) >= 0 && memcmp(mac_end, mac, 6) >= 0 ) {
  283. mac[3] += 0x02; // contain carry bit
  284. mac[4] += 0xd0;
  285. } else {
  286. mac[5] += 1;
  287. }
  288. #else
  289. mac[5] += 1;
  290. #endif // IDF_TARGET_ESP32S2
  291. #else
  292. esp_derive_local_mac(mac, base_mac_addr);
  293. #endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP
  294. break;
  295. case ESP_MAC_BT:
  296. #if CONFIG_ESP_MAC_ADDR_UNIVERSE_BT
  297. memcpy(mac, base_mac_addr, 6);
  298. #if SOC_WIFI_SUPPORTED
  299. // If the chips do not have wifi module, the mac address do not need to add the BT offset
  300. mac[5] += MAC_ADDR_UNIVERSE_BT_OFFSET;
  301. #endif //SOC_WIFI_SUPPORTED
  302. #else
  303. return ESP_ERR_NOT_SUPPORTED;
  304. #endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_BT
  305. break;
  306. case ESP_MAC_ETH:
  307. #if CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH
  308. memcpy(mac, base_mac_addr, 6);
  309. mac[5] += 3;
  310. #else
  311. base_mac_addr[5] += 1;
  312. esp_derive_local_mac(mac, base_mac_addr);
  313. #endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH
  314. break;
  315. #if CONFIG_IEEE802154_ENABLED
  316. case ESP_MAC_IEEE802154:
  317. memcpy(mac, base_mac_addr, 8);
  318. break;
  319. #endif
  320. default:
  321. ESP_LOGE(TAG, "unsupported mac type");
  322. return ESP_ERR_NOT_SUPPORTED;
  323. }
  324. return ESP_OK;
  325. }