diff --git a/components/bootloader_support/include_priv/bootloader_config.h b/components/bootloader_support/include_priv/bootloader_config.h index dc5c6a6b..78160341 100644 --- a/components/bootloader_support/include_priv/bootloader_config.h +++ b/components/bootloader_support/include_priv/bootloader_config.h @@ -22,6 +22,7 @@ extern "C" { #endif +#include "sdkconfig.h" #include "esp_flash_data_types.h" #define SPI_SEC_SIZE 0x1000 @@ -34,6 +35,9 @@ typedef struct { esp_partition_pos_t ota_info; esp_partition_pos_t factory; esp_partition_pos_t test; +#ifdef CONFIG_LOAD_OLD_RF_PARAMETER + esp_partition_pos_t rf; +#endif esp_partition_pos_t ota[MAX_OTA_SLOTS]; uint32_t app_count; uint32_t selected_subtype; diff --git a/components/bootloader_support/src/bootloader_utility.c b/components/bootloader_support/src/bootloader_utility.c index 89eef476..d0631844 100644 --- a/components/bootloader_support/src/bootloader_utility.c +++ b/components/bootloader_support/src/bootloader_utility.c @@ -590,6 +590,9 @@ bool bootloader_utility_load_partition_table(bootloader_state_t* bs) partition_usage = "OTA data"; break; case PART_SUBTYPE_DATA_RF: +#ifdef CONFIG_LOAD_OLD_RF_PARAMETER + bs->rf = partition->pos; +#endif partition_usage = "RF data"; break; case PART_SUBTYPE_DATA_WIFI: diff --git a/components/esp8266/Kconfig b/components/esp8266/Kconfig index ad263a31..67bdcdb4 100644 --- a/components/esp8266/Kconfig +++ b/components/esp8266/Kconfig @@ -277,7 +277,7 @@ config INIT_OS_BEFORE_START FreeRTOS need not do this. config ESP8266_OTA_FROM_OLD - bool "(**Expected**)ESP8266 update from old SDK by OTA" + bool "ESP8266 update from old SDK by OTA" default n depends on IDF_TARGET_ESP8266 select ESP8266_BOOT_COPY_APP if ESPTOOLPY_FLASHSIZE_1MB @@ -289,8 +289,21 @@ config ESP8266_OTA_FROM_OLD The old RTOS SDK(before V3.0) or NonOS SDK can download the firmware to its partition and run it as it self's application. +config LOAD_OLD_RF_PARAMETER + bool "Load old RF Parameters" + default n + depends on ESP8266_OTA_FROM_OLD + select ESP_PHY_INIT_DATA_IN_PARTITION + help + The function is not released. + + Enable this option, after updating from old SDK to new SDK, bootloader will copy RF + parameters from old SDK partition to new SDK partition. + + Then application can read the RF parameters from new partition directly. + config ESP8266_BOOT_COPY_APP - bool "(**Expected**)Boot copy app" + bool "Boot copy app" default n help The function is not released. diff --git a/components/esp8266/Makefile.projbuild b/components/esp8266/Makefile.projbuild index f71f4a27..f8de9d8d 100644 --- a/components/esp8266/Makefile.projbuild +++ b/components/esp8266/Makefile.projbuild @@ -40,9 +40,9 @@ ESPTOOL_ALL_FLASH_ARGS += $(PHY_DATA_OFFSET) $(PHY_INIT_DATA_BIN) ESP8266_COMPONENT_PATH := $(COMPONENT_PATH) -$(PHY_INIT_DATA_OBJ): $(ESP8266_COMPONENT_PATH)/source/phy_init_data.h $(BUILD_DIR_BASE)/include/sdkconfig.h +$(PHY_INIT_DATA_OBJ): $(ESP8266_COMPONENT_PATH)/include/internal/phy_init_data.h $(BUILD_DIR_BASE)/include/sdkconfig.h $(summary) CC $(notdir $@) - printf "#include \"source/phy_init_data.h\"\n" | $(CC) -I $(BUILD_DIR_BASE)/include -I $(ESP8266_COMPONENT_PATH) -I $(ESP8266_COMPONENT_PATH)/include -c -o $@ -xc - + printf "#include \"internal/phy_init_data.h\"\n" | $(CC) -I $(BUILD_DIR_BASE)/include -I $(ESP8266_COMPONENT_PATH) -I $(ESP8266_COMPONENT_PATH)/include -c -o $@ -xc - $(PHY_INIT_DATA_BIN): $(PHY_INIT_DATA_OBJ) $(summary) BIN $(notdir $@) @@ -120,7 +120,7 @@ endif PYTHON ?= $(call dequote,$(CONFIG_PYTHON)) ifdef CONFIG_ESP8266_OTA_FROM_OLD -$(OTA_V2_TO_V3_BIN): all_binaries +$(OTA_V2_TO_V3_BIN): all @$(PYTHON) $(IDF_PATH)/tools/pack_fw.py --output $(OTA_V2_TO_V3_BIN) --app $(PROJECT_NAME).bin pack3 $(ESPTOOL_ALL_FLASH_ARGS) @echo [GEN] $(OTA_V2_TO_V3_BIN) endif diff --git a/components/esp8266/source/phy_init_data.h b/components/esp8266/include/internal/phy_init_data.h similarity index 100% rename from components/esp8266/source/phy_init_data.h rename to components/esp8266/include/internal/phy_init_data.h diff --git a/components/esp8266/source/phy_init.c b/components/esp8266/source/phy_init.c index ec26e8bf..7f7e9437 100644 --- a/components/esp8266/source/phy_init.c +++ b/components/esp8266/source/phy_init.c @@ -27,7 +27,7 @@ #include "nvs_flash.h" #include "sdkconfig.h" -#include "phy_init_data.h" +#include "internal/phy_init_data.h" #include "phy.h" static const char *TAG = "phy_init"; diff --git a/components/spi_flash/port/port.c b/components/spi_flash/port/port.c index 527e60c6..13816c0d 100644 --- a/components/spi_flash/port/port.c +++ b/components/spi_flash/port/port.c @@ -26,6 +26,7 @@ #include "esp_libc.h" #include "esp8266/rom_functions.h" #include "esp8266/eagle_soc.h" +#include "internal/phy_init_data.h" #define PARTITION_DATA_OFFSET (s_v2_flash_bin_size / 2) @@ -65,6 +66,7 @@ typedef union s_boot_param { static const char *TAG = "partition_port"; static uint32_t s_partition_offset; static ROM_FLASH_BUF_DECLARE(s_cache_buf, SPI_FLASH_SEC_SIZE); +static ROM_FLASH_BUF_DECLARE(check_buf, SPI_FLASH_SEC_SIZE); static uint32_t s_v2_flash_bin_size; static uint32_t s_v2_flash_size; static sys_param_t s_sys_param; @@ -109,12 +111,12 @@ static const uint32_t s_v2_flash_bin_map_table[] = { static const uint32_t s_v2_flash_bin_size_map_size = sizeof(s_v2_flash_bin_map_table) / sizeof(s_v2_flash_bin_map_table[0]); -static inline void esp_hw_reset(void) +static void esp_hw_reset(uint32_t t, int block) { CLEAR_WDT_REG_MASK(WDT_CTL_ADDRESS, BIT0); - WDT_REG_WRITE(WDT_OP_ADDRESS, 8); - WDT_REG_WRITE(WDT_OP_ND_ADDRESS, 8); + WDT_REG_WRITE(WDT_OP_ADDRESS, t); + WDT_REG_WRITE(WDT_OP_ND_ADDRESS, t); SET_PERI_REG_BITS(PERIPHS_WDT_BASEADDR + WDT_CTL_ADDRESS, WDT_CTL_RSTLEN_MASK, 7 << WDT_CTL_RSTLEN_LSB, 0); // interrupt then reset @@ -122,10 +124,18 @@ static inline void esp_hw_reset(void) // start task watch dog1 SET_PERI_REG_BITS(PERIPHS_WDT_BASEADDR + WDT_CTL_ADDRESS, WDT_CTL_EN_MASK, 1 << WDT_CTL_EN_LSB, 0); - while (1); + WDT_FEED(); + + if (block) + while (1); } -static inline int spi_flash_read_data(uint32_t addr, void *buf, size_t n) +static void esp_hw_feed_wdt(void) +{ + WDT_FEED(); +} + +static int spi_flash_read_data(uint32_t addr, void *buf, size_t n) { int ret; @@ -138,10 +148,12 @@ static inline int spi_flash_read_data(uint32_t addr, void *buf, size_t n) ret = SPI_read_data(&s_flash_chip, addr, buf, n); + esp_hw_feed_wdt(); + return ret; } -static inline int spi_flash_write_data(uint32_t addr, const void *buf, uint32_t n) +static int spi_flash_write_data(uint32_t addr, const void *buf, uint32_t n) { int ret; @@ -154,10 +166,12 @@ static inline int spi_flash_write_data(uint32_t addr, const void *buf, uint32_t ret = SPIWrite(addr, (void *)buf, n); + esp_hw_feed_wdt(); + return ret; } -static inline int spi_flash_erase(uint32_t addr) +static int spi_flash_erase(uint32_t addr) { int ret; @@ -165,10 +179,12 @@ static inline int spi_flash_erase(uint32_t addr) ret = SPIEraseSector(addr / SPI_FLASH_SEC_SIZE); + esp_hw_feed_wdt(); + return ret; } -static inline int get_v2_flash_map_size(void) +static int get_v2_flash_map_size(void) { int ret; esp_image_header_t header; @@ -197,7 +213,7 @@ static inline int get_v2_flash_map_size(void) return 0; } -static inline uint32_t esp_get_updated_partition_table_addr(void) +static uint32_t esp_get_updated_partition_table_addr(void) { int ret; size_t offset; @@ -259,10 +275,9 @@ static inline uint32_t esp_get_updated_partition_table_addr(void) return s_partition_offset; } -static inline int spi_flash_write_data_safe(uint32_t addr, const void *buf, size_t n) +static int spi_flash_write_data_safe(uint32_t addr, const void *buf, size_t n) { - int ret; - static ROM_FLASH_BUF_DECLARE(check_buf, SPI_FLASH_SEC_SIZE); + int ret; ret = spi_flash_erase(addr); if (ret) { @@ -313,7 +328,79 @@ static int esp_flash_sector_copy(uint32_t dest, uint32_t src, uint32_t total_siz return 0; } -static inline int esp_set_v2boot_app1(void) +#ifdef CONFIG_LOAD_OLD_RF_PARAMETER + +static bool esp_rf_param_need_unpack(size_t rf_dest, size_t rf_src, size_t rf_size) +{ + bool ret; + const uint32_t rf_magic_size = sizeof(phy_init_magic_pre); + const uint32_t rf_new_size = rf_size + rf_magic_size * 2; + + ret = spi_flash_read_data(rf_dest, check_buf, rf_new_size); + if (ret) { + ESP_LOGE(TAG, "Fail to read RF parameters from 0x%x", rf_src); + return -1; + } + + ret = spi_flash_read_data(rf_src, ((uint8_t *)s_cache_buf) + rf_magic_size, rf_size); + if (ret) { + ESP_LOGE(TAG, "Fail to read RF parameters from 0x%x", rf_src); + return -1; + } + memcpy(s_cache_buf, phy_init_magic_pre, rf_magic_size); + memcpy(((uint8_t *)s_cache_buf) + rf_magic_size + rf_size, phy_init_magic_pre, rf_magic_size); + + if (!memcmp(check_buf, s_cache_buf, rf_new_size)) + ret = false; + else + ret = true; + + return ret; +} + +static int esp_unpack_old_rf_param(size_t rf_dest, size_t rf_src, size_t rf_size) +{ + int ret; + const uint32_t rf_magic_size = sizeof(phy_init_magic_pre); + const uint32_t rf_new_size = rf_size + rf_magic_size * 2; + + ret = spi_flash_erase(rf_dest); + if (ret) { + ESP_LOGE(TAG, "Fail to erase new RF parameters from 0x%x", rf_src); + return -1; + } + + ret = spi_flash_read_data(rf_src, ((uint8_t *)s_cache_buf) + rf_magic_size, rf_size); + if (ret) { + ESP_LOGE(TAG, "Fail to read old RF parameters from 0x%x", rf_src); + return -1; + } + memcpy(s_cache_buf, phy_init_magic_pre, rf_magic_size); + memcpy(((uint8_t *)s_cache_buf) + rf_magic_size + rf_size, phy_init_magic_pre, rf_magic_size); + + ret = spi_flash_write_data(rf_dest, s_cache_buf , rf_new_size); + if (ret) { + ESP_LOGE(TAG, "Fail to write new RF parameters from 0x%x", rf_src); + return -1; + } + + ret = spi_flash_read_data(rf_dest, check_buf, rf_new_size); + if (ret) { + ESP_LOGE(TAG, "Fail to read new RF parameters from 0x%x", rf_src); + return -1; + } + + if (memcmp(check_buf, s_cache_buf, rf_new_size)) { + ESP_LOGE(TAG, "Check RF parameters error"); + return -1; + } + + return 0; +} + +#endif + +static int esp_set_v2boot_app1(void) { int ret; const size_t offset = s_sys_param.flag ? 1 : 0; @@ -360,7 +447,7 @@ static inline int esp_set_v2boot_app1(void) return 0; } -static inline int esp_sdk_update_from_v2(void) +static int esp_sdk_update_from_v2(void) { const int segment_cnt = 3; const size_t v2_max_size = 4096; @@ -397,6 +484,8 @@ int esp_patition_table_init_location(void) { uint32_t addr; + esp_hw_reset(14, 0); + if (!esp_sdk_update_from_v2()) return 0; @@ -433,6 +522,7 @@ int esp_patition_copy_ota1_to_ota0(const void *partition_info) int esp_patition_table_init_data(void *partition_info) { int ret; + int reboot = 0; const uint32_t boot_base = 0x1000; const bootloader_state_t *bs = (const bootloader_state_t *)partition_info; const uint32_t boot_size = bs->ota[0].offset + bs->ota[0].size - boot_base; @@ -440,29 +530,52 @@ int esp_patition_table_init_data(void *partition_info) if (!esp_sdk_update_from_v2()) return 0; - if (esp_get_updated_partition_table_addr() == CONFIG_PARTITION_TABLE_OFFSET) - return 0; + if (esp_get_updated_partition_table_addr() != CONFIG_PARTITION_TABLE_OFFSET) { + ESP_LOGD(TAG, "Copy firmware1 from %d total %d", boot_base + PARTITION_DATA_OFFSET, boot_size); - ESP_LOGD(TAG, "Copy firmware1 from %d total %d", boot_base + PARTITION_DATA_OFFSET, boot_size); + ESP_LOGI(TAG, "Start unpacking V3 firmware ..."); - ESP_LOGI(TAG, "Start unpacking V3 firmware ..."); + ret = esp_flash_sector_copy(boot_base, boot_base + PARTITION_DATA_OFFSET, boot_size); + if (ret) { + ESP_LOGE(TAG, "Fail to copy V3 firmware from 0x%x to 0x%x total %d", boot_base + PARTITION_DATA_OFFSET, + boot_base, boot_size); + return -1; + } - ret = esp_flash_sector_copy(boot_base, boot_base + PARTITION_DATA_OFFSET, boot_size); - if (ret) { - ESP_LOGE(TAG, "Fail to copy V3 firmware from 0x%x to 0x%x total %d", boot_base + PARTITION_DATA_OFFSET, - boot_base, boot_size); - return -1; + ret = esp_set_v2boot_app1(); + if (ret) { + ESP_LOGE(TAG, "Fail to set V2 app1 default"); + return -1; + } + + ESP_LOGI(TAG, "Pack V3 firmware successfully and start to reboot"); + + reboot = 1; } - ret = esp_set_v2boot_app1(); - if (ret) { - ESP_LOGE(TAG, "Fail to set V2 app1 default"); - return -1; +#ifdef CONFIG_LOAD_OLD_RF_PARAMETER + const uint32_t rf_dest = bs->rf.offset; + const uint32_t rf_src = s_v2_flash_bin_size - 4 * 4096; + const uint32_t rf_size = 128; + + if (esp_rf_param_need_unpack(rf_dest, rf_src, rf_size)) { + + ESP_LOGI(TAG, "Start unpacking RF parameters from 0x%x to 0x%x total size 0x%x ...", rf_src, rf_dest, rf_size); + + ret = esp_unpack_old_rf_param(rf_dest, rf_src, rf_size); + if (ret) { + ESP_LOGE(TAG, "Fail to copy RF parameters from 0x%x to 0x%x total %d", rf_src, rf_dest, rf_size); + return -1; + } + + reboot = 1; + } else { + ESP_LOGD(TAG, "RF parameters is same"); } +#endif - ESP_LOGI(TAG, "Pack V3 firmware successfully and start to reboot"); - - esp_hw_reset(); + if (reboot) + esp_hw_reset(4, 1); return 0; }