Skip to content

Commit e736bcb

Browse files
committed
fix: port to esp8266 platform
1 parent 256d238 commit e736bcb

File tree

1 file changed

+50
-9
lines changed

1 file changed

+50
-9
lines changed

components/app_update/esp_ota_ops.c

+50-9
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@
2727
#include "esp_spi_flash.h"
2828
#include "esp_image_format.h"
2929
#include "esp_secure_boot.h"
30-
#include "esp_flash_encrypt.h"
3130
#include "sdkconfig.h"
3231

3332
#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"
3735
#include "esp_log.h"
3836

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
3941

4042
#define OTA_MAX(a,b) ((a) >= (b) ? (a) : (b))
4143
#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)
150152
return ESP_ERR_OTA_VALIDATE_FAILED;
151153
}
152154

155+
#ifdef CONFIG_TARGET_PLATFORM_ESP32
153156
if (esp_flash_encryption_enabled()) {
154157
/* Can only write 16 byte blocks to flash, so need to cache anything else */
155158
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)
182185
}
183186
}
184187

188+
#endif
185189
ret = esp_partition_write(it->part, it->wrote_size, data_bytes, size);
186190
if(ret == ESP_OK){
187191
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)
191195
}
192196

193197
//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");
195199
return ESP_ERR_INVALID_ARG;
196200
}
197201

@@ -299,7 +303,6 @@ static esp_err_t esp_rewrite_ota_data(esp_partition_subtype_t subtype)
299303
uint16_t ota_app_count = 0;
300304
uint32_t i = 0;
301305
uint32_t seq;
302-
static spi_flash_mmap_memory_t ota_data_map;
303306
const void *result = NULL;
304307

305308
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)
323326
if (SUB_TYPE_ID(subtype) >= ota_app_count) {
324327
return ESP_ERR_INVALID_ARG;
325328
}
326-
329+
#ifdef CONFIG_TARGET_PLATFORM_ESP32
330+
static spi_flash_mmap_memory_t ota_data_map;
327331
ret = esp_partition_mmap(find_partition, 0, find_partition->size, SPI_FLASH_MMAP_DATA, &result, &ota_data_map);
328332
if (ret != ESP_OK) {
329333
result = NULL;
@@ -333,6 +337,21 @@ static esp_err_t esp_rewrite_ota_data(esp_partition_subtype_t subtype)
333337
memcpy(&s_ota_select[1], result + SPI_FLASH_SEC_SIZE, sizeof(ota_select));
334338
spi_flash_munmap(ota_data_map);
335339
}
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
336355

337356
if (ota_select_valid(&s_ota_select[0]) && ota_select_valid(&s_ota_select[1])) {
338357
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)
445464
{
446465
esp_err_t ret;
447466
const esp_partition_t *find_partition = NULL;
448-
static spi_flash_mmap_memory_t ota_data_map;
467+
449468
const void *result = NULL;
450469
uint16_t ota_app_count = 0;
451470
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)
455474
return NULL;
456475
}
457476

477+
#ifdef CONFIG_TARGET_PLATFORM_ESP32
478+
static spi_flash_mmap_memory_t ota_data_map;
458479
ret = esp_partition_mmap(find_partition, 0, find_partition->size, SPI_FLASH_MMAP_DATA, &result, &ota_data_map);
459480
if (ret != ESP_OK) {
460481
spi_flash_munmap(ota_data_map);
@@ -464,9 +485,23 @@ const esp_partition_t *esp_ota_get_boot_partition(void)
464485
memcpy(&s_ota_select[0], result, sizeof(ota_select));
465486
memcpy(&s_ota_select[1], result + 0x1000, sizeof(ota_select));
466487
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;
467496
}
468-
ota_app_count = get_ota_partition_count();
469497

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();
470505
ESP_LOGD(TAG, "found ota app max = %d", ota_app_count);
471506

472507
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)
504539
/* Find the flash address of this exact function. By definition that is part
505540
of the currently running firmware. Then find the enclosing partition. */
506541

542+
#ifdef CONFIG_TARGET_PLATFORM_ESP32
507543
size_t phys_offs = spi_flash_cache2phys(esp_ota_get_running_partition);
508544

509545
assert (phys_offs != SPI_FLASH_CACHE2PHYS_FAIL); /* indicates cache2phys lookup is buggy */
546+
#endif
510547

548+
#if CONFIG_TARGET_PLATFORM_ESP32
511549
esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_APP,
512550
ESP_PARTITION_SUBTYPE_ANY,
513551
NULL);
@@ -523,6 +561,9 @@ const esp_partition_t* esp_ota_get_running_partition(void)
523561
}
524562

525563
abort(); /* Partition table is invalid or corrupt */
564+
#else
565+
return esp_ota_get_boot_partition();
566+
#endif
526567
}
527568

528569

0 commit comments

Comments
 (0)