@@ -741,20 +741,16 @@ void RAMFUNCTION wolfBoot_erase_partition(uint8_t part)
741741void RAMFUNCTION wolfBoot_update_trigger (void )
742742{
743743 uint8_t st = IMG_STATE_UPDATING ;
744- #if defined(NVM_FLASH_WRITEONCE ) || defined(WOLFBOOT_FLAGS_INVERT )
745744 uintptr_t lastSector = PART_UPDATE_ENDFLAGS -
746745 (PART_UPDATE_ENDFLAGS % WOLFBOOT_SECTOR_SIZE );
746+ #ifdef NVM_FLASH_WRITEONCE
747+ uint8_t selSec = 0 ;
748+ #endif
747749
748- #ifndef FLAGS_HOME
749750 /* if PART_UPDATE_ENDFLAGS stradles a sector, (all non FLAGS_HOME builds)
750751 * align it to the correct sector */
751752 if (PART_UPDATE_ENDFLAGS % WOLFBOOT_SECTOR_SIZE == 0 )
752753 lastSector -= WOLFBOOT_SECTOR_SIZE ;
753- #endif
754- #endif
755- #ifdef NVM_FLASH_WRITEONCE
756- uint8_t selSec = 0 ;
757- #endif
758754
759755 /* erase the sector flags */
760756 if (FLAGS_UPDATE_EXT ()) {
@@ -767,11 +763,12 @@ void RAMFUNCTION wolfBoot_update_trigger(void)
767763 * partition based on how many flags are non-erased
768764 * FLAGS_INVERT needs erased flags because the bin-assemble's fill byte may
769765 * not match what's in wolfBoot */
770- #if defined(NVM_FLASH_WRITEONCE ) || defined(WOLFBOOT_FLAGS_INVERT )
771766 if (FLAGS_UPDATE_EXT ()) {
772767 ext_flash_erase (lastSector , SECTOR_FLAGS_SIZE );
773768 } else {
774- #ifdef NVM_FLASH_WRITEONCE
769+ #ifndef NVM_FLASH_WRITEONCE
770+ hal_flash_erase (lastSector , SECTOR_FLAGS_SIZE );
771+ #else
775772 selSec = nvm_select_fresh_sector (PART_UPDATE );
776773 XMEMCPY (NVM_CACHE ,
777774 (uint8_t * )(lastSector - WOLFBOOT_SECTOR_SIZE * selSec ),
@@ -783,11 +780,8 @@ void RAMFUNCTION wolfBoot_update_trigger(void)
783780 /* erase the previously selected sector */
784781 hal_flash_erase (lastSector - WOLFBOOT_SECTOR_SIZE * selSec ,
785782 WOLFBOOT_SECTOR_SIZE );
786- #elif defined(WOLFBOOT_FLAGS_INVERT )
787- hal_flash_erase (lastSector , SECTOR_FLAGS_SIZE );
788783#endif
789784 }
790- #endif
791785
792786 wolfBoot_set_partition_state (PART_UPDATE , st );
793787
@@ -812,10 +806,16 @@ void RAMFUNCTION wolfBoot_success(void)
812806 if (FLAGS_BOOT_EXT ()) {
813807 ext_flash_unlock ();
814808 wolfBoot_set_partition_state (PART_BOOT , st );
809+ /* set update so IMG_STATE_FINAL_FLAGS isn't triggering pointless calls
810+ * to wolfBoot update */
811+ wolfBoot_set_partition_state (PART_UPDATE , st );
815812 ext_flash_lock ();
816813 } else {
817814 hal_flash_unlock ();
818815 wolfBoot_set_partition_state (PART_BOOT , st );
816+ /* set update so IMG_STATE_FINAL_FLAGS isn't triggering pointless calls
817+ * to wolfBoot update */
818+ wolfBoot_set_partition_state (PART_UPDATE , st );
819819 hal_flash_lock ();
820820 }
821821#ifdef EXT_ENCRYPTED
@@ -1431,23 +1431,6 @@ int RAMFUNCTION wolfBoot_set_encrypt_key(const uint8_t *key,
14311431 return 0 ;
14321432}
14331433
1434- int RAMFUNCTION wolfBoot_backup_encrypt_key (const uint8_t * key ,
1435- const uint8_t * nonce )
1436- {
1437- #ifndef MMU
1438- uint32_t magic [2 ] = {WOLFBOOT_MAGIC , WOLFBOOT_MAGIC_TRAIL };
1439-
1440- hal_flash_write (WOLFBOOT_PARTITION_BOOT_ADDRESS , key ,
1441- ENCRYPT_KEY_SIZE );
1442- hal_flash_write (WOLFBOOT_PARTITION_BOOT_ADDRESS +
1443- ENCRYPT_KEY_SIZE , nonce , ENCRYPT_NONCE_SIZE );
1444- /* write magic so we know we finished in case of a powerfail */
1445- hal_flash_write (WOLFBOOT_PARTITION_BOOT_ADDRESS +
1446- ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE , (uint8_t * )magic , sizeof (magic ));
1447- #endif
1448- return 0 ;
1449- }
1450-
14511434#ifndef UNIT_TEST
14521435/**
14531436 * @brief Get the encryption key.
@@ -1463,39 +1446,21 @@ int RAMFUNCTION wolfBoot_backup_encrypt_key(const uint8_t* key,
14631446 */
14641447int RAMFUNCTION wolfBoot_get_encrypt_key (uint8_t * k , uint8_t * nonce )
14651448{
1466- int ret = 0 ;
14671449#if defined(MMU )
14681450 XMEMCPY (k , ENCRYPT_KEY , ENCRYPT_KEY_SIZE );
14691451 XMEMCPY (nonce , ENCRYPT_KEY + ENCRYPT_KEY_SIZE , ENCRYPT_NONCE_SIZE );
14701452#else
1471- uint8_t * mem ;
1472- uint32_t magic [2 ];
1473-
1474- /* see if we've backed up the key, this will only matter for final swap */
1475- XMEMCPY (magic , (uint8_t * )WOLFBOOT_PARTITION_BOOT_ADDRESS +
1476- ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE , sizeof (magic ));
1477-
1478- if (magic [0 ] == WOLFBOOT_MAGIC && magic [1 ] == WOLFBOOT_MAGIC_TRAIL ) {
1479- mem = (uint8_t * )WOLFBOOT_PARTITION_BOOT_ADDRESS ;
1480- /* not a failure but finalize needs to know that it's safe to erase and
1481- * write the key to the normal spot */
1482- ret = 1 ;
1483- }
1484- else {
1485- mem = (uint8_t * )(ENCRYPT_TMP_SECRET_OFFSET +
1486- WOLFBOOT_PARTITION_BOOT_ADDRESS );
1487-
1488- #ifdef NVM_FLASH_WRITEONCE
1489- int sel_sec = 0 ;
1490- sel_sec = nvm_select_fresh_sector (PART_BOOT );
1491- mem -= (sel_sec * WOLFBOOT_SECTOR_SIZE );
1492- #endif
1493- }
1494-
1453+ uint8_t * mem = (uint8_t * )(ENCRYPT_TMP_SECRET_OFFSET +
1454+ WOLFBOOT_PARTITION_BOOT_ADDRESS );
1455+ int sel_sec = 0 ;
1456+ #ifdef NVM_FLASH_WRITEONCE
1457+ sel_sec = nvm_select_fresh_sector (PART_BOOT );
1458+ mem -= (sel_sec * WOLFBOOT_SECTOR_SIZE );
1459+ #endif
14951460 XMEMCPY (k , mem , ENCRYPT_KEY_SIZE );
14961461 XMEMCPY (nonce , mem + ENCRYPT_KEY_SIZE , ENCRYPT_NONCE_SIZE );
14971462#endif
1498- return ret ;
1463+ return 0 ;
14991464}
15001465#endif
15011466/**
@@ -1539,12 +1504,16 @@ int RAMFUNCTION chacha_init(void)
15391504#if defined(MMU ) || defined(UNIT_TEST )
15401505 uint8_t * key = ENCRYPT_KEY ;
15411506#else
1542- uint8_t key [ ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE ];
1543- wolfBoot_get_encrypt_key ( key , key + ENCRYPT_KEY_SIZE );
1507+ uint8_t * key = ( uint8_t * )( WOLFBOOT_PARTITION_BOOT_ADDRESS +
1508+ ENCRYPT_TMP_SECRET_OFFSET );
15441509#endif
15451510 uint8_t ff [ENCRYPT_KEY_SIZE ];
15461511 uint8_t * stored_nonce ;
15471512
1513+ #ifdef NVM_FLASH_WRITEONCE
1514+ key -= WOLFBOOT_SECTOR_SIZE * nvm_select_fresh_sector (PART_BOOT );
1515+ #endif
1516+
15481517 stored_nonce = key + ENCRYPT_KEY_SIZE ;
15491518
15501519 XMEMSET (& chacha , 0 , sizeof (chacha ));
@@ -1581,13 +1550,17 @@ int aes_init(void)
15811550#if defined(MMU ) || defined(UNIT_TEST )
15821551 uint8_t * key = ENCRYPT_KEY ;
15831552#else
1584- uint8_t key [ ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE ];
1585- wolfBoot_get_encrypt_key ( key , key + ENCRYPT_KEY_SIZE );
1553+ uint8_t * key = ( uint8_t * )( WOLFBOOT_PARTITION_BOOT_ADDRESS +
1554+ ENCRYPT_TMP_SECRET_OFFSET );
15861555#endif
15871556 uint8_t ff [ENCRYPT_KEY_SIZE ];
15881557 uint8_t iv_buf [ENCRYPT_NONCE_SIZE ];
15891558 uint8_t * stored_nonce ;
15901559
1560+ #ifdef NVM_FLASH_WRITEONCE
1561+ key -= WOLFBOOT_SECTOR_SIZE * nvm_select_fresh_sector (PART_BOOT );
1562+ #endif
1563+
15911564 stored_nonce = key + ENCRYPT_KEY_SIZE ;
15921565
15931566 XMEMSET (& aes_enc , 0 , sizeof (aes_enc ));
@@ -1703,8 +1676,8 @@ static uint8_t RAMFUNCTION part_address(uintptr_t a)
17031676 *
17041677 * @return int 0 if successful, -1 on failure.
17051678 */
1706- int RAMFUNCTION ext_flash_encrypt_write_ex (uintptr_t address ,
1707- const uint8_t * data , int len , int forcedEnc )
1679+ int RAMFUNCTION ext_flash_encrypt_write (uintptr_t address , const uint8_t * data ,
1680+ int len )
17081681{
17091682 uint8_t block [ENCRYPT_BLOCK_SIZE ];
17101683 uint8_t enc_block [ENCRYPT_BLOCK_SIZE ];
@@ -1739,9 +1712,7 @@ int RAMFUNCTION ext_flash_encrypt_write_ex(uintptr_t address,
17391712 break ;
17401713 case PART_SWAP :
17411714 /* data is coming from update and is already encrypted */
1742- if (forcedEnc == 0 )
1743- return ext_flash_write (address , data , len );
1744- break ;
1715+ return ext_flash_write (address , data , len );
17451716 default :
17461717 return -1 ;
17471718 }
@@ -1772,22 +1743,6 @@ int RAMFUNCTION ext_flash_encrypt_write_ex(uintptr_t address,
17721743 return ext_flash_write (address , ENCRYPT_CACHE , step );
17731744}
17741745
1775- /**
1776- * @brief Write encrypted data to an external flash.
1777- *
1778- * This function calls ext_flash_encrypt_write_ex with forced encryption off
1779- *
1780- * @param address The address in the external flash to write the data to.
1781- * @param data Pointer to the data buffer to be written.
1782- * @param len The length of the data to be written.
1783- *
1784- * @return int 0 if successful, -1 on failure.
1785- */
1786- int RAMFUNCTION ext_flash_encrypt_write (uintptr_t address , const uint8_t * data , int len )
1787- {
1788- return ext_flash_encrypt_write_ex (address , data , len , 0 );
1789- }
1790-
17911746/**
17921747 * @brief Read and decrypt data from an external flash.
17931748 *
@@ -1934,39 +1889,3 @@ int wolfBoot_ram_decrypt(uint8_t *src, uint8_t *dst)
19341889}
19351890#endif /* MMU */
19361891#endif /* EXT_ENCRYPTED */
1937-
1938- #ifdef FLAGS_HOME
1939- /* we need to write a marker to update since the boot and update flags are all
1940- * in the same sector so write magic to the first sector of boot */
1941- int wolfBoot_flags_home_set_final_swap ()
1942- {
1943- /* EXT_ENCRYPTED uses the first sector to store the key and magic, don't
1944- * overwrite it */
1945- #ifndef EXT_ENCRYPTED
1946- uint32_t magic [2 ] = {WOLFBOOT_MAGIC , WOLFBOOT_MAGIC_TRAIL };
1947- uintptr_t addr = (uintptr_t )WOLFBOOT_PARTITION_BOOT_ADDRESS ;
1948-
1949- hal_flash_write (addr , (uint8_t * )magic , sizeof (magic ));
1950- #endif /* !EXT_ENCRYPTED */
1951-
1952- return 0 ;
1953- }
1954-
1955- int wolfBoot_flags_home_get_final_swap ()
1956- {
1957- uint32_t magic [2 ];
1958- uintptr_t addr = (uintptr_t )WOLFBOOT_PARTITION_BOOT_ADDRESS ;
1959-
1960- /* if encryption is on magic will be after the key and nonce */
1961- #ifdef EXT_ENCRYPTED
1962- addr += ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE ;
1963- #endif
1964-
1965- XMEMCPY ((uint8_t * )magic , (uint8_t * )addr , sizeof (magic ));
1966-
1967- if (magic [0 ] == WOLFBOOT_MAGIC && magic [1 ] == WOLFBOOT_MAGIC_TRAIL )
1968- return 1 ;
1969-
1970- return 0 ;
1971- }
1972- #endif /* FLAGS_HOME */
0 commit comments