2828#include "hal.h"
2929#include "spi_flash.h"
3030#include "wolfboot/wolfboot.h"
31+ #include "target.h"
3132
3233#include "delta.h"
3334#include "printf.h"
@@ -152,7 +153,11 @@ static int RAMFUNCTION wolfBoot_copy_sector(struct wolfBoot_image *src,
152153 dst_sector_offset = 0 ;
153154
154155#ifdef EXT_ENCRYPTED
156+ if (wolfBoot_initialize_encryption () < 0 ) {
157+ return -1 ;
158+ }
155159 wolfBoot_get_encrypt_key (key , nonce );
160+
156161 if (src -> part == PART_SWAP )
157162 iv_counter = dst_sector_offset ;
158163 else
@@ -202,6 +207,54 @@ static int RAMFUNCTION wolfBoot_copy_sector(struct wolfBoot_image *src,
202207 return pos ;
203208}
204209
210+ #ifdef EXT_ENCRYPTED
211+ static int RAMFUNCTION wolfBoot_backup_last_boot_sector (uint32_t sector )
212+ {
213+ uint32_t pos = 0 ;
214+ uint32_t src_sector_offset = (sector * WOLFBOOT_SECTOR_SIZE );
215+ uint32_t dst_sector_offset = 0 ;
216+ uint8_t key [ENCRYPT_KEY_SIZE ];
217+ uint8_t nonce [ENCRYPT_NONCE_SIZE ];
218+ uint32_t iv_counter ;
219+ uint8_t block [ENCRYPT_BLOCK_SIZE ], encrypted_block [ENCRYPT_BLOCK_SIZE ];
220+ struct wolfBoot_image src [1 ], dst [1 ];
221+
222+ wolfBoot_open_image (src , PART_BOOT );
223+ wolfBoot_open_image (dst , PART_SWAP );
224+
225+
226+ wolfBoot_printf ("Copy sector %d (part %d->%d)\n" ,
227+ sector , src -> part , dst -> part );
228+
229+ wolfBoot_get_encrypt_key (key , nonce );
230+ wolfBoot_printf ("In function wolfBoot_backup_last_boot_sector (sector # %u)\n" ,
231+ sector );
232+
233+ iv_counter = src_sector_offset ;
234+ iv_counter /= ENCRYPT_BLOCK_SIZE ;
235+ if (wolfBoot_initialize_encryption () < 0 )
236+ return -1 ;
237+ crypto_set_iv (nonce , iv_counter );
238+
239+ /* Erase swap space */
240+ wb_flash_erase (dst , dst_sector_offset , WOLFBOOT_SECTOR_SIZE );
241+ if (PART_IS_EXT (dst )) {
242+ uint8_t * orig = (uint8_t * )(WOLFBOOT_PARTITION_BOOT_ADDRESS ) +
243+ src_sector_offset ;
244+ while (pos < WOLFBOOT_SECTOR_SIZE ) {
245+ XMEMCPY (block , orig + pos , ENCRYPT_BLOCK_SIZE );
246+ crypto_encrypt (encrypted_block , block , ENCRYPT_BLOCK_SIZE );
247+ wb_flash_write (dst , dst_sector_offset + pos , encrypted_block , ENCRYPT_BLOCK_SIZE );
248+ pos += ENCRYPT_BLOCK_SIZE ;
249+ }
250+ return 0 ;
251+ } else
252+ return wolfBoot_copy_sector (src , dst , sector );
253+ }
254+ #else
255+ #define wolfBoot_backup_last_boot_sector (sec ) wolfBoot_copy_sector(boot, swap, sec)
256+ #endif
257+
205258#if !defined(DISABLE_BACKUP ) && !defined(CUSTOM_PARTITION_TRAILER )
206259
207260#ifdef EXT_ENCRYPTED
@@ -293,11 +346,13 @@ static int RAMFUNCTION wolfBoot_swap_and_final_erase(int resume)
293346 if (updateState != IMG_STATE_FINAL_FLAGS ) {
294347 /* First, backup the staging sector (sector at tmpBootPos) into swap partition */
295348 /* This sector will be modified with the magic trailer, so we need to preserve it */
296- wolfBoot_copy_sector (boot , swap , tmpBootPos / WOLFBOOT_SECTOR_SIZE );
349+ wolfBoot_backup_last_boot_sector (tmpBootPos / WOLFBOOT_SECTOR_SIZE );
350+ wolfBoot_printf ("Copied boot sector to swap\n" );
297351 /* Mark update as being in final swap phase to allow resumption if power fails */
298352 wolfBoot_set_partition_state (PART_UPDATE , IMG_STATE_FINAL_FLAGS );
299353 }
300354#ifdef EXT_ENCRYPTED
355+ wolfBoot_printf ("In function wolfBoot_final_swap: swapDone = %d\n" , swapDone );
301356 if (swapDone == 0 ) {
302357 /* For encrypted images: Get the encryption key and IV */
303358 wolfBoot_get_encrypt_key ((uint8_t * )tmpBuffer ,
@@ -321,6 +376,7 @@ static int RAMFUNCTION wolfBoot_swap_and_final_erase(int resume)
321376#endif
322377 /* Restore the original contents of the staging sector (with the magic trailer if encrypted) */
323378 if (tmpBootPos < boot -> fw_size + IMAGE_HEADER_SIZE ) {
379+ wolfBoot_printf ("Restoring last boot sector from swap\n" );
324380 wolfBoot_copy_sector (swap , boot , tmpBootPos / WOLFBOOT_SECTOR_SIZE );
325381 }
326382 else {
@@ -461,6 +517,10 @@ static int wolfBoot_delta_update(struct wolfBoot_image *boot,
461517#ifdef EXT_ENCRYPTED
462518 uint32_t iv_counter = sector * WOLFBOOT_SECTOR_SIZE + len ;
463519 int wr_ret ;
520+ if (wolfBoot_initialize_encryption () < 0 ) {
521+ ret = -1 ;
522+ goto out ;
523+ }
464524 iv_counter /= ENCRYPT_BLOCK_SIZE ;
465525 /* Encrypt + send */
466526 crypto_set_iv (nonce , iv_counter );
0 commit comments