@@ -339,16 +339,42 @@ const esp_phy_init_data_t* esp_phy_get_init_data(void)
339
339
ESP_LOGE (TAG , "failed to allocate memory for PHY init data" );
340
340
return NULL ;
341
341
}
342
+ // read phy data from flash
342
343
esp_err_t err = esp_partition_read (partition , 0 , init_data_store , init_data_store_length );
343
344
if (err != ESP_OK ) {
344
345
ESP_LOGE (TAG , "failed to read PHY data partition (0x%x)" , err );
346
+ free (init_data_store );
345
347
return NULL ;
346
348
}
349
+ // verify data
347
350
if (memcmp (init_data_store , PHY_INIT_MAGIC , sizeof (phy_init_magic_pre )) != 0 ||
348
351
memcmp (init_data_store + init_data_store_length - sizeof (phy_init_magic_post ),
349
352
PHY_INIT_MAGIC , sizeof (phy_init_magic_post )) != 0 ) {
353
+ #ifndef CONFIG_ESP32_PHY_DEFAULT_INIT_IF_INVALID
350
354
ESP_LOGE (TAG , "failed to validate PHY data partition" );
351
355
return NULL ;
356
+ #else
357
+ ESP_LOGE (TAG , "failed to validate PHY data partition, restoring default data into flash..." );
358
+
359
+ memcpy (init_data_store ,
360
+ PHY_INIT_MAGIC , sizeof (phy_init_magic_pre ));
361
+ memcpy (init_data_store + sizeof (phy_init_magic_pre ),
362
+ & phy_init_data , sizeof (phy_init_data ));
363
+ memcpy (init_data_store + sizeof (phy_init_magic_pre ) + sizeof (phy_init_data ),
364
+ PHY_INIT_MAGIC , sizeof (phy_init_magic_post ));
365
+
366
+ assert (memcmp (init_data_store , PHY_INIT_MAGIC , sizeof (phy_init_magic_pre )) == 0 );
367
+ assert (memcmp (init_data_store + init_data_store_length - sizeof (phy_init_magic_post ),
368
+ PHY_INIT_MAGIC , sizeof (phy_init_magic_post )) == 0 );
369
+
370
+ // write default data
371
+ err = esp_partition_write (partition , 0 , init_data_store , init_data_store_length );
372
+ if (err != ESP_OK ) {
373
+ ESP_LOGE (TAG , "failed to write default PHY data partition (0x%x)" , err );
374
+ free (init_data_store );
375
+ return NULL ;
376
+ }
377
+ #endif // CONFIG_ESP32_PHY_DEFAULT_INIT_IF_INVALID
352
378
}
353
379
#if CONFIG_ESP32_SUPPORT_MULTIPLE_PHY_INIT_DATA_BIN
354
380
if ((* (init_data_store + (sizeof (phy_init_magic_pre ) + PHY_SUPPORT_MULTIPLE_BIN_OFFSET )))) {
@@ -751,8 +777,8 @@ static esp_err_t phy_get_multiple_init_data(const esp_partition_t* partition,
751
777
752
778
err = phy_find_bin_data_according_type (init_data_store , init_data_control_info , init_data_multiple , init_data_type );
753
779
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 ;
780
+ ESP_LOGW (TAG , "%s has not been certified, use DEFAULT PHY init data" , s_phy_type [init_data_type ]);
781
+ s_phy_init_data_type = ESP_PHY_INIT_DATA_TYPE_DEFAULT ;
756
782
} else {
757
783
s_phy_init_data_type = init_data_type ;
758
784
}
0 commit comments