2626#include "esp_libc.h"
2727#include "esp8266/rom_functions.h"
2828#include "esp8266/eagle_soc.h"
29+ #include "internal/phy_init_data.h"
2930
3031#define PARTITION_DATA_OFFSET (s_v2_flash_bin_size / 2)
3132
@@ -65,6 +66,7 @@ typedef union s_boot_param {
6566static const char * TAG = "partition_port" ;
6667static uint32_t s_partition_offset ;
6768static ROM_FLASH_BUF_DECLARE (s_cache_buf , SPI_FLASH_SEC_SIZE ) ;
69+ static ROM_FLASH_BUF_DECLARE (check_buf , SPI_FLASH_SEC_SIZE ) ;
6870static uint32_t s_v2_flash_bin_size ;
6971static uint32_t s_v2_flash_size ;
7072static sys_param_t s_sys_param ;
@@ -109,23 +111,31 @@ static const uint32_t s_v2_flash_bin_map_table[] = {
109111
110112static 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 ]);
111113
112- static inline void esp_hw_reset (void )
114+ static void esp_hw_reset (uint32_t t , int block )
113115{
114116 CLEAR_WDT_REG_MASK (WDT_CTL_ADDRESS , BIT0 );
115117
116- WDT_REG_WRITE (WDT_OP_ADDRESS , 8 );
117- WDT_REG_WRITE (WDT_OP_ND_ADDRESS , 8 );
118+ WDT_REG_WRITE (WDT_OP_ADDRESS , t );
119+ WDT_REG_WRITE (WDT_OP_ND_ADDRESS , t );
118120
119121 SET_PERI_REG_BITS (PERIPHS_WDT_BASEADDR + WDT_CTL_ADDRESS , WDT_CTL_RSTLEN_MASK , 7 << WDT_CTL_RSTLEN_LSB , 0 );
120122 // interrupt then reset
121123 SET_PERI_REG_BITS (PERIPHS_WDT_BASEADDR + WDT_CTL_ADDRESS , WDT_CTL_RSPMOD_MASK , 0 << WDT_CTL_RSPMOD_LSB , 0 );
122124 // start task watch dog1
123125 SET_PERI_REG_BITS (PERIPHS_WDT_BASEADDR + WDT_CTL_ADDRESS , WDT_CTL_EN_MASK , 1 << WDT_CTL_EN_LSB , 0 );
124126
125- while (1 );
127+ WDT_FEED ();
128+
129+ if (block )
130+ while (1 );
126131}
127132
128- static inline int spi_flash_read_data (uint32_t addr , void * buf , size_t n )
133+ static void esp_hw_feed_wdt (void )
134+ {
135+ WDT_FEED ();
136+ }
137+
138+ static int spi_flash_read_data (uint32_t addr , void * buf , size_t n )
129139{
130140 int ret ;
131141
@@ -138,10 +148,12 @@ static inline int spi_flash_read_data(uint32_t addr, void *buf, size_t n)
138148
139149 ret = SPI_read_data (& s_flash_chip , addr , buf , n );
140150
151+ esp_hw_feed_wdt ();
152+
141153 return ret ;
142154}
143155
144- static inline int spi_flash_write_data (uint32_t addr , const void * buf , uint32_t n )
156+ static int spi_flash_write_data (uint32_t addr , const void * buf , uint32_t n )
145157{
146158 int ret ;
147159
@@ -154,21 +166,25 @@ static inline int spi_flash_write_data(uint32_t addr, const void *buf, uint32_t
154166
155167 ret = SPIWrite (addr , (void * )buf , n );
156168
169+ esp_hw_feed_wdt ();
170+
157171 return ret ;
158172}
159173
160- static inline int spi_flash_erase (uint32_t addr )
174+ static int spi_flash_erase (uint32_t addr )
161175{
162176 int ret ;
163177
164178 ESP_LOGD (TAG , "erase addr is 0x%x" , addr );
165179
166180 ret = SPIEraseSector (addr / SPI_FLASH_SEC_SIZE );
167181
182+ esp_hw_feed_wdt ();
183+
168184 return ret ;
169185}
170186
171- static inline int get_v2_flash_map_size (void )
187+ static int get_v2_flash_map_size (void )
172188{
173189 int ret ;
174190 esp_image_header_t header ;
@@ -197,7 +213,7 @@ static inline int get_v2_flash_map_size(void)
197213 return 0 ;
198214}
199215
200- static inline uint32_t esp_get_updated_partition_table_addr (void )
216+ static uint32_t esp_get_updated_partition_table_addr (void )
201217{
202218 int ret ;
203219 size_t offset ;
@@ -259,10 +275,9 @@ static inline uint32_t esp_get_updated_partition_table_addr(void)
259275 return s_partition_offset ;
260276}
261277
262- static inline int spi_flash_write_data_safe (uint32_t addr , const void * buf , size_t n )
278+ static int spi_flash_write_data_safe (uint32_t addr , const void * buf , size_t n )
263279{
264- int ret ;
265- static ROM_FLASH_BUF_DECLARE (check_buf , SPI_FLASH_SEC_SIZE );
280+ int ret ;
266281
267282 ret = spi_flash_erase (addr );
268283 if (ret ) {
@@ -313,7 +328,79 @@ static int esp_flash_sector_copy(uint32_t dest, uint32_t src, uint32_t total_siz
313328 return 0 ;
314329}
315330
316- static inline int esp_set_v2boot_app1 (void )
331+ #ifdef CONFIG_LOAD_OLD_RF_PARAMETER
332+
333+ static bool esp_rf_param_need_unpack (size_t rf_dest , size_t rf_src , size_t rf_size )
334+ {
335+ bool ret ;
336+ const uint32_t rf_magic_size = sizeof (phy_init_magic_pre );
337+ const uint32_t rf_new_size = rf_size + rf_magic_size * 2 ;
338+
339+ ret = spi_flash_read_data (rf_dest , check_buf , rf_new_size );
340+ if (ret ) {
341+ ESP_LOGE (TAG , "Fail to read RF parameters from 0x%x" , rf_src );
342+ return -1 ;
343+ }
344+
345+ ret = spi_flash_read_data (rf_src , ((uint8_t * )s_cache_buf ) + rf_magic_size , rf_size );
346+ if (ret ) {
347+ ESP_LOGE (TAG , "Fail to read RF parameters from 0x%x" , rf_src );
348+ return -1 ;
349+ }
350+ memcpy (s_cache_buf , phy_init_magic_pre , rf_magic_size );
351+ memcpy (((uint8_t * )s_cache_buf ) + rf_magic_size + rf_size , phy_init_magic_pre , rf_magic_size );
352+
353+ if (!memcmp (check_buf , s_cache_buf , rf_new_size ))
354+ ret = false;
355+ else
356+ ret = true;
357+
358+ return ret ;
359+ }
360+
361+ static int esp_unpack_old_rf_param (size_t rf_dest , size_t rf_src , size_t rf_size )
362+ {
363+ int ret ;
364+ const uint32_t rf_magic_size = sizeof (phy_init_magic_pre );
365+ const uint32_t rf_new_size = rf_size + rf_magic_size * 2 ;
366+
367+ ret = spi_flash_erase (rf_dest );
368+ if (ret ) {
369+ ESP_LOGE (TAG , "Fail to erase new RF parameters from 0x%x" , rf_src );
370+ return -1 ;
371+ }
372+
373+ ret = spi_flash_read_data (rf_src , ((uint8_t * )s_cache_buf ) + rf_magic_size , rf_size );
374+ if (ret ) {
375+ ESP_LOGE (TAG , "Fail to read old RF parameters from 0x%x" , rf_src );
376+ return -1 ;
377+ }
378+ memcpy (s_cache_buf , phy_init_magic_pre , rf_magic_size );
379+ memcpy (((uint8_t * )s_cache_buf ) + rf_magic_size + rf_size , phy_init_magic_pre , rf_magic_size );
380+
381+ ret = spi_flash_write_data (rf_dest , s_cache_buf , rf_new_size );
382+ if (ret ) {
383+ ESP_LOGE (TAG , "Fail to write new RF parameters from 0x%x" , rf_src );
384+ return -1 ;
385+ }
386+
387+ ret = spi_flash_read_data (rf_dest , check_buf , rf_new_size );
388+ if (ret ) {
389+ ESP_LOGE (TAG , "Fail to read new RF parameters from 0x%x" , rf_src );
390+ return -1 ;
391+ }
392+
393+ if (memcmp (check_buf , s_cache_buf , rf_new_size )) {
394+ ESP_LOGE (TAG , "Check RF parameters error" );
395+ return -1 ;
396+ }
397+
398+ return 0 ;
399+ }
400+
401+ #endif
402+
403+ static int esp_set_v2boot_app1 (void )
317404{
318405 int ret ;
319406 const size_t offset = s_sys_param .flag ? 1 : 0 ;
@@ -360,7 +447,7 @@ static inline int esp_set_v2boot_app1(void)
360447 return 0 ;
361448}
362449
363- static inline int esp_sdk_update_from_v2 (void )
450+ static int esp_sdk_update_from_v2 (void )
364451{
365452 const int segment_cnt = 3 ;
366453 const size_t v2_max_size = 4096 ;
@@ -397,6 +484,8 @@ int esp_patition_table_init_location(void)
397484{
398485 uint32_t addr ;
399486
487+ esp_hw_reset (14 , 0 );
488+
400489 if (!esp_sdk_update_from_v2 ())
401490 return 0 ;
402491
@@ -433,36 +522,60 @@ int esp_patition_copy_ota1_to_ota0(const void *partition_info)
433522int esp_patition_table_init_data (void * partition_info )
434523{
435524 int ret ;
525+ int reboot = 0 ;
436526 const uint32_t boot_base = 0x1000 ;
437527 const bootloader_state_t * bs = (const bootloader_state_t * )partition_info ;
438528 const uint32_t boot_size = bs -> ota [0 ].offset + bs -> ota [0 ].size - boot_base ;
439529
440530 if (!esp_sdk_update_from_v2 ())
441531 return 0 ;
442532
443- if (esp_get_updated_partition_table_addr () == CONFIG_PARTITION_TABLE_OFFSET )
444- return 0 ;
533+ if (esp_get_updated_partition_table_addr () != CONFIG_PARTITION_TABLE_OFFSET ) {
534+ ESP_LOGD (TAG , "Copy firmware1 from %d total %d" , boot_base + PARTITION_DATA_OFFSET , boot_size );
535+
536+ ESP_LOGI (TAG , "Start unpacking V3 firmware ..." );
537+
538+ ret = esp_flash_sector_copy (boot_base , boot_base + PARTITION_DATA_OFFSET , boot_size );
539+ if (ret ) {
540+ ESP_LOGE (TAG , "Fail to copy V3 firmware from 0x%x to 0x%x total %d" , boot_base + PARTITION_DATA_OFFSET ,
541+ boot_base , boot_size );
542+ return -1 ;
543+ }
445544
446- ESP_LOGD (TAG , "Copy firmware1 from %d total %d" , boot_base + PARTITION_DATA_OFFSET , boot_size );
545+ ret = esp_set_v2boot_app1 ();
546+ if (ret ) {
547+ ESP_LOGE (TAG , "Fail to set V2 app1 default" );
548+ return -1 ;
549+ }
447550
448- ESP_LOGI (TAG , "Start unpacking V3 firmware ... " );
551+ ESP_LOGI (TAG , "Pack V3 firmware successfully and start to reboot " );
449552
450- ret = esp_flash_sector_copy (boot_base , boot_base + PARTITION_DATA_OFFSET , boot_size );
451- if (ret ) {
452- ESP_LOGE (TAG , "Fail to copy V3 firmware from 0x%x to 0x%x total %d" , boot_base + PARTITION_DATA_OFFSET ,
453- boot_base , boot_size );
454- return -1 ;
553+ reboot = 1 ;
455554 }
456555
457- ret = esp_set_v2boot_app1 ();
458- if (ret ) {
459- ESP_LOGE (TAG , "Fail to set V2 app1 default" );
460- return -1 ;
461- }
556+ #ifdef CONFIG_LOAD_OLD_RF_PARAMETER
557+ const uint32_t rf_dest = bs -> rf .offset ;
558+ const uint32_t rf_src = s_v2_flash_bin_size - 4 * 4096 ;
559+ const uint32_t rf_size = 128 ;
560+
561+ if (esp_rf_param_need_unpack (rf_dest , rf_src , rf_size )) {
562+
563+ ESP_LOGI (TAG , "Start unpacking RF parameters from 0x%x to 0x%x total size 0x%x ..." , rf_src , rf_dest , rf_size );
462564
463- ESP_LOGI (TAG , "Pack V3 firmware successfully and start to reboot" );
565+ ret = esp_unpack_old_rf_param (rf_dest , rf_src , rf_size );
566+ if (ret ) {
567+ ESP_LOGE (TAG , "Fail to copy RF parameters from 0x%x to 0x%x total %d" , rf_src , rf_dest , rf_size );
568+ return -1 ;
569+ }
570+
571+ reboot = 1 ;
572+ } else {
573+ ESP_LOGD (TAG , "RF parameters is same" );
574+ }
575+ #endif
464576
465- esp_hw_reset ();
577+ if (reboot )
578+ esp_hw_reset (4 , 1 );
466579
467580 return 0 ;
468581}
0 commit comments