Merge branch 'fix/phy_init_variables_in_header_file' into 'master'

fix(esp_phy): header file produces non-zero object

See merge request espressif/esp-idf!29930
This commit is contained in:
Zhang Wen Xu
2024-04-02 16:50:54 +08:00
17 changed files with 1173 additions and 1095 deletions

View File

@@ -35,6 +35,10 @@
#include "soc/rtc_periph.h"
#if CONFIG_ESP_PHY_INIT_DATA_IN_PARTITION
#include "esp_partition.h"
#endif
#if __has_include("soc/syscon_reg.h")
#include "soc/syscon_reg.h"
#endif
@@ -527,13 +531,12 @@ IRAM_ATTR void esp_mac_bb_power_down(void)
// PHY init data handling functions
#if CONFIG_ESP_PHY_INIT_DATA_IN_PARTITION
#include "esp_partition.h"
const esp_phy_init_data_t* esp_phy_get_init_data(void)
{
#if CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN_EMBED
size_t init_data_store_length = sizeof(phy_init_magic_pre) +
sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post);
size_t init_data_store_length = PHY_INIT_MAGIC_LEN +
sizeof(esp_phy_init_data_t) + PHY_INIT_MAGIC_LEN;
uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length);
if (init_data_store == NULL) {
ESP_LOGE(TAG, "failed to allocate memory for updated country code PHY init data");
@@ -549,8 +552,8 @@ const esp_phy_init_data_t* esp_phy_get_init_data(void)
return NULL;
}
ESP_LOGD(TAG, "loading PHY init data from partition at offset 0x%" PRIx32 "", partition->address);
size_t init_data_store_length = sizeof(phy_init_magic_pre) +
sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post);
size_t init_data_store_length = PHY_INIT_MAGIC_LEN +
sizeof(esp_phy_init_data_t) + PHY_INIT_MAGIC_LEN;
uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length);
if (init_data_store == NULL) {
ESP_LOGE(TAG, "failed to allocate memory for PHY init data");
@@ -565,9 +568,9 @@ const esp_phy_init_data_t* esp_phy_get_init_data(void)
}
#endif
// verify data
if (memcmp(init_data_store, PHY_INIT_MAGIC, sizeof(phy_init_magic_pre)) != 0 ||
memcmp(init_data_store + init_data_store_length - sizeof(phy_init_magic_post),
PHY_INIT_MAGIC, sizeof(phy_init_magic_post)) != 0) {
if (memcmp(init_data_store, PHY_INIT_MAGIC, PHY_INIT_MAGIC_LEN) != 0 ||
memcmp(init_data_store + init_data_store_length - PHY_INIT_MAGIC_LEN,
PHY_INIT_MAGIC, PHY_INIT_MAGIC_LEN) != 0) {
#if CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN_EMBED
ESP_LOGE(TAG, "failed to validate embedded PHY init data");
free(init_data_store);
@@ -581,15 +584,15 @@ const esp_phy_init_data_t* esp_phy_get_init_data(void)
ESP_LOGE(TAG, "failed to validate PHY data partition, restoring default data into flash...");
memcpy(init_data_store,
PHY_INIT_MAGIC, sizeof(phy_init_magic_pre));
memcpy(init_data_store + sizeof(phy_init_magic_pre),
PHY_INIT_MAGIC, PHY_INIT_MAGIC_LEN);
memcpy(init_data_store + PHY_INIT_MAGIC_LEN,
&phy_init_data, sizeof(phy_init_data));
memcpy(init_data_store + sizeof(phy_init_magic_pre) + sizeof(phy_init_data),
PHY_INIT_MAGIC, sizeof(phy_init_magic_post));
memcpy(init_data_store + PHY_INIT_MAGIC_LEN + sizeof(phy_init_data),
PHY_INIT_MAGIC, PHY_INIT_MAGIC_LEN);
assert(memcmp(init_data_store, PHY_INIT_MAGIC, sizeof(phy_init_magic_pre)) == 0);
assert(memcmp(init_data_store + init_data_store_length - sizeof(phy_init_magic_post),
PHY_INIT_MAGIC, sizeof(phy_init_magic_post)) == 0);
assert(memcmp(init_data_store, PHY_INIT_MAGIC, PHY_INIT_MAGIC_LEN) == 0);
assert(memcmp(init_data_store + init_data_store_length - PHY_INIT_MAGIC_LEN,
PHY_INIT_MAGIC, PHY_INIT_MAGIC_LEN) == 0);
// write default data
err = esp_partition_write(partition, 0, init_data_store, init_data_store_length);
@@ -602,7 +605,7 @@ const esp_phy_init_data_t* esp_phy_get_init_data(void)
#endif // CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN_EMBED
}
#if CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN
if ((*(init_data_store + (sizeof(phy_init_magic_pre) + PHY_SUPPORT_MULTIPLE_BIN_OFFSET)))) {
if ((*(init_data_store + (PHY_INIT_MAGIC_LEN + PHY_SUPPORT_MULTIPLE_BIN_OFFSET)))) {
s_multiple_phy_init_data_bin = true;
ESP_LOGI(TAG, "Support multiple PHY init data bins");
} else {
@@ -610,12 +613,12 @@ const esp_phy_init_data_t* esp_phy_get_init_data(void)
}
#endif
ESP_LOGD(TAG, "PHY data partition validated");
return (const esp_phy_init_data_t*) (init_data_store + sizeof(phy_init_magic_pre));
return (const esp_phy_init_data_t*) (init_data_store + PHY_INIT_MAGIC_LEN);
}
void esp_phy_release_init_data(const esp_phy_init_data_t* init_data)
{
free((uint8_t*) init_data - sizeof(phy_init_magic_pre));
free((uint8_t*) init_data - PHY_INIT_MAGIC_LEN);
}
#else // CONFIG_ESP_PHY_INIT_DATA_IN_PARTITION
@@ -947,7 +950,7 @@ static esp_err_t phy_find_bin_data_according_type(uint8_t* out_init_data_store,
int i = 0;
for (i = 0; i < init_data_control_info->number; i++) {
if (init_data_type == *(init_data_multiple + (i * sizeof(esp_phy_init_data_t)) + PHY_INIT_DATA_TYPE_OFFSET)) {
memcpy(out_init_data_store + sizeof(phy_init_magic_pre),
memcpy(out_init_data_store + PHY_INIT_MAGIC_LEN,
init_data_multiple + (i * sizeof(esp_phy_init_data_t)), sizeof(esp_phy_init_data_t));
break;
}
@@ -1047,8 +1050,8 @@ esp_err_t esp_phy_update_init_data(phy_init_data_type_t init_data_type)
#if CONFIG_ESP_PHY_MULTIPLE_INIT_DATA_BIN_EMBED
esp_err_t err = ESP_OK;
const esp_partition_t* partition = NULL;
size_t init_data_store_length = sizeof(phy_init_magic_pre) +
sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post);
size_t init_data_store_length = PHY_INIT_MAGIC_LEN +
sizeof(esp_phy_init_data_t) + PHY_INIT_MAGIC_LEN;
uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length);
if (init_data_store == NULL) {
ESP_LOGE(TAG, "failed to allocate memory for updated country code PHY init data");
@@ -1063,8 +1066,8 @@ esp_err_t esp_phy_update_init_data(phy_init_data_type_t init_data_type)
ESP_LOGE(TAG, "Updated country code PHY data partition not found");
return ESP_FAIL;
}
size_t init_data_store_length = sizeof(phy_init_magic_pre) +
sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post);
size_t init_data_store_length = PHY_INIT_MAGIC_LEN +
sizeof(esp_phy_init_data_t) + PHY_INIT_MAGIC_LEN;
uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length);
if (init_data_store == NULL) {
ESP_LOGE(TAG, "failed to allocate memory for updated country code PHY init data");
@@ -1078,9 +1081,9 @@ esp_err_t esp_phy_update_init_data(phy_init_data_type_t init_data_type)
return ESP_FAIL;
}
#endif
if (memcmp(init_data_store, PHY_INIT_MAGIC, sizeof(phy_init_magic_pre)) != 0 ||
memcmp(init_data_store + init_data_store_length - sizeof(phy_init_magic_post),
PHY_INIT_MAGIC, sizeof(phy_init_magic_post)) != 0) {
if (memcmp(init_data_store, PHY_INIT_MAGIC, PHY_INIT_MAGIC_LEN) != 0 ||
memcmp(init_data_store + init_data_store_length - PHY_INIT_MAGIC_LEN,
PHY_INIT_MAGIC, PHY_INIT_MAGIC_LEN) != 0) {
free(init_data_store);
ESP_LOGE(TAG, "failed to validate updated country code PHY data partition");
return ESP_FAIL;
@@ -1102,7 +1105,7 @@ esp_err_t esp_phy_update_init_data(phy_init_data_type_t init_data_type)
}
if (s_current_apply_phy_init_data != s_phy_init_data_type) {
err = esp_phy_apply_phy_init_data(init_data_store + sizeof(phy_init_magic_pre));
err = esp_phy_apply_phy_init_data(init_data_store + PHY_INIT_MAGIC_LEN);
if (err != ESP_OK) {
ESP_LOGE(TAG, "PHY init data failed to load");
free(init_data_store);