@@ -29,7 +29,7 @@ static const char *TAG = "updater";
2929#define UPDATER_IMG_PATH "/flash/sys/appimg.bin"
3030
3131/* if flash is encrypted, it requires the flash_write operation to be done in 16 Bytes chunks */
32- #define ENCRYP_FLASH_MIN_CHUNK 16
32+ #define ENCRYP_FLASH_MIN_CHUNK 16
3333
3434/******************************************************************************
3535 DEFINE TYPES
@@ -72,14 +72,14 @@ bool updater_read_boot_info (boot_info_t *boot_info, uint32_t *boot_info_offset)
7272 ESP_LOGV (TAG , "Reading boot info\n" );
7373
7474 if (ESP_OK != updater_spi_flash_read (ESP_PARTITION_TABLE_ADDR , (void * )partition_info , sizeof (partition_info ), true)) {
75- ESP_LOGE (TAG , "err1\n" );
76- return false;
75+ ESP_LOGE (TAG , "err1\n" );
76+ return false;
7777 }
7878 // get the data from the boot info partition
7979 ESP_LOGI (TAG , "read data from: 0x%X\n" , partition_info [OTA_DATA_INDEX ].pos .offset );
8080 if (ESP_OK != updater_spi_flash_read (partition_info [OTA_DATA_INDEX ].pos .offset , (void * )boot_info , sizeof (boot_info_t ), true)) {
81- ESP_LOGE (TAG , "err2\n" );
82- return false;
81+ ESP_LOGE (TAG , "err2\n" );
82+ return false;
8383 }
8484 * boot_info_offset = partition_info [OTA_DATA_INDEX ].pos .offset ;
8585 ESP_LOGD (TAG , "off: %d, status:%d, %d\n" , * boot_info_offset , boot_info -> Status , boot_info -> ActiveImg );
@@ -159,49 +159,9 @@ bool updater_finish (void) {
159159 boot_info .ActiveImg = IMG_ACT_UPDATE1 ;
160160 }
161161 boot_info .Status = IMG_STATUS_CHECK ;
162- boot_info .crc = crc32_le (UINT32_MAX , (uint8_t * )& boot_info .ActiveImg ,
163- sizeof (boot_info ) - sizeof (boot_info .crc ));
164- ESP_LOGI (TAG , "Wr crc=0x%x\n" , boot_info .crc );
165-
166- if (ESP_OK != spi_flash_erase_sector (boot_info_offset / SPI_FLASH_SEC_SIZE )) {
167- ESP_LOGE (TAG , "Erasing boot info failed\n" );
168- return false;
169- }
170-
171- // saving boot info, encrypted
172- esp_err_t ret ; // return code of the flash_write operation
173- if (esp_flash_encryption_enabled ()) {
174- // sizeof(boot_info_t) is 40 bytes, and we have to write multiple of 16
175- // so read next 48-40 bytes from flash, and write back 48 B
176-
177- uint32_t len_aligned_16 = ((sizeof (boot_info_t ) + 15 ) / 16 ) * 16 ;
178- uint8_t * buff ; // buffer used for filling boot_info data
179- buff = (uint8_t * )malloc (len_aligned_16 );
180-
181- if (!buff ) {
182- ESP_LOGE (TAG , "Can't allocate %d\n" , len_aligned_16 );
183- return false;
184- }
185-
186- // put the first sizeof(boot_info_t)
187- memcpy (buff , (void * )& boot_info , sizeof (boot_info_t ));
188162
189- // read the next bytes
190- spi_flash_read_encrypted (boot_info_offset + sizeof (boot_info_t ),
191- (void * )(buff + sizeof (boot_info_t )),
192- len_aligned_16 - sizeof (boot_info_t ) );
193-
194- ret = spi_flash_write_encrypted (boot_info_offset , (void * )buff , len_aligned_16 );
195- } else { // not-encrypted flash, just write directly boot_info
196- ret = spi_flash_write (boot_info_offset , (void * )& boot_info , sizeof (boot_info_t ));
197- }
198-
199- if (ESP_OK != ret ) {
200- ESP_LOGE (TAG , "Saving boot info failed\n" );
201- return false;
202- }
203-
204- ESP_LOGI (TAG , "Boot info saved OK\n" );
163+ // save the actual boot_info structure to otadata partition
164+ updater_write_boot_info (& boot_info , boot_info_offset );
205165 }
206166// sl_LockObjUnlock (&wlan_LockObj);
207167 updater_data .offset = 0 ;
@@ -214,10 +174,10 @@ bool updater_verify (void) {
214174 // bootloader verifies anyway the image, but the user can check himself
215175 // so, the next code is adapted from bootloader/bootloader.c,
216176
217- // the last image written stats at updater_data.offset_start_upd and
177+ // the last image written stats at updater_data.offset_start_upd and
218178 // has the lenght boot_info.size
219179
220- esp_err_t ret ;
180+ esp_err_t ret ;
221181 esp_image_metadata_t data ;
222182 const esp_partition_pos_t part_pos = {
223183 .offset = updater_data .offset_start_upd ,
@@ -233,37 +193,68 @@ bool updater_verify (void) {
233193
234194
235195bool updater_write_boot_info (boot_info_t * boot_info , uint32_t boot_info_offset ) {
196+
236197 boot_info -> crc = crc32_le (UINT32_MAX , (uint8_t * )boot_info , sizeof (boot_info_t ) - sizeof (boot_info -> crc ));
198+ ESP_LOGI (TAG , "Wr crc=0x%x\n" , boot_info -> crc );
237199
238200 if (ESP_OK != spi_flash_erase_sector (boot_info_offset / SPI_FLASH_SEC_SIZE )) {
239201 printf ("Erasing boot info failed\n" );
240202 return false;
241203 }
242204
243- if (ESP_OK != spi_flash_write (boot_info_offset , (void * )boot_info , sizeof (boot_info_t ))) {
244- printf ("Saving boot info failed\n" );
245- return false;
205+ // saving boot info, encrypted
206+ esp_err_t ret ; // return code of the flash_write operation
207+ if (esp_flash_encryption_enabled ()) {
208+ // sizeof(boot_info_t) is 40 bytes, and we have to write multiple of 16
209+ // so read next 48-40 bytes from flash, and write back 48 B
210+
211+ uint32_t len_aligned_16 = ((sizeof (boot_info_t ) + 15 ) / 16 ) * 16 ;
212+ uint8_t * buff ; // buffer used for filling boot_info data
213+ buff = (uint8_t * )malloc (len_aligned_16 );
214+
215+ if (!buff ) {
216+ ESP_LOGE (TAG , "Can't allocate %d\n" , len_aligned_16 );
217+ return false;
218+ }
219+
220+ // put the first sizeof(boot_info_t)
221+ memcpy (buff , (void * )boot_info , sizeof (boot_info_t ));
222+
223+ // read the next bytes
224+ spi_flash_read_encrypted (boot_info_offset + sizeof (boot_info_t ),
225+ (void * )(buff + sizeof (boot_info_t )),
226+ len_aligned_16 - sizeof (boot_info_t ) );
227+
228+ ret = spi_flash_write_encrypted (boot_info_offset , (void * )buff , len_aligned_16 );
229+ } else { // not-encrypted flash, just write directly boot_info
230+ ret = spi_flash_write (boot_info_offset , (void * )boot_info , sizeof (boot_info_t ));
246231 }
247- printf ("Boot info saved OK\n" );
248- return true;
232+
233+ if (ESP_OK != ret ) {
234+ ESP_LOGE (TAG , "Saving boot info failed\n" );
235+ } else {
236+ ESP_LOGI (TAG , "Boot info saved OK\n" );
237+ }
238+
239+ return (ESP_OK == ret );
249240}
250241
251242int updater_ota_next_slot_address () {
252243
253- int ota_offset = IMG_UPDATE1_OFFSET ;
244+ int ota_offset = IMG_UPDATE1_OFFSET ;
254245
255- // check which one should be the next active image
246+ // check which one should be the next active image
256247 if (updater_read_boot_info (& boot_info , & boot_info_offset )) {
257248 // if we still have an image pending for verification, keep overwriting it
258249 if ((boot_info .Status == IMG_STATUS_CHECK && boot_info .ActiveImg == IMG_ACT_UPDATE2 ) ||
259250 (boot_info .ActiveImg == IMG_ACT_UPDATE1 && boot_info .Status != IMG_STATUS_CHECK )) {
260- ota_offset = IMG_UPDATE2_OFFSET ;
251+ ota_offset = IMG_UPDATE2_OFFSET ;
261252 }
262253 }
263254
264255 ESP_LOGI (TAG , "Next slot address: 0x%6X\n" , ota_offset );
265256
266- return ota_offset ;
257+ return ota_offset ;
267258}
268259
269260/******************************************************************************
@@ -284,7 +275,7 @@ static esp_err_t updater_spi_flash_read(size_t src, void *dest, size_t size, boo
284275 * be multiples of 32 bytes.
285276*/
286277static esp_err_t updater_spi_flash_write (size_t dest_addr , void * src , size_t size ,
287- bool write_encrypted )
278+ bool write_encrypted )
288279{
289280 if (write_encrypted && esp_flash_encryption_enabled ()) {
290281 return spi_flash_write_encrypted (dest_addr , src , size );
0 commit comments