@@ -196,6 +196,12 @@ static int RAMFUNCTION nvm_select_fresh_sector(int part)
196196 uint32_t word_0 ;
197197 uint32_t word_1 ;
198198
199+ #ifdef EXT_FLASH
200+ if ((part == PART_UPDATE ) && FLAGS_UPDATE_EXT ()) {
201+ return 0 ;
202+ }
203+ #endif
204+
199205 hal_cache_invalidate ();
200206
201207 if (part == PART_BOOT ) {
@@ -219,6 +225,7 @@ static int RAMFUNCTION nvm_select_fresh_sector(int part)
219225 word_1 = * ((uint32_t * )((uintptr_t )base - (WOLFBOOT_SECTOR_SIZE + magic_off +
220226 sizeof (uint32_t ))));
221227
228+
222229 if (word_0 == WOLFBOOT_MAGIC_TRAIL && word_1 != WOLFBOOT_MAGIC_TRAIL ) {
223230 sel = 0 ;
224231 goto finish ;
@@ -287,6 +294,7 @@ static int RAMFUNCTION trailer_write(uint8_t part, uintptr_t addr, uint8_t val)
287294 uintptr_t addr_off = addr & (NVM_CACHE_SIZE - 1 );
288295 int ret = 0 ;
289296
297+
290298 nvm_cached_sector = nvm_select_fresh_sector (part );
291299 addr_read = addr_align - (nvm_cached_sector * NVM_CACHE_SIZE );
292300 XMEMCPY (NVM_CACHE , (void * )addr_read , NVM_CACHE_SIZE );
@@ -329,6 +337,7 @@ static int RAMFUNCTION partition_magic_write(uint8_t part, uintptr_t addr)
329337 uintptr_t base = (uintptr_t )addr - off ;
330338 uintptr_t addr_read , addr_write ;
331339 int ret ;
340+
332341 nvm_cached_sector = nvm_select_fresh_sector (part );
333342 addr_read = base - (nvm_cached_sector * NVM_CACHE_SIZE );
334343 addr_write = base - (!nvm_cached_sector * NVM_CACHE_SIZE );
@@ -1403,6 +1412,7 @@ static int RAMFUNCTION hal_set_key(const uint8_t *k, const uint8_t *nonce)
14031412int RAMFUNCTION wolfBoot_set_encrypt_key (const uint8_t * key ,
14041413 const uint8_t * nonce )
14051414{
1415+ set_partition_magic (PART_BOOT );
14061416 hal_set_key (key , nonce );
14071417 return 0 ;
14081418}
@@ -1670,10 +1680,6 @@ int RAMFUNCTION ext_flash_encrypt_write(uintptr_t address, const uint8_t *data,
16701680 if (sz < ENCRYPT_BLOCK_SIZE ) {
16711681 sz = ENCRYPT_BLOCK_SIZE ;
16721682 }
1673- if (!encrypt_initialized ) {
1674- if (crypto_init () < 0 )
1675- return -1 ;
1676- }
16771683 part = part_address (address );
16781684 switch (part ) {
16791685 case PART_UPDATE :
@@ -1684,6 +1690,10 @@ int RAMFUNCTION ext_flash_encrypt_write(uintptr_t address, const uint8_t *data,
16841690 ENCRYPT_BLOCK_SIZE ) {
16851691 return ext_flash_write (address , data , len );
16861692 }
1693+ if (!encrypt_initialized ) {
1694+ if (crypto_init () < 0 )
1695+ return -1 ;
1696+ }
16871697 crypto_set_iv (encrypt_iv_nonce , iv_counter );
16881698 break ;
16891699 case PART_SWAP :
@@ -1752,10 +1762,6 @@ int RAMFUNCTION ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len
17521762 if (row_offset != 0 ) {
17531763 row_address = address & ~(ENCRYPT_BLOCK_SIZE - 1 );
17541764 }
1755- if (!encrypt_initialized ) {
1756- if (crypto_init () < 0 )
1757- return -1 ;
1758- }
17591765 part = part_address (row_address );
17601766 switch (part ) {
17611767 case PART_UPDATE :
@@ -1766,6 +1772,11 @@ int RAMFUNCTION ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len
17661772 ENCRYPT_BLOCK_SIZE ) {
17671773 return ext_flash_read (address , data , len );
17681774 }
1775+ if (!encrypt_initialized ) {
1776+ if (crypto_init () < 0 ) {
1777+ return -1 ;
1778+ }
1779+ }
17691780 crypto_set_iv (encrypt_iv_nonce , iv_counter );
17701781 break ;
17711782 case PART_SWAP :
@@ -1847,18 +1858,15 @@ int wolfBoot_ram_decrypt(uint8_t *src, uint8_t *dst)
18471858 uint32_t dst_offset = 0 , iv_counter = 0 ;
18481859 uint32_t magic , len ;
18491860
1850- wolfBoot_printf ("Decrypting %p to %p\n" , src , dst );
18511861
18521862 if (!encrypt_initialized ) {
18531863 if (crypto_init () < 0 ) {
1854- wolfBoot_printf ("Error initializing crypto!\n" );
18551864 return -1 ;
18561865 }
18571866 }
18581867
18591868 /* Attempt to decrypt firmware header */
18601869 if (decrypt_header (src ) != 0 ) {
1861- wolfBoot_printf ("Error decrypting header at %p!\n" , src );
18621870 return -1 ;
18631871 }
18641872 len = * ((uint32_t * )(dec_hdr + sizeof (uint32_t )));
0 commit comments