@@ -616,7 +616,7 @@ boot_check_header_erased(struct boot_loader_state *state, int slot)
616616#if (BOOT_IMAGE_NUMBER > 1 ) || \
617617 defined(MCUBOOT_DIRECT_XIP ) || \
618618 defined(MCUBOOT_RAM_LOAD ) || \
619- ( defined(MCUBOOT_OVERWRITE_ONLY ) && defined( MCUBOOT_DOWNGRADE_PREVENTION ) )
619+ defined(MCUBOOT_DOWNGRADE_PREVENTION )
620620/**
621621 * Compare image version numbers not including the build number
622622 *
@@ -1332,6 +1332,8 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
13321332 boot_status_fails );
13331333 }
13341334#endif
1335+ rc = BOOT_HOOK_CALL (boot_copy_region_post_hook , 0 , BOOT_CURR_IMG (state ),
1336+ BOOT_IMG_AREA (state , BOOT_PRIMARY_SLOT ), size );
13351337
13361338 return 0 ;
13371339}
@@ -1903,6 +1905,60 @@ boot_update_hw_rollback_protection(struct boot_loader_state *state)
19031905#endif
19041906}
19051907
1908+ /**
1909+ * Checks test swap downgrade prevention conditions.
1910+ *
1911+ * Function called only for swap upgrades test run. It may prevent
1912+ * swap if slot 1 image has <= version number or < security counter
1913+ *
1914+ * @param state Boot loader status information.
1915+ *
1916+ * @return 0 - image can be swapped, -1 downgrade prevention
1917+ */
1918+ static int
1919+ check_downgrade_prevention (struct boot_loader_state * state )
1920+ {
1921+ #if defined(MCUBOOT_DOWNGRADE_PREVENTION ) && \
1922+ (defined(MCUBOOT_SWAP_USING_MOVE ) || defined(MCUBOOT_SWAP_USING_SCRATCH ))
1923+ uint32_t security_counter [2 ];
1924+ int rc ;
1925+
1926+ if (MCUBOOT_DOWNGRADE_PREVENTION_SECURITY_COUNTER ) {
1927+ /* If there was security no counter in slot 0, allow swap */
1928+ rc = bootutil_get_img_security_cnt (& (BOOT_IMG (state , 0 ).hdr ),
1929+ BOOT_IMG (state , 0 ).area ,
1930+ & security_counter [0 ]);
1931+ if (rc != 0 ) {
1932+ return 0 ;
1933+ }
1934+ /* If there is no security counter in slot 1, or it's lower than
1935+ * that of slot 0, prevent downgrade */
1936+ rc = bootutil_get_img_security_cnt (& (BOOT_IMG (state , 1 ).hdr ),
1937+ BOOT_IMG (state , 1 ).area ,
1938+ & security_counter [1 ]);
1939+ if (rc != 0 || security_counter [0 ] > security_counter [1 ]) {
1940+ rc = -1 ;
1941+ }
1942+ }
1943+ else {
1944+ rc = boot_version_cmp (& boot_img_hdr (state , BOOT_SECONDARY_SLOT )-> ih_ver ,
1945+ & boot_img_hdr (state , BOOT_PRIMARY_SLOT )-> ih_ver );
1946+ }
1947+ if (rc < 0 ) {
1948+ /* Image in slot 0 prevents downgrade, delete image in slot 1 */
1949+ BOOT_LOG_INF ("Image in slot 1 erased due to downgrade prevention" );
1950+ flash_area_erase (BOOT_IMG (state , 1 ).area , 0 ,
1951+ flash_area_get_size (BOOT_IMG (state , 1 ).area ));
1952+ } else {
1953+ rc = 0 ;
1954+ }
1955+ return rc ;
1956+ #else
1957+ (void )state ;
1958+ return 0 ;
1959+ #endif
1960+ }
1961+
19061962fih_int
19071963context_boot_go (struct boot_loader_state * state , struct boot_rsp * rsp )
19081964{
@@ -2031,7 +2087,13 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
20312087 case BOOT_SWAP_TYPE_NONE :
20322088 break ;
20332089
2034- case BOOT_SWAP_TYPE_TEST : /* fallthrough */
2090+ case BOOT_SWAP_TYPE_TEST :
2091+ if (check_downgrade_prevention (state ) != 0 ) {
2092+ /* Downgrade prevented */
2093+ BOOT_SWAP_TYPE (state ) = BOOT_SWAP_TYPE_NONE ;
2094+ break ;
2095+ }
2096+ /* fallthrough */
20352097 case BOOT_SWAP_TYPE_PERM : /* fallthrough */
20362098 case BOOT_SWAP_TYPE_REVERT :
20372099 rc = BOOT_HOOK_CALL (boot_perform_update_hook , BOOT_HOOK_REGULAR ,
0 commit comments