Skip to content

Commit 63a7a84

Browse files
0xFEEDC0DE64HarveyRong-Esp
authored andcommitted
Init phy data to default if invalid in flash partition to avoid bootloops
Signed-off-by: ronghulin <[email protected]> Merges #6610
1 parent 3373eff commit 63a7a84

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

components/esp_wifi/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,16 @@ menu "PHY"
430430

431431
If unsure, choose 'n'.
432432

433+
config ESP32_PHY_DEFAULT_INIT_IF_INVALID
434+
bool "Reset default PHY init data if invalid"
435+
default n
436+
depends on ESP32_PHY_INIT_DATA_IN_PARTITION
437+
help
438+
If enabled, PHY init data will be restored to default if
439+
it cannot be verified successfully to avoid endless bootloops.
440+
441+
If unsure, choose 'n'.
442+
433443
if ESP32_PHY_INIT_DATA_IN_PARTITION
434444
config ESP32_SUPPORT_MULTIPLE_PHY_INIT_DATA_BIN
435445
bool "Support multiple PHY init data bin"

components/esp_wifi/src/phy_init.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,16 +339,42 @@ 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");
351355
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
352378
}
353379
#if CONFIG_ESP32_SUPPORT_MULTIPLE_PHY_INIT_DATA_BIN
354380
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,
751777

752778
err = phy_find_bin_data_according_type(init_data_store, init_data_control_info, init_data_multiple, init_data_type);
753779
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;
756782
} else {
757783
s_phy_init_data_type = init_data_type;
758784
}

0 commit comments

Comments
 (0)