@@ -1151,6 +1151,47 @@ START_TEST (test_diffbase_version_reads_from_little_endian_bytes)
11511151END_TEST
11521152
11531153#ifdef DELTA_UPDATES
1154+ static void prepare_inverse_delta_update (struct wolfBoot_image * boot ,
1155+ struct wolfBoot_image * update , struct wolfBoot_image * swap ,
1156+ uint32_t boot_version , uint32_t update_version , uint32_t delta_base ,
1157+ uint32_t delta_inverse_offset , uint32_t delta_inverse_size )
1158+ {
1159+ uint32_t word ;
1160+
1161+ reset_mock_stats ();
1162+ prepare_flash ();
1163+
1164+ add_payload (PART_BOOT , boot_version , TEST_SIZE_SMALL );
1165+ add_payload (PART_UPDATE , update_version , TEST_SIZE_SMALL );
1166+
1167+ ext_flash_unlock ();
1168+ word = (4u << 16 ) | HDR_IMG_DELTA_INVERSE ;
1169+ ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 64 ,
1170+ (const uint8_t * )& word , sizeof (word ));
1171+ word = host_to_img_u32 (delta_inverse_offset );
1172+ ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 68 ,
1173+ (const uint8_t * )& word , sizeof (word ));
1174+ word = (4u << 16 ) | HDR_IMG_DELTA_INVERSE_SIZE ;
1175+ ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 72 ,
1176+ (const uint8_t * )& word , sizeof (word ));
1177+ word = host_to_img_u32 (delta_inverse_size );
1178+ ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 76 ,
1179+ (const uint8_t * )& word , sizeof (word ));
1180+ word = (4u << 16 ) | HDR_IMG_DELTA_BASE ;
1181+ ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 80 ,
1182+ (const uint8_t * )& word , sizeof (word ));
1183+ word = host_to_img_u32 (delta_base );
1184+ ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 84 ,
1185+ (const uint8_t * )& word , sizeof (word ));
1186+ ext_flash_lock ();
1187+
1188+ ck_assert_int_eq (wolfBoot_open_image (boot , PART_BOOT ), 0 );
1189+ ck_assert_int_eq (wolfBoot_open_image (update , PART_UPDATE ), 0 );
1190+ memset (swap , 0 , sizeof (* swap ));
1191+ swap -> part = PART_SWAP ;
1192+ swap -> hdr = (void * )(uintptr_t )WOLFBOOT_PARTITION_SWAP_ADDRESS ;
1193+ }
1194+
11541195START_TEST (test_delta_zero_size_valid_header_rejected_without_recovery_heuristic )
11551196{
11561197 struct wolfBoot_image boot , update , swap ;
@@ -1284,46 +1325,54 @@ END_TEST
12841325START_TEST (test_delta_inverse_values_passed_with_native_endian )
12851326{
12861327 struct wolfBoot_image boot , update , swap ;
1287- uint32_t word ;
12881328 uint32_t delta_inverse_offset = 0x00001020 ;
12891329 uint32_t delta_inverse_size = 0x00002040 ;
1290- uint32_t delta_base = 1 ;
12911330 int ret ;
12921331
1293- reset_mock_stats ();
1294- prepare_flash ( );
1332+ prepare_inverse_delta_update ( & boot , & update , & swap , 1 , 2 , 1 ,
1333+ delta_inverse_offset , delta_inverse_size );
12951334
1296- add_payload (PART_BOOT , 1 , TEST_SIZE_SMALL );
1297- add_payload (PART_UPDATE , 2 , TEST_SIZE_SMALL );
1335+ ret = wolfBoot_delta_update (& boot , & update , & swap , 1 , 1 );
1336+ ck_assert_int_eq (ret , 0 );
1337+ ck_assert_int_eq (mock_wb_patch_init_calls , 1 );
1338+ ck_assert_ptr_eq (mock_wb_patch_init_patch , update .hdr + delta_inverse_offset );
1339+ ck_assert_uint_eq (mock_wb_patch_init_psz , delta_inverse_size );
12981340
1299- ext_flash_unlock ();
1300- word = (4u << 16 ) | HDR_IMG_DELTA_INVERSE ;
1301- ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 64 ,
1302- (const uint8_t * )& word , sizeof (word ));
1303- word = host_to_img_u32 (delta_inverse_offset );
1304- ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 68 ,
1305- (const uint8_t * )& word , sizeof (word ));
1306- word = (4u << 16 ) | HDR_IMG_DELTA_INVERSE_SIZE ;
1307- ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 72 ,
1308- (const uint8_t * )& word , sizeof (word ));
1309- word = host_to_img_u32 (delta_inverse_size );
1310- ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 76 ,
1311- (const uint8_t * )& word , sizeof (word ));
1312- word = (4u << 16 ) | HDR_IMG_DELTA_BASE ;
1313- ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 80 ,
1314- (const uint8_t * )& word , sizeof (word ));
1315- word = host_to_img_u32 (delta_base );
1316- ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 84 ,
1317- (const uint8_t * )& word , sizeof (word ));
1318- ext_flash_lock ();
1341+ cleanup_flash ();
1342+ }
1343+ END_TEST
13191344
1320- ck_assert_int_eq (wolfBoot_open_image (& boot , PART_BOOT ), 0 );
1321- ck_assert_int_eq (wolfBoot_open_image (& update , PART_UPDATE ), 0 );
1322- memset (& swap , 0 , sizeof (swap ));
1323- swap .part = PART_SWAP ;
1324- swap .hdr = (void * )(uintptr_t )WOLFBOOT_PARTITION_SWAP_ADDRESS ;
1345+ START_TEST (test_delta_inverse_accepts_when_current_matches_update )
1346+ {
1347+ struct wolfBoot_image boot , update , swap ;
1348+ uint32_t delta_inverse_offset = 0x00001020 ;
1349+ uint32_t delta_inverse_size = 0x00002040 ;
1350+ int ret ;
13251351
1326- ret = wolfBoot_delta_update (& boot , & update , & swap , 1 , 1 );
1352+ prepare_inverse_delta_update (& boot , & update , & swap , 2 , 2 , 1 ,
1353+ delta_inverse_offset , delta_inverse_size );
1354+
1355+ ret = wolfBoot_delta_update (& boot , & update , & swap , 1 , 0 );
1356+ ck_assert_int_eq (ret , 0 );
1357+ ck_assert_int_eq (mock_wb_patch_init_calls , 1 );
1358+ ck_assert_ptr_eq (mock_wb_patch_init_patch , update .hdr + delta_inverse_offset );
1359+ ck_assert_uint_eq (mock_wb_patch_init_psz , delta_inverse_size );
1360+
1361+ cleanup_flash ();
1362+ }
1363+ END_TEST
1364+
1365+ START_TEST (test_delta_inverse_accepts_when_current_matches_delta_base )
1366+ {
1367+ struct wolfBoot_image boot , update , swap ;
1368+ uint32_t delta_inverse_offset = 0x00001020 ;
1369+ uint32_t delta_inverse_size = 0x00002040 ;
1370+ int ret ;
1371+
1372+ prepare_inverse_delta_update (& boot , & update , & swap , 1 , 2 , 1 ,
1373+ delta_inverse_offset , delta_inverse_size );
1374+
1375+ ret = wolfBoot_delta_update (& boot , & update , & swap , 1 , 0 );
13271376 ck_assert_int_eq (ret , 0 );
13281377 ck_assert_int_eq (mock_wb_patch_init_calls , 1 );
13291378 ck_assert_ptr_eq (mock_wb_patch_init_patch , update .hdr + delta_inverse_offset );
@@ -1332,6 +1381,38 @@ START_TEST (test_delta_inverse_values_passed_with_native_endian)
13321381 cleanup_flash ();
13331382}
13341383END_TEST
1384+
1385+ START_TEST (test_delta_inverse_rejects_when_delta_base_is_newer_than_current )
1386+ {
1387+ struct wolfBoot_image boot , update , swap ;
1388+ int ret ;
1389+
1390+ prepare_inverse_delta_update (& boot , & update , & swap , 2 , 2 , 3 ,
1391+ 0x00001020 , 0x00002040 );
1392+
1393+ ret = wolfBoot_delta_update (& boot , & update , & swap , 1 , 0 );
1394+ ck_assert_int_eq (ret , -1 );
1395+ ck_assert_int_eq (mock_wb_patch_init_calls , 0 );
1396+
1397+ cleanup_flash ();
1398+ }
1399+ END_TEST
1400+
1401+ START_TEST (test_delta_inverse_rejects_when_versions_do_not_match_any_arm )
1402+ {
1403+ struct wolfBoot_image boot , update , swap ;
1404+ int ret ;
1405+
1406+ prepare_inverse_delta_update (& boot , & update , & swap , 2 , 1 , 3 ,
1407+ 0x00001020 , 0x00002040 );
1408+
1409+ ret = wolfBoot_delta_update (& boot , & update , & swap , 1 , 0 );
1410+ ck_assert_int_eq (ret , -1 );
1411+ ck_assert_int_eq (mock_wb_patch_init_calls , 0 );
1412+
1413+ cleanup_flash ();
1414+ }
1415+ END_TEST
13351416#endif
13361417#endif
13371418
@@ -1450,6 +1531,10 @@ Suite *wolfboot_suite(void)
14501531 tcase_add_test (delta_base_version , test_delta_base_version_mismatch_rejected );
14511532 tcase_add_test (delta_base_version , test_delta_base_version_match_accepts );
14521533 tcase_add_test (delta_base_version , test_delta_inverse_values_passed_with_native_endian );
1534+ tcase_add_test (delta_base_version , test_delta_inverse_accepts_when_current_matches_update );
1535+ tcase_add_test (delta_base_version , test_delta_inverse_accepts_when_current_matches_delta_base );
1536+ tcase_add_test (delta_base_version , test_delta_inverse_rejects_when_delta_base_is_newer_than_current );
1537+ tcase_add_test (delta_base_version , test_delta_inverse_rejects_when_versions_do_not_match_any_arm );
14531538#endif
14541539#ifdef RAM_CODE
14551540 tcase_add_test (self_update_sameversion , test_self_update_sameversion_erased );
0 commit comments