|
|
|
@ -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);
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|