7#include "esp_efuse_table.h"
10#include <freertos/FreeRTOS.h>
11#include <freertos/portmacro.h>
13#include "esp_random.h"
14#include "esp_system.h"
18static const char *
const TAG =
"esp32";
21 esp_fill_random(data,
len);
30#ifdef CONFIG_LWIP_TCPIP_CORE_LOCKING
31#include "lwip/priv/tcpip_priv.h"
35#ifdef CONFIG_LWIP_TCPIP_CORE_LOCKING
45 if (!sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)) {
52#ifdef CONFIG_LWIP_TCPIP_CORE_LOCKING
63 if (sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)) {
70static bool read_valid_mac(uint8_t *mac, esp_err_t err) {
return err == ESP_OK &&
mac_address_is_valid(mac); }
72static constexpr size_t MAC_ADDRESS_SIZE_BITS = MAC_ADDRESS_SIZE * 8;
80#if defined(CONFIG_SOC_IEEE802154_SUPPORTED)
81 return read_valid_mac(mac, esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, MAC_ADDRESS_SIZE_BITS));
83 return read_valid_mac(mac, esp_efuse_mac_get_custom(mac));
91#if defined(CONFIG_SOC_IEEE802154_SUPPORTED)
96 if (read_valid_mac(mac, esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, MAC_ADDRESS_SIZE_BITS))) {
100 if (read_valid_mac(mac, esp_efuse_mac_get_default(mac))) {
107 if (read_valid_mac(mac, esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, MAC_ADDRESS_SIZE_BITS))) {
108 ESP_LOGW(TAG,
"eFuse MAC CRC failed but raw bytes appear valid - using raw eFuse MAC");
113 ESP_LOGE(TAG,
"Failed to read a valid MAC address from eFuse");
114 memset(mac, 0, MAC_ADDRESS_SIZE);
120#if !defined(USE_ESP32_IGNORE_EFUSE_CUSTOM_MAC)
121 uint8_t mac[MAC_ADDRESS_SIZE];
123#ifndef USE_ESP32_VARIANT_ESP32
124 return (esp_efuse_read_field_blob(ESP_EFUSE_USER_DATA_MAC_CUSTOM, mac, MAC_ADDRESS_SIZE_BITS) == ESP_OK) &&
127 return (esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, MAC_ADDRESS_SIZE_BITS) == ESP_OK) &&
bool random_bytes(uint8_t *data, size_t len)
Generate len random bytes using the platform's secure RNG (hardware RNG or OS CSPRNG).
bool mac_address_is_valid(const uint8_t *mac)
Check if the MAC address is not all zeros or all ones.
bool has_custom_mac_address()
Check if a custom MAC address is set (ESP32 & variants)
void set_mac_address(uint8_t *mac)
Set the MAC address to use from the provided byte array (6 bytes).
void get_mac_address_raw(uint8_t *mac)
Get the device MAC address as raw bytes, written into the provided byte array (6 bytes).
bool get_custom_mac_address(uint8_t *mac)
Read the custom MAC address from eFuse into the provided byte array (6 bytes).