@@ -339,16 +339,43 @@ const esp_phy_init_data_t* esp_phy_get_init_data(void)
339339 ESP_LOGE (TAG , "failed to allocate memory for PHY init data" );
340340 return NULL ;
341341 }
342+ // read phy data from flash
342343 esp_err_t err = esp_partition_read (partition , 0 , init_data_store , init_data_store_length );
343344 if (err != ESP_OK ) {
344345 ESP_LOGE (TAG , "failed to read PHY data partition (0x%x)" , err );
346+ free (init_data_store );
345347 return NULL ;
346348 }
349+ // verify data
347350 if (memcmp (init_data_store , PHY_INIT_MAGIC , sizeof (phy_init_magic_pre )) != 0 ||
348351 memcmp (init_data_store + init_data_store_length - sizeof (phy_init_magic_post ),
349352 PHY_INIT_MAGIC , sizeof (phy_init_magic_post )) != 0 ) {
353+ #ifndef CONFIG_ESP32_PHY_DEFAULT_INIT_IF_INVALID
350354 ESP_LOGE (TAG , "failed to validate PHY data partition" );
355+ free (init_data_store );
351356 return NULL ;
357+ #else
358+ ESP_LOGE (TAG , "failed to validate PHY data partition, restoring default data into flash..." );
359+
360+ memcpy (init_data_store ,
361+ PHY_INIT_MAGIC , sizeof (phy_init_magic_pre ));
362+ memcpy (init_data_store + sizeof (phy_init_magic_pre ),
363+ & phy_init_data , sizeof (phy_init_data ));
364+ memcpy (init_data_store + sizeof (phy_init_magic_pre ) + sizeof (phy_init_data ),
365+ PHY_INIT_MAGIC , sizeof (phy_init_magic_post ));
366+
367+ assert (memcmp (init_data_store , PHY_INIT_MAGIC , sizeof (phy_init_magic_pre )) == 0 );
368+ assert (memcmp (init_data_store + init_data_store_length - sizeof (phy_init_magic_post ),
369+ PHY_INIT_MAGIC , sizeof (phy_init_magic_post )) == 0 );
370+
371+ // write default data
372+ err = esp_partition_write (partition , 0 , init_data_store , init_data_store_length );
373+ if (err != ESP_OK ) {
374+ ESP_LOGE (TAG , "failed to write default PHY data partition (0x%x)" , err );
375+ free (init_data_store );
376+ return NULL ;
377+ }
378+ #endif // CONFIG_ESP32_PHY_DEFAULT_INIT_IF_INVALID
352379 }
353380#if CONFIG_ESP32_SUPPORT_MULTIPLE_PHY_INIT_DATA_BIN
354381 if ((* (init_data_store + (sizeof (phy_init_magic_pre ) + PHY_SUPPORT_MULTIPLE_BIN_OFFSET )))) {
@@ -751,8 +778,8 @@ static esp_err_t phy_get_multiple_init_data(const esp_partition_t* partition,
751778
752779 err = phy_find_bin_data_according_type (init_data_store , init_data_control_info , init_data_multiple , init_data_type );
753780 if (err != ESP_OK ) {
754- ESP_LOGW (TAG , "%s has not been certified, use DEFAULT PHY init data" , s_phy_type [init_data_type ]);
755- s_phy_init_data_type = ESP_PHY_INIT_DATA_TYPE_DEFAULT ;
781+ ESP_LOGW (TAG , "%s has not been certified, use DEFAULT PHY init data" , s_phy_type [init_data_type ]);
782+ s_phy_init_data_type = ESP_PHY_INIT_DATA_TYPE_DEFAULT ;
756783 } else {
757784 s_phy_init_data_type = init_data_type ;
758785 }
@@ -829,18 +856,19 @@ esp_err_t esp_phy_update_country_info(const char *country)
829856{
830857#if CONFIG_ESP32_SUPPORT_MULTIPLE_PHY_INIT_DATA_BIN
831858 uint8_t phy_init_data_type_map = 0 ;
832- //if country equal s_phy_current_country, return;
833- if (!memcmp (country , s_phy_current_country , sizeof (s_phy_current_country ))) {
834- return ESP_OK ;
835- }
836-
837- memcpy (s_phy_current_country , country , sizeof (s_phy_current_country ));
838859
839860 if (!s_multiple_phy_init_data_bin ) {
840861 ESP_LOGD (TAG , "Does not support multiple PHY init data bins" );
841862 return ESP_FAIL ;
842863 }
843864
865+ //if country equal s_phy_current_country, return;
866+ if (!memcmp (country , s_phy_current_country , sizeof (s_phy_current_country ))) {
867+ return ESP_OK ;
868+ }
869+
870+ memcpy (s_phy_current_country , country , sizeof (s_phy_current_country ));
871+
844872 phy_init_data_type_map = phy_find_bin_type_according_country (country );
845873 if (phy_init_data_type_map == s_phy_init_data_type ) {
846874 return ESP_OK ;
0 commit comments