2727 */
2828#include <stdint.h>
2929
30-
3130#include "hal.h"
3231#include "wolfboot/wolfboot.h"
3332#include "image.h"
@@ -192,11 +191,10 @@ static int RAMFUNCTION nvm_select_fresh_sector(int part)
192191 uintptr_t off ;
193192 uint8_t * base ;
194193 uint8_t * addrErase = 0 ;
195- uint32_t magic_off = 0 ;
196194 uint32_t word_0 ;
197195 uint32_t word_1 ;
198196
199- #ifdef EXT_FLASH
197+ #if defined( EXT_FLASH ) && !defined( FLAGS_HOME )
200198 if ((part == PART_UPDATE ) && FLAGS_UPDATE_EXT ()) {
201199 return 0 ;
202200 }
@@ -221,10 +219,8 @@ static int RAMFUNCTION nvm_select_fresh_sector(int part)
221219 }
222220
223221 /* check magic in case the sector is corrupt */
224- word_0 = * ((uint32_t * )((uintptr_t )base - (magic_off + sizeof (uint32_t ))));
225- word_1 = * ((uint32_t * )((uintptr_t )base - (WOLFBOOT_SECTOR_SIZE + magic_off +
226- sizeof (uint32_t ))));
227-
222+ word_0 = * ((uint32_t * )((uintptr_t )base - sizeof (uint32_t )));
223+ word_1 = * ((uint32_t * )((uintptr_t )base - (WOLFBOOT_SECTOR_SIZE + sizeof (uint32_t ))));
228224
229225 if (word_0 == WOLFBOOT_MAGIC_TRAIL && word_1 != WOLFBOOT_MAGIC_TRAIL ) {
230226 sel = 0 ;
@@ -245,7 +241,7 @@ static int RAMFUNCTION nvm_select_fresh_sector(int part)
245241 /* Select the sector with more flags set. Partition flag is at offset '4'.
246242 * Sector flags begin from offset '5'.
247243 */
248- for (off = 4 + magic_off ; off < WOLFBOOT_SECTOR_SIZE ; off ++ ) {
244+ for (off = 4 ; off < WOLFBOOT_SECTOR_SIZE ; off ++ ) {
249245 volatile uint8_t byte_0 = get_base_offset (base , off );
250246 volatile uint8_t byte_1 = get_base_offset (base , (WOLFBOOT_SECTOR_SIZE + off ));
251247
@@ -294,7 +290,6 @@ static int RAMFUNCTION trailer_write(uint8_t part, uintptr_t addr, uint8_t val)
294290 uintptr_t addr_off = addr & (NVM_CACHE_SIZE - 1 );
295291 int ret = 0 ;
296292
297-
298293 nvm_cached_sector = nvm_select_fresh_sector (part );
299294 addr_read = addr_align - (nvm_cached_sector * NVM_CACHE_SIZE );
300295 XMEMCPY (NVM_CACHE , (void * )addr_read , NVM_CACHE_SIZE );
@@ -728,8 +723,7 @@ void RAMFUNCTION wolfBoot_erase_partition(uint8_t part)
728723void RAMFUNCTION wolfBoot_update_trigger (void )
729724{
730725 uint8_t st = IMG_STATE_UPDATING ;
731- uintptr_t lastSector = PART_UPDATE_ENDFLAGS -
732- (PART_UPDATE_ENDFLAGS % WOLFBOOT_SECTOR_SIZE );
726+ uintptr_t lastSector = ((PART_UPDATE_ENDFLAGS - 1 ) / WOLFBOOT_SECTOR_SIZE ) * WOLFBOOT_SECTOR_SIZE ;
733727#ifdef NVM_FLASH_WRITEONCE
734728 uint8_t selSec = 0 ;
735729#endif
@@ -757,10 +751,8 @@ void RAMFUNCTION wolfBoot_update_trigger(void)
757751 hal_flash_erase (lastSector , SECTOR_FLAGS_SIZE );
758752#else
759753 selSec = nvm_select_fresh_sector (PART_UPDATE );
760- XMEMCPY (NVM_CACHE ,
761- (uint8_t * )(lastSector - WOLFBOOT_SECTOR_SIZE * selSec ),
762- WOLFBOOT_SECTOR_SIZE );
763- XMEMSET (NVM_CACHE , FLASH_BYTE_ERASED , SECTOR_FLAGS_SIZE );
754+ lastSector -= selSec * WOLFBOOT_SECTOR_SIZE ;
755+ XMEMCPY (NVM_CACHE , (uint8_t * )lastSector , WOLFBOOT_SECTOR_SIZE );
764756 /* write to the non selected sector */
765757 hal_flash_write (lastSector - WOLFBOOT_SECTOR_SIZE * !selSec , NVM_CACHE ,
766758 WOLFBOOT_SECTOR_SIZE );
@@ -1353,6 +1345,7 @@ static int RAMFUNCTION hal_set_key(const uint8_t *k, const uint8_t *nonce)
13531345 uintptr_t addr , addr_align , addr_off ;
13541346 int ret = 0 ;
13551347 int sel_sec = 0 ;
1348+ uint32_t trailer_relative_off = 4 ;
13561349#ifdef MMU
13571350 XMEMCPY (ENCRYPT_KEY , k , ENCRYPT_KEY_SIZE );
13581351 XMEMCPY (ENCRYPT_KEY + ENCRYPT_KEY_SIZE , nonce , ENCRYPT_NONCE_SIZE );
@@ -1382,16 +1375,32 @@ static int RAMFUNCTION hal_set_key(const uint8_t *k, const uint8_t *nonce)
13821375 return ret ;
13831376#endif
13841377
1378+ /* Populate key + nonce in the cache */
13851379 XMEMCPY (ENCRYPT_CACHE + addr_off , k , ENCRYPT_KEY_SIZE );
13861380 XMEMCPY (ENCRYPT_CACHE + addr_off + ENCRYPT_KEY_SIZE , nonce ,
13871381 ENCRYPT_NONCE_SIZE );
1388- XMEMCPY (ENCRYPT_CACHE + addr_off - 4 ,
1382+
1383+ /* Add a valid trailer */
1384+ XMEMCPY (ENCRYPT_CACHE + addr_off - trailer_relative_off ,
13891385 & wolfboot_magic_trail , 4 );
1386+ #ifdef FLAGS_HOME
1387+ /* If flags are stored in BOOT partition, take into account the offset
1388+ * of the flags used for the update partition too, to avoid erasing the
1389+ * sector.
1390+ */
1391+ trailer_relative_off += (PART_BOOT_ENDFLAGS - PART_UPDATE_ENDFLAGS );
1392+ XMEMCPY (ENCRYPT_CACHE + addr_off - trailer_relative_off ,
1393+ & wolfboot_magic_trail , 4 );
1394+ #endif
1395+
1396+ /* Writing cache back to sector "!sel_sec" */
13901397 ret = hal_flash_write (addr_align , ENCRYPT_CACHE , WOLFBOOT_SECTOR_SIZE );
13911398#ifdef NVM_FLASH_WRITEONCE
1392- /* now erase the old populated sector */
13931399 if (ret != 0 )
13941400 return ret ;
1401+ /* Erasing original sector "sel_sec",
1402+ * same one returned from by nvm_select.
1403+ */
13951404 addr_align = addr & (~(WOLFBOOT_SECTOR_SIZE - 1 ));
13961405 addr_align -= (sel_sec * WOLFBOOT_SECTOR_SIZE );
13971406 ret = hal_flash_erase (addr_align , WOLFBOOT_SECTOR_SIZE );
0 commit comments