@@ -148,81 +148,87 @@ bool updater_start (void) {
148
148
149
149
esp_err_t ret ;
150
150
151
- // Save the current OTADATA partition before updating the partition table
151
+ // Get the current otadata partition
152
152
boot_info_t boot_info_local ;
153
153
uint32_t boot_info_offset_local ;
154
154
if (true != updater_read_boot_info (& boot_info_local , & boot_info_offset_local )) {
155
155
ESP_LOGE (TAG , "Reading boot info (otadata partition) failed!\n" );
156
156
return false;
157
157
}
158
158
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 ));
162
176
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 ???
165
179
return false;
166
180
}
167
- }
168
181
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
+ }
192
197
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
+ }
206
211
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
+ }
214
219
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
+ }
226
232
}
227
233
228
234
updater_data .size = (esp32_get_chip_rev () > 0 ? IMG_SIZE_8MB : IMG_SIZE_4MB );
0 commit comments