mac_addr.c 12 KB

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