27
27
#include "esp_spi_flash.h"
28
28
#include "esp_image_format.h"
29
29
#include "esp_secure_boot.h"
30
- #include "esp_flash_encrypt.h"
31
30
#include "sdkconfig.h"
32
31
33
32
#include "esp_ota_ops.h"
34
- #include "rom/queue.h"
35
- #include "rom/crc.h"
36
- #include "soc/dport_reg.h"
33
+ #include "sys/queue.h"
34
+ #include "crc.h"
37
35
#include "esp_log.h"
38
36
37
+ #ifdef CONFIG_TARGET_PLATFORM_ESP8266
38
+ #include "spi_flash.h"
39
+ esp_err_t bootloader_flash_read (size_t src_addr , void * dest , size_t size , bool allow_decrypt );
40
+ #endif
39
41
40
42
#define OTA_MAX (a ,b ) ((a) >= (b) ? (a) : (b))
41
43
#define OTA_MIN (a ,b ) ((a) <= (b) ? (a) : (b))
@@ -150,6 +152,7 @@ esp_err_t esp_ota_write(esp_ota_handle_t handle, const void *data, size_t size)
150
152
return ESP_ERR_OTA_VALIDATE_FAILED ;
151
153
}
152
154
155
+ #ifdef CONFIG_TARGET_PLATFORM_ESP32
153
156
if (esp_flash_encryption_enabled ()) {
154
157
/* Can only write 16 byte blocks to flash, so need to cache anything else */
155
158
size_t copy_len ;
@@ -182,6 +185,7 @@ esp_err_t esp_ota_write(esp_ota_handle_t handle, const void *data, size_t size)
182
185
}
183
186
}
184
187
188
+ #endif
185
189
ret = esp_partition_write (it -> part , it -> wrote_size , data_bytes , size );
186
190
if (ret == ESP_OK ){
187
191
it -> wrote_size += size ;
@@ -191,7 +195,7 @@ esp_err_t esp_ota_write(esp_ota_handle_t handle, const void *data, size_t size)
191
195
}
192
196
193
197
//if go to here ,means don't find the handle
194
- ESP_LOGE (TAG ,"not found the handle" )
198
+ ESP_LOGE (TAG ,"not found the handle" );
195
199
return ESP_ERR_INVALID_ARG ;
196
200
}
197
201
@@ -299,7 +303,6 @@ static esp_err_t esp_rewrite_ota_data(esp_partition_subtype_t subtype)
299
303
uint16_t ota_app_count = 0 ;
300
304
uint32_t i = 0 ;
301
305
uint32_t seq ;
302
- static spi_flash_mmap_memory_t ota_data_map ;
303
306
const void * result = NULL ;
304
307
305
308
find_partition = esp_partition_find_first (ESP_PARTITION_TYPE_DATA , ESP_PARTITION_SUBTYPE_DATA_OTA , NULL );
@@ -323,7 +326,8 @@ static esp_err_t esp_rewrite_ota_data(esp_partition_subtype_t subtype)
323
326
if (SUB_TYPE_ID (subtype ) >= ota_app_count ) {
324
327
return ESP_ERR_INVALID_ARG ;
325
328
}
326
-
329
+ #ifdef CONFIG_TARGET_PLATFORM_ESP32
330
+ static spi_flash_mmap_memory_t ota_data_map ;
327
331
ret = esp_partition_mmap (find_partition , 0 , find_partition -> size , SPI_FLASH_MMAP_DATA , & result , & ota_data_map );
328
332
if (ret != ESP_OK ) {
329
333
result = NULL ;
@@ -333,6 +337,21 @@ static esp_err_t esp_rewrite_ota_data(esp_partition_subtype_t subtype)
333
337
memcpy (& s_ota_select [1 ], result + SPI_FLASH_SEC_SIZE , sizeof (ota_select ));
334
338
spi_flash_munmap (ota_data_map );
335
339
}
340
+ #endif
341
+
342
+ #ifdef CONFIG_TARGET_PLATFORM_ESP8266
343
+ ret = spi_flash_read (find_partition -> address , & s_ota_select [0 ], sizeof (ota_select ));
344
+ if (ret != ESP_OK ) {
345
+ ESP_LOGE (TAG , "read failed" );
346
+ return NULL ;
347
+ }
348
+
349
+ ret = spi_flash_read (find_partition -> address + 0x1000 , & s_ota_select [1 ], sizeof (ota_select ));
350
+ if (ret != ESP_OK ) {
351
+ ESP_LOGE (TAG , "read failed" );
352
+ return NULL ;
353
+ }
354
+ #endif
336
355
337
356
if (ota_select_valid (& s_ota_select [0 ]) && ota_select_valid (& s_ota_select [1 ])) {
338
357
seq = OTA_MAX (s_ota_select [0 ].ota_seq , s_ota_select [1 ].ota_seq );
@@ -445,7 +464,7 @@ const esp_partition_t *esp_ota_get_boot_partition(void)
445
464
{
446
465
esp_err_t ret ;
447
466
const esp_partition_t * find_partition = NULL ;
448
- static spi_flash_mmap_memory_t ota_data_map ;
467
+
449
468
const void * result = NULL ;
450
469
uint16_t ota_app_count = 0 ;
451
470
find_partition = esp_partition_find_first (ESP_PARTITION_TYPE_DATA , ESP_PARTITION_SUBTYPE_DATA_OTA , NULL );
@@ -455,6 +474,8 @@ const esp_partition_t *esp_ota_get_boot_partition(void)
455
474
return NULL ;
456
475
}
457
476
477
+ #ifdef CONFIG_TARGET_PLATFORM_ESP32
478
+ static spi_flash_mmap_memory_t ota_data_map ;
458
479
ret = esp_partition_mmap (find_partition , 0 , find_partition -> size , SPI_FLASH_MMAP_DATA , & result , & ota_data_map );
459
480
if (ret != ESP_OK ) {
460
481
spi_flash_munmap (ota_data_map );
@@ -464,9 +485,23 @@ const esp_partition_t *esp_ota_get_boot_partition(void)
464
485
memcpy (& s_ota_select [0 ], result , sizeof (ota_select ));
465
486
memcpy (& s_ota_select [1 ], result + 0x1000 , sizeof (ota_select ));
466
487
spi_flash_munmap (ota_data_map );
488
+
489
+ #endif
490
+
491
+ #ifdef CONFIG_TARGET_PLATFORM_ESP8266
492
+ ret = spi_flash_read (find_partition -> address , & s_ota_select [0 ], sizeof (ota_select ));
493
+ if (ret != ESP_OK ) {
494
+ ESP_LOGE (TAG , "read failed" );
495
+ return NULL ;
467
496
}
468
- ota_app_count = get_ota_partition_count ();
469
497
498
+ ret = spi_flash_read (find_partition -> address + 0x1000 , & s_ota_select [1 ], sizeof (ota_select ));
499
+ if (ret != ESP_OK ) {
500
+ ESP_LOGE (TAG , "read failed" );
501
+ return NULL ;
502
+ }
503
+ #endif
504
+ ota_app_count = get_ota_partition_count ();
470
505
ESP_LOGD (TAG , "found ota app max = %d" , ota_app_count );
471
506
472
507
if (s_ota_select [0 ].ota_seq == 0xFFFFFFFF && s_ota_select [1 ].ota_seq == 0xFFFFFFFF ) {
@@ -504,10 +539,13 @@ const esp_partition_t* esp_ota_get_running_partition(void)
504
539
/* Find the flash address of this exact function. By definition that is part
505
540
of the currently running firmware. Then find the enclosing partition. */
506
541
542
+ #ifdef CONFIG_TARGET_PLATFORM_ESP32
507
543
size_t phys_offs = spi_flash_cache2phys (esp_ota_get_running_partition );
508
544
509
545
assert (phys_offs != SPI_FLASH_CACHE2PHYS_FAIL ); /* indicates cache2phys lookup is buggy */
546
+ #endif
510
547
548
+ #if CONFIG_TARGET_PLATFORM_ESP32
511
549
esp_partition_iterator_t it = esp_partition_find (ESP_PARTITION_TYPE_APP ,
512
550
ESP_PARTITION_SUBTYPE_ANY ,
513
551
NULL );
@@ -523,6 +561,9 @@ const esp_partition_t* esp_ota_get_running_partition(void)
523
561
}
524
562
525
563
abort (); /* Partition table is invalid or corrupt */
564
+ #else
565
+ return esp_ota_get_boot_partition ();
566
+ #endif
526
567
}
527
568
528
569
0 commit comments