26
26
#include "esp_libc.h"
27
27
#include "esp8266/rom_functions.h"
28
28
#include "esp8266/eagle_soc.h"
29
+ #include "internal/phy_init_data.h"
29
30
30
31
#define PARTITION_DATA_OFFSET (s_v2_flash_bin_size / 2)
31
32
@@ -65,6 +66,7 @@ typedef union s_boot_param {
65
66
static const char * TAG = "partition_port" ;
66
67
static uint32_t s_partition_offset ;
67
68
static ROM_FLASH_BUF_DECLARE (s_cache_buf , SPI_FLASH_SEC_SIZE ) ;
69
+ static ROM_FLASH_BUF_DECLARE (check_buf , SPI_FLASH_SEC_SIZE ) ;
68
70
static uint32_t s_v2_flash_bin_size ;
69
71
static uint32_t s_v2_flash_size ;
70
72
static sys_param_t s_sys_param ;
@@ -109,23 +111,31 @@ static const uint32_t s_v2_flash_bin_map_table[] = {
109
111
110
112
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 ]);
111
113
112
- static inline void esp_hw_reset (void )
114
+ static void esp_hw_reset (uint32_t t , int block )
113
115
{
114
116
CLEAR_WDT_REG_MASK (WDT_CTL_ADDRESS , BIT0 );
115
117
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 );
118
120
119
121
SET_PERI_REG_BITS (PERIPHS_WDT_BASEADDR + WDT_CTL_ADDRESS , WDT_CTL_RSTLEN_MASK , 7 << WDT_CTL_RSTLEN_LSB , 0 );
120
122
// interrupt then reset
121
123
SET_PERI_REG_BITS (PERIPHS_WDT_BASEADDR + WDT_CTL_ADDRESS , WDT_CTL_RSPMOD_MASK , 0 << WDT_CTL_RSPMOD_LSB , 0 );
122
124
// start task watch dog1
123
125
SET_PERI_REG_BITS (PERIPHS_WDT_BASEADDR + WDT_CTL_ADDRESS , WDT_CTL_EN_MASK , 1 << WDT_CTL_EN_LSB , 0 );
124
126
125
- while (1 );
127
+ WDT_FEED ();
128
+
129
+ if (block )
130
+ while (1 );
126
131
}
127
132
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 )
129
139
{
130
140
int ret ;
131
141
@@ -138,10 +148,12 @@ static inline int spi_flash_read_data(uint32_t addr, void *buf, size_t n)
138
148
139
149
ret = SPI_read_data (& s_flash_chip , addr , buf , n );
140
150
151
+ esp_hw_feed_wdt ();
152
+
141
153
return ret ;
142
154
}
143
155
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 )
145
157
{
146
158
int ret ;
147
159
@@ -154,21 +166,25 @@ static inline int spi_flash_write_data(uint32_t addr, const void *buf, uint32_t
154
166
155
167
ret = SPIWrite (addr , (void * )buf , n );
156
168
169
+ esp_hw_feed_wdt ();
170
+
157
171
return ret ;
158
172
}
159
173
160
- static inline int spi_flash_erase (uint32_t addr )
174
+ static int spi_flash_erase (uint32_t addr )
161
175
{
162
176
int ret ;
163
177
164
178
ESP_LOGD (TAG , "erase addr is 0x%x" , addr );
165
179
166
180
ret = SPIEraseSector (addr / SPI_FLASH_SEC_SIZE );
167
181
182
+ esp_hw_feed_wdt ();
183
+
168
184
return ret ;
169
185
}
170
186
171
- static inline int get_v2_flash_map_size (void )
187
+ static int get_v2_flash_map_size (void )
172
188
{
173
189
int ret ;
174
190
esp_image_header_t header ;
@@ -197,7 +213,7 @@ static inline int get_v2_flash_map_size(void)
197
213
return 0 ;
198
214
}
199
215
200
- static inline uint32_t esp_get_updated_partition_table_addr (void )
216
+ static uint32_t esp_get_updated_partition_table_addr (void )
201
217
{
202
218
int ret ;
203
219
size_t offset ;
@@ -259,10 +275,9 @@ static inline uint32_t esp_get_updated_partition_table_addr(void)
259
275
return s_partition_offset ;
260
276
}
261
277
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 )
263
279
{
264
- int ret ;
265
- static ROM_FLASH_BUF_DECLARE (check_buf , SPI_FLASH_SEC_SIZE );
280
+ int ret ;
266
281
267
282
ret = spi_flash_erase (addr );
268
283
if (ret ) {
@@ -313,7 +328,79 @@ static int esp_flash_sector_copy(uint32_t dest, uint32_t src, uint32_t total_siz
313
328
return 0 ;
314
329
}
315
330
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 )
317
404
{
318
405
int ret ;
319
406
const size_t offset = s_sys_param .flag ? 1 : 0 ;
@@ -360,7 +447,7 @@ static inline int esp_set_v2boot_app1(void)
360
447
return 0 ;
361
448
}
362
449
363
- static inline int esp_sdk_update_from_v2 (void )
450
+ static int esp_sdk_update_from_v2 (void )
364
451
{
365
452
const int segment_cnt = 3 ;
366
453
const size_t v2_max_size = 4096 ;
@@ -397,6 +484,8 @@ int esp_patition_table_init_location(void)
397
484
{
398
485
uint32_t addr ;
399
486
487
+ esp_hw_reset (14 , 0 );
488
+
400
489
if (!esp_sdk_update_from_v2 ())
401
490
return 0 ;
402
491
@@ -433,36 +522,60 @@ int esp_patition_copy_ota1_to_ota0(const void *partition_info)
433
522
int esp_patition_table_init_data (void * partition_info )
434
523
{
435
524
int ret ;
525
+ int reboot = 0 ;
436
526
const uint32_t boot_base = 0x1000 ;
437
527
const bootloader_state_t * bs = (const bootloader_state_t * )partition_info ;
438
528
const uint32_t boot_size = bs -> ota [0 ].offset + bs -> ota [0 ].size - boot_base ;
439
529
440
530
if (!esp_sdk_update_from_v2 ())
441
531
return 0 ;
442
532
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
+ }
445
544
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
+ }
447
550
448
- ESP_LOGI (TAG , "Start unpacking V3 firmware ... " );
551
+ ESP_LOGI (TAG , "Pack V3 firmware successfully and start to reboot " );
449
552
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 ;
455
554
}
456
555
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 );
462
564
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
464
576
465
- esp_hw_reset ();
577
+ if (reboot )
578
+ esp_hw_reset (4 , 1 );
466
579
467
580
return 0 ;
468
581
}
0 commit comments