2020-01-10 04:46:46 -05:00
|
|
|
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2019-10-29 23:19:22 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include "sdkconfig.h"
|
2020-07-13 09:57:24 -04:00
|
|
|
#include "esp_rom_efuse.h"
|
2019-10-29 23:19:22 -04:00
|
|
|
#include "esp_system.h"
|
|
|
|
#include "esp_efuse.h"
|
|
|
|
#include "esp_efuse_table.h"
|
|
|
|
|
|
|
|
/* esp_system.h APIs relating to MAC addresses */
|
|
|
|
|
2021-03-12 01:05:17 -05:00
|
|
|
#if CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES_FOUR || \
|
|
|
|
CONFIG_ESP32S3_UNIVERSAL_MAC_ADDRESSES_FOUR || \
|
|
|
|
CONFIG_ESP32C3_UNIVERSAL_MAC_ADDRESSES_FOUR
|
|
|
|
#define MAC_ADDR_UNIVERSE_BT_OFFSET 2
|
|
|
|
#else
|
|
|
|
#define MAC_ADDR_UNIVERSE_BT_OFFSET 1
|
|
|
|
#endif
|
|
|
|
|
2021-07-14 06:00:51 -04:00
|
|
|
#if CONFIG_IEEE802154_ENABLED
|
|
|
|
#define ESP_MAC_ADDRESS_LEN 8
|
|
|
|
#else
|
|
|
|
#define ESP_MAC_ADDRESS_LEN 6
|
|
|
|
#endif
|
|
|
|
static const char *TAG = "system_api";
|
2019-10-29 23:19:22 -04:00
|
|
|
|
2021-07-14 06:00:51 -04:00
|
|
|
static uint8_t base_mac_addr[ESP_MAC_ADDRESS_LEN] = { 0 };
|
2019-10-29 23:19:22 -04:00
|
|
|
|
|
|
|
esp_err_t esp_base_mac_addr_set(const uint8_t *mac)
|
|
|
|
{
|
|
|
|
if (mac == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Base MAC address is NULL");
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
if (mac[0] & 0x01) {
|
|
|
|
ESP_LOGE(TAG, "Base MAC must be a unicast MAC");
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2021-07-14 06:00:51 -04:00
|
|
|
memcpy(base_mac_addr, mac, ESP_MAC_ADDRESS_LEN);
|
2019-10-29 23:19:22 -04:00
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_base_mac_addr_get(uint8_t *mac)
|
|
|
|
{
|
2021-07-14 06:00:51 -04:00
|
|
|
uint8_t null_mac[ESP_MAC_ADDRESS_LEN] = {0};
|
2019-10-29 23:19:22 -04:00
|
|
|
|
2021-07-14 06:00:51 -04:00
|
|
|
if (memcmp(base_mac_addr, null_mac, ESP_MAC_ADDRESS_LEN) == 0) {
|
2020-01-10 04:46:46 -05:00
|
|
|
ESP_LOGI(TAG, "Base MAC address is not set");
|
2019-10-29 23:19:22 -04:00
|
|
|
return ESP_ERR_INVALID_MAC;
|
|
|
|
}
|
|
|
|
|
2021-07-14 06:00:51 -04:00
|
|
|
memcpy(mac, base_mac_addr, ESP_MAC_ADDRESS_LEN);
|
2019-10-29 23:19:22 -04:00
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_efuse_mac_get_custom(uint8_t *mac)
|
|
|
|
{
|
2020-07-29 10:03:46 -04:00
|
|
|
#if !CONFIG_IDF_TARGET_ESP32
|
2020-11-05 22:48:41 -05:00
|
|
|
return ESP_ERR_NOT_SUPPORTED; // TODO IDF-1326
|
2019-10-29 23:19:22 -04:00
|
|
|
#else
|
|
|
|
uint8_t version;
|
|
|
|
esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM_VER, &version, 8);
|
|
|
|
if (version != 1) {
|
|
|
|
ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE version error, version = %d", version);
|
|
|
|
return ESP_ERR_INVALID_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t efuse_crc;
|
|
|
|
esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48);
|
|
|
|
esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM_CRC, &efuse_crc, 8);
|
2020-07-13 09:57:24 -04:00
|
|
|
uint8_t calc_crc = esp_rom_efuse_mac_address_crc8(mac, 6);
|
2019-10-29 23:19:22 -04:00
|
|
|
|
|
|
|
if (efuse_crc != calc_crc) {
|
|
|
|
ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
|
|
|
|
return ESP_ERR_INVALID_CRC;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
2020-07-29 10:03:46 -04:00
|
|
|
#endif
|
2019-10-29 23:19:22 -04:00
|
|
|
}
|
|
|
|
|
2021-07-14 06:00:51 -04:00
|
|
|
esp_err_t esp_efuse_mac_get_default(uint8_t *mac)
|
2019-10-29 23:19:22 -04:00
|
|
|
{
|
2021-07-14 06:00:51 -04:00
|
|
|
if ( esp_efuse_get_field_size(ESP_EFUSE_MAC_FACTORY) != ESP_MAC_ADDRESS_LEN * 8) {
|
|
|
|
ESP_LOGE(TAG, "mac address length is incorrect, please check the mac address length which your type of the chip is supported");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
esp_err_t err = esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, ESP_MAC_ADDRESS_LEN * 8);
|
2019-10-29 23:19:22 -04:00
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
#ifdef CONFIG_IDF_TARGET_ESP32
|
2020-07-29 10:03:46 -04:00
|
|
|
// Only ESP32 has MAC CRC in efuse
|
2019-10-29 23:19:22 -04:00
|
|
|
uint8_t efuse_crc;
|
|
|
|
esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY_CRC, &efuse_crc, 8);
|
2020-07-13 09:57:24 -04:00
|
|
|
uint8_t calc_crc = esp_rom_efuse_mac_address_crc8(mac, 6);
|
2019-10-29 23:19:22 -04:00
|
|
|
|
|
|
|
if (efuse_crc != calc_crc) {
|
2021-07-14 06:00:51 -04:00
|
|
|
// Small range of MAC addresses are accepted even if CRC is invalid.
|
|
|
|
// These addresses are reserved for Espressif internal use.
|
2019-10-29 23:19:22 -04:00
|
|
|
uint32_t mac_high = ((uint32_t)mac[0] << 8) | mac[1];
|
2020-10-20 10:12:28 -04:00
|
|
|
uint32_t mac_low = ((uint32_t)mac[2] << 24) | ((uint32_t)mac[3] << 16) | ((uint32_t)mac[4] << 8) | mac[5];
|
|
|
|
if (((mac_high & 0xFFFF) == 0x18fe) && (mac_low >= 0x346a85c7) && (mac_low <= 0x346a85f8)) {
|
|
|
|
return ESP_OK;
|
2019-10-29 23:19:22 -04:00
|
|
|
} else {
|
|
|
|
ESP_LOGE(TAG, "Base MAC address from BLK0 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // CONFIG_IDF_TARGET_ESP32
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2021-07-14 06:00:51 -04:00
|
|
|
esp_err_t esp_derive_local_mac(uint8_t *local_mac, const uint8_t *universal_mac)
|
2019-10-29 23:19:22 -04:00
|
|
|
{
|
|
|
|
if (local_mac == NULL || universal_mac == NULL) {
|
|
|
|
ESP_LOGE(TAG, "mac address param is NULL");
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(local_mac, universal_mac, 6);
|
|
|
|
|
2021-04-21 22:09:54 -04:00
|
|
|
const unsigned UL_BIT = 0x2;
|
|
|
|
local_mac[0] |= UL_BIT;
|
|
|
|
|
|
|
|
if (local_mac[0] == universal_mac[0]) {
|
|
|
|
// universal_mac was already local, so flip this bit instead
|
|
|
|
// (this is kept to be compatible with the previous behaviour of this function)
|
|
|
|
local_mac[0] ^= 0x4;
|
2019-10-29 23:19:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2021-07-14 06:00:51 -04:00
|
|
|
esp_err_t esp_read_mac(uint8_t *mac, esp_mac_type_t type)
|
2019-10-29 23:19:22 -04:00
|
|
|
{
|
2021-07-14 06:00:51 -04:00
|
|
|
uint8_t efuse_mac[ESP_MAC_ADDRESS_LEN];
|
2019-10-29 23:19:22 -04:00
|
|
|
|
|
|
|
if (mac == NULL) {
|
|
|
|
ESP_LOGE(TAG, "mac address param is NULL");
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
2021-07-14 06:00:51 -04:00
|
|
|
#if CONFIG_IEEE802154_ENABLED
|
|
|
|
if (type < ESP_MAC_WIFI_STA || type > ESP_MAC_IEEE802154) {
|
|
|
|
#else
|
2019-10-29 23:19:22 -04:00
|
|
|
if (type < ESP_MAC_WIFI_STA || type > ESP_MAC_ETH) {
|
2021-07-14 06:00:51 -04:00
|
|
|
#endif
|
2019-10-29 23:19:22 -04:00
|
|
|
ESP_LOGE(TAG, "mac type is incorrect");
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2020-01-10 04:46:46 -05:00
|
|
|
// if base mac address is not set, read one from EFUSE and then write back
|
2019-10-29 23:19:22 -04:00
|
|
|
if (esp_base_mac_addr_get(efuse_mac) != ESP_OK) {
|
2020-01-10 04:46:46 -05:00
|
|
|
ESP_LOGI(TAG, "read default base MAC address from EFUSE");
|
2019-10-29 23:19:22 -04:00
|
|
|
esp_efuse_mac_get_default(efuse_mac);
|
2020-01-10 04:46:46 -05:00
|
|
|
esp_base_mac_addr_set(efuse_mac);
|
2019-10-29 23:19:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case ESP_MAC_WIFI_STA:
|
|
|
|
memcpy(mac, efuse_mac, 6);
|
|
|
|
break;
|
|
|
|
case ESP_MAC_WIFI_SOFTAP:
|
2020-01-10 04:46:46 -05:00
|
|
|
#if CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP
|
|
|
|
memcpy(mac, efuse_mac, 6);
|
2021-07-14 06:00:51 -04:00
|
|
|
// as a result of some esp32s2 chips burned with one MAC address by mistake,
|
|
|
|
// there are some MAC address are reserved for this bug fix.
|
|
|
|
// related mistake MAC address is 0x7cdfa1003000~0x7cdfa1005fff,
|
|
|
|
// reserved MAC address is 0x7cdfa1020000~0x7cdfa1022fff (MAC address + 0x1d000).
|
2020-03-03 22:15:52 -05:00
|
|
|
#ifdef CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
uint8_t mac_begin[6] = { 0x7c, 0xdf, 0xa1, 0x00, 0x30, 0x00 };
|
|
|
|
uint8_t mac_end[6] = { 0x7c, 0xdf, 0xa1, 0x00, 0x5f, 0xff };
|
2021-07-14 06:00:51 -04:00
|
|
|
if (memcmp(mac, mac_begin, 6) >= 0 && memcmp(mac_end, mac, 6) >= 0 ) {
|
2020-11-10 02:40:01 -05:00
|
|
|
mac[3] += 0x02; // contain carry bit
|
2020-03-03 22:15:52 -05:00
|
|
|
mac[4] += 0xd0;
|
2020-03-17 09:31:32 -04:00
|
|
|
} else {
|
|
|
|
mac[5] += 1;
|
2020-03-03 22:15:52 -05:00
|
|
|
}
|
|
|
|
#else
|
2020-01-10 04:46:46 -05:00
|
|
|
mac[5] += 1;
|
2020-03-03 22:15:52 -05:00
|
|
|
#endif // IDF_TARGET_ESP32S2
|
2020-01-10 04:46:46 -05:00
|
|
|
#else
|
|
|
|
esp_derive_local_mac(mac, efuse_mac);
|
2021-04-21 22:11:09 -04:00
|
|
|
#endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP
|
2019-10-29 23:19:22 -04:00
|
|
|
break;
|
|
|
|
case ESP_MAC_BT:
|
2020-01-10 04:46:46 -05:00
|
|
|
#if CONFIG_ESP_MAC_ADDR_UNIVERSE_BT
|
2020-05-24 23:57:37 -04:00
|
|
|
memcpy(mac, efuse_mac, 6);
|
2021-07-14 06:00:51 -04:00
|
|
|
#if !CONFIG_IDF_TARGET_ESP32H2
|
|
|
|
// esp32h2 chips do not have wifi module, so the mac address do not need to add the BT offset
|
2021-03-12 01:05:17 -05:00
|
|
|
mac[5] += MAC_ADDR_UNIVERSE_BT_OFFSET;
|
2021-07-14 06:00:51 -04:00
|
|
|
#endif //!CONFIG_IDF_TARGET_ESP32H2
|
2021-04-21 22:11:09 -04:00
|
|
|
#else
|
|
|
|
return ESP_ERR_NOT_SUPPORTED;
|
|
|
|
#endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_BT
|
2019-10-29 23:19:22 -04:00
|
|
|
break;
|
|
|
|
case ESP_MAC_ETH:
|
2020-01-10 04:46:46 -05:00
|
|
|
#if CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH
|
|
|
|
memcpy(mac, efuse_mac, 6);
|
|
|
|
mac[5] += 3;
|
|
|
|
#else
|
|
|
|
efuse_mac[5] += 1;
|
|
|
|
esp_derive_local_mac(mac, efuse_mac);
|
2021-04-21 22:11:09 -04:00
|
|
|
#endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH
|
2019-10-29 23:19:22 -04:00
|
|
|
break;
|
2021-07-14 06:00:51 -04:00
|
|
|
#if CONFIG_IEEE802154_ENABLED
|
|
|
|
case ESP_MAC_IEEE802154:
|
|
|
|
memcpy(mac, efuse_mac, 8);
|
|
|
|
break;
|
|
|
|
#endif
|
2019-10-29 23:19:22 -04:00
|
|
|
default:
|
2020-01-10 04:46:46 -05:00
|
|
|
ESP_LOGE(TAG, "unsupported mac type");
|
2019-10-29 23:19:22 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|