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

Commit 94629bb

Browse files
committed
PYFW-378: Make possible to downgrade from 1.20 to 1.18.3
1 parent ada3f43 commit 94629bb

File tree

2 files changed

+79
-68
lines changed

2 files changed

+79
-68
lines changed

esp32/ftp/updater.c

Lines changed: 67 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -148,81 +148,87 @@ bool updater_start (void) {
148148

149149
esp_err_t ret;
150150

151-
// Save the current OTADATA partition before updating the partition table
151+
// Get the current otadata partition
152152
boot_info_t boot_info_local;
153153
uint32_t boot_info_offset_local;
154154
if(true != updater_read_boot_info(&boot_info_local, &boot_info_offset_local)) {
155155
ESP_LOGE(TAG, "Reading boot info (otadata partition) failed!\n");
156156
return false;
157157
}
158158

159-
// Erasing address space of bootloader in 4 KB chunks, it is from 0x1000-0x9000 = 32KB (SPI_FLASH_SEC_SIZE = 4KB)
160-
for(int i = 0; i < 8; i++){
161-
ret = spi_flash_erase_sector((0x1000 + (i*SPI_FLASH_SEC_SIZE)) / SPI_FLASH_SEC_SIZE);
159+
// Only update bootloader, otadata and partition table if coming from older version (e.g. 1.18.2) and this is not a downgrade
160+
// In case of upgrade the boot_info located under 0x190000 address
161+
// 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){
163+
164+
// Erasing address space of bootloader in 4 KB chunks, it is from 0x1000-0x9000 = 32KB (SPI_FLASH_SEC_SIZE = 4KB)
165+
for(int i = 0; i < 8; i++){
166+
ret = spi_flash_erase_sector((0x1000 + (i*SPI_FLASH_SEC_SIZE)) / SPI_FLASH_SEC_SIZE);
167+
if (ESP_OK != ret) {
168+
ESP_LOGE(TAG, "Erasing sectors of bootloader failed, error code: %d!\n", ret);
169+
// TODO: try again ???
170+
return false;
171+
}
172+
}
173+
174+
// Update bootloader
175+
ret = spi_flash_write(0x1000, (void *)bootloader_bin, sizeof(bootloader_bin));
162176
if (ESP_OK != ret) {
163-
ESP_LOGE(TAG, "Erasing sectors of bootloader failed, error code: %d!\n", ret);
164-
// TODO: try again ???
177+
ESP_LOGE(TAG, "Updating bootloader failed, error code: %d\n", ret);
178+
//TODO: try again ???
165179
return false;
166180
}
167-
}
168181

169-
// Update bootloader
170-
ret = spi_flash_write(0x1000, (void *)bootloader_bin, sizeof(bootloader_bin));
171-
if (ESP_OK != ret) {
172-
ESP_LOGE(TAG, "Updating bootloader failed, error code: %d\n", ret);
173-
//TODO: try again ???
174-
return false;
175-
}
176-
177-
/* Erasing the NEW location of otadata partition, this will ruin/corrupt the current firmware on "ota_0" partition
178-
* The new location of otadata is 0x1BE000 or 0x1FF000 as per updated partition table and has size of
179-
* 4096 bytes which is size of a sector
180-
*/
181-
if (esp32_get_chip_rev() > 0) {
182-
ret = spi_flash_erase_sector(OTA_DATA_ADDRESS_8MB / SPI_FLASH_SEC_SIZE);
183-
}
184-
else {
185-
ret = spi_flash_erase_sector(OTA_DATA_ADDRESS_4MB / SPI_FLASH_SEC_SIZE);
186-
}
187-
if (ESP_OK != ret) {
188-
ESP_LOGE(TAG, "Erasing new sector of boot info failed, error code: %d!\n", ret);
189-
// TODO: try again ???
190-
return false;
191-
}
182+
/* Erasing the NEW location of otadata partition, this will ruin/corrupt the current firmware on "ota_0" partition
183+
* The new location of otadata is 0x1BE000 or 0x1FF000 as per updated partition table and has size of
184+
* 4096 bytes which is size of a sector
185+
*/
186+
if (esp32_get_chip_rev() > 0) {
187+
ret = spi_flash_erase_sector(OTA_DATA_ADDRESS_8MB / SPI_FLASH_SEC_SIZE);
188+
}
189+
else {
190+
ret = spi_flash_erase_sector(OTA_DATA_ADDRESS_4MB / SPI_FLASH_SEC_SIZE);
191+
}
192+
if (ESP_OK != ret) {
193+
ESP_LOGE(TAG, "Erasing new sector of boot info failed, error code: %d!\n", ret);
194+
// TODO: try again ???
195+
return false;
196+
}
192197

193-
// Updating the NEW otadata partition with the OLD information
194-
bool updater_ret = false;
195-
if (esp32_get_chip_rev() > 0) {
196-
updater_ret = updater_write_boot_info(&boot_info_local, OTA_DATA_ADDRESS_8MB);
197-
}
198-
else {
199-
updater_ret = updater_write_boot_info(&boot_info_local, OTA_DATA_ADDRESS_4MB);
200-
}
201-
if (true != updater_ret) {
202-
ESP_LOGE(TAG, "Writing new sector of boot info failed!\n");
203-
//TODO: try again ???
204-
return false;
205-
}
198+
// Updating the NEW otadata partition with the OLD information
199+
bool updater_ret = false;
200+
if (esp32_get_chip_rev() > 0) {
201+
updater_ret = updater_write_boot_info(&boot_info_local, OTA_DATA_ADDRESS_8MB);
202+
}
203+
else {
204+
updater_ret = updater_write_boot_info(&boot_info_local, OTA_DATA_ADDRESS_4MB);
205+
}
206+
if (true != updater_ret) {
207+
ESP_LOGE(TAG, "Writing new sector of boot info failed!\n");
208+
//TODO: try again ???
209+
return false;
210+
}
206211

207-
// Update partition table, it has size of 4096 which is 1 sector
208-
ret = spi_flash_erase_sector(ESP_PARTITION_TABLE_ADDR / SPI_FLASH_SEC_SIZE);
209-
if (ESP_OK != ret) {
210-
ESP_LOGE(TAG, "Erasing partition table partition failed, error code: %d!\n", ret);
211-
//TODO: write back old one ??
212-
return false;
213-
}
212+
// Update partition table, it has size of 4096 which is 1 sector
213+
ret = spi_flash_erase_sector(ESP_PARTITION_TABLE_ADDR / SPI_FLASH_SEC_SIZE);
214+
if (ESP_OK != ret) {
215+
ESP_LOGE(TAG, "Erasing partition table partition failed, error code: %d!\n", ret);
216+
//TODO: write back old one ??
217+
return false;
218+
}
214219

215-
// Writing the new partition table
216-
if (esp32_get_chip_rev() > 0) {
217-
ret = spi_flash_write(ESP_PARTITION_TABLE_ADDR, (void *)partitions_bin_8MB, sizeof(partitions_bin_8MB));
218-
}
219-
else {
220-
ret = spi_flash_write(ESP_PARTITION_TABLE_ADDR, (void *)partitions_bin_4MB, sizeof(partitions_bin_4MB));
221-
}
222-
if (ESP_OK != ret) {
223-
ESP_LOGE(TAG, "Writing new partition table failed, error code: %d\n", ret);
224-
//TODO: try again ???
225-
return false;
220+
// Writing the new partition table
221+
if (esp32_get_chip_rev() > 0) {
222+
ret = spi_flash_write(ESP_PARTITION_TABLE_ADDR, (void *)partitions_bin_8MB, sizeof(partitions_bin_8MB));
223+
}
224+
else {
225+
ret = spi_flash_write(ESP_PARTITION_TABLE_ADDR, (void *)partitions_bin_4MB, sizeof(partitions_bin_4MB));
226+
}
227+
if (ESP_OK != ret) {
228+
ESP_LOGE(TAG, "Writing new partition table failed, error code: %d\n", ret);
229+
//TODO: try again ???
230+
return false;
231+
}
226232
}
227233

228234
updater_data.size = (esp32_get_chip_rev() > 0 ? IMG_SIZE_8MB : IMG_SIZE_4MB);

esp32/mptask.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,19 @@ void TASK_Micropython (void *pvParameters) {
144144
ret = updater_read_boot_info(&boot_info_local, &boot_info_offset_local);
145145
}
146146
if(boot_info_local.ActiveImg != IMG_ACT_FACTORY) {
147-
printf("Copying image from OTA_0 partition to Factory partition, please wait...\n");
148-
if(true == update_to_factory_partition()) {
149-
printf("Image copy finished successfully!\n");
150-
}
147+
// Only copy if coming from older version (1.18.2) and this is not a downgrade
148+
// In case of upgrade the boot_info located under 0x190000 address
149+
// 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){
151+
printf("Copying image from OTA_0 partition to Factory partition, please wait...\n");
152+
if(true == update_to_factory_partition()) {
153+
printf("Image copy finished successfully!\n");
154+
}
151155

152-
//Restart the system
153-
machine_wdt_start(100);
154-
for ( ; ; );
156+
//Restart the system
157+
machine_wdt_start(100);
158+
for ( ; ; );
159+
}
155160
}
156161

157162
// configure the antenna select switch here

0 commit comments

Comments
 (0)