Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit a6d011d

Browse files
committed
PYFW-379: Replace spi_flash_write with updater_spi_flash_write function encryption enabled
1 parent 94629bb commit a6d011d

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

esp32/ftp/updater.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ static boot_info_t boot_info;
6666
static uint32_t boot_info_offset;
6767

6868
static uint8_t buf[SPI_FLASH_SEC_SIZE];
69-
#define IMG_UPDATE1_OFFSET_OLD 0x1a0000
69+
#define IMG_UPDATE1_OFFSET_OLD (0x1a0000)
70+
#define OTA_DATA_ADDRESS_OLD (0x190000)
71+
#define BOOTLOADER_ADDRESS (0x1000)
7072

7173
/******************************************************************************
7274
DECLARE PRIVATE FUNCTIONS
@@ -109,6 +111,7 @@ bool updater_check_path (void *path) {
109111
return false;
110112
}
111113

114+
// This function basically simulates an OTA update, the new firmware comes from OTA_0 partition and not from a server
112115
bool update_to_factory_partition(void) {
113116

114117
// Size of the image used in 1.18.2r7 version
@@ -159,11 +162,11 @@ bool updater_start (void) {
159162
// Only update bootloader, otadata and partition table if coming from older version (e.g. 1.18.2) and this is not a downgrade
160163
// In case of upgrade the boot_info located under 0x190000 address
161164
// In case of a downgrade, the boot info located somewhere else than 0x190000 because of the updated partition table
162-
if(boot_info_offset_local == (uint32_t)0x190000){
165+
if(boot_info_offset_local == (uint32_t)OTA_DATA_ADDRESS_OLD){
163166

164167
// Erasing address space of bootloader in 4 KB chunks, it is from 0x1000-0x9000 = 32KB (SPI_FLASH_SEC_SIZE = 4KB)
165168
for(int i = 0; i < 8; i++){
166-
ret = spi_flash_erase_sector((0x1000 + (i*SPI_FLASH_SEC_SIZE)) / SPI_FLASH_SEC_SIZE);
169+
ret = spi_flash_erase_sector((BOOTLOADER_ADDRESS + (i*SPI_FLASH_SEC_SIZE)) / SPI_FLASH_SEC_SIZE);
167170
if (ESP_OK != ret) {
168171
ESP_LOGE(TAG, "Erasing sectors of bootloader failed, error code: %d!\n", ret);
169172
// TODO: try again ???
@@ -172,7 +175,7 @@ bool updater_start (void) {
172175
}
173176

174177
// Update bootloader
175-
ret = spi_flash_write(0x1000, (void *)bootloader_bin, sizeof(bootloader_bin));
178+
ret = updater_spi_flash_write(BOOTLOADER_ADDRESS, (void *)bootloader_bin, sizeof(bootloader_bin), true);
176179
if (ESP_OK != ret) {
177180
ESP_LOGE(TAG, "Updating bootloader failed, error code: %d\n", ret);
178181
//TODO: try again ???
@@ -219,10 +222,10 @@ bool updater_start (void) {
219222

220223
// Writing the new partition table
221224
if (esp32_get_chip_rev() > 0) {
222-
ret = spi_flash_write(ESP_PARTITION_TABLE_ADDR, (void *)partitions_bin_8MB, sizeof(partitions_bin_8MB));
225+
ret = updater_spi_flash_write(ESP_PARTITION_TABLE_ADDR, (void *)partitions_bin_8MB, sizeof(partitions_bin_8MB), true);
223226
}
224227
else {
225-
ret = spi_flash_write(ESP_PARTITION_TABLE_ADDR, (void *)partitions_bin_4MB, sizeof(partitions_bin_4MB));
228+
ret = updater_spi_flash_write(ESP_PARTITION_TABLE_ADDR, (void *)partitions_bin_4MB, sizeof(partitions_bin_4MB), true);
226229
}
227230
if (ESP_OK != ret) {
228231
ESP_LOGE(TAG, "Writing new partition table failed, error code: %d\n", ret);

esp32/mptask.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ extern void modpycom_init0(void);
9191
#define GC_POOL_SIZE_BYTES (67 * 1024)
9292
#define GC_POOL_SIZE_BYTES_PSRAM ((2048 + 512) * 1024)
9393

94+
#define OTA_DATA_ADDRESS_OLD (0x190000)
95+
9496
/******************************************************************************
9597
DECLARE PRIVATE FUNCTIONS
9698
******************************************************************************/
@@ -147,7 +149,7 @@ void TASK_Micropython (void *pvParameters) {
147149
// Only copy if coming from older version (1.18.2) and this is not a downgrade
148150
// In case of upgrade the boot_info located under 0x190000 address
149151
// In case of a downgrade, the boot info located somewhere else than 0x190000 because of the updated partition table
150-
if(boot_info_offset_local == (uint32_t)0x190000){
152+
if(boot_info_offset_local == (uint32_t)OTA_DATA_ADDRESS_OLD){
151153
printf("Copying image from OTA_0 partition to Factory partition, please wait...\n");
152154
if(true == update_to_factory_partition()) {
153155
printf("Image copy finished successfully!\n");

0 commit comments

Comments
 (0)