@@ -63,18 +63,42 @@ static void cleanup_flash(void);
6363static int add_payload_type (uint8_t part , uint32_t version , uint32_t size ,
6464 uint16_t img_type );
6565
66+ static uint32_t host_to_img_u32 (uint32_t val )
67+ {
68+ #ifdef BIG_ENDIAN_ORDER
69+ return (((val & 0x000000FFu ) << 24 ) |
70+ ((val & 0x0000FF00u ) << 8 ) |
71+ ((val & 0x00FF0000u ) >> 8 ) |
72+ ((val & 0xFF000000u ) >> 24 ));
73+ #else
74+ return val ;
75+ #endif
76+ }
77+
78+ static uint16_t host_to_img_u16 (uint16_t val )
79+ {
80+ #ifdef BIG_ENDIAN_ORDER
81+ return (uint16_t )(((val & 0x00FFu ) << 8 ) |
82+ ((val & 0xFF00u ) >> 8 ));
83+ #else
84+ return val ;
85+ #endif
86+ }
87+
6688#ifdef DELTA_UPDATES
6789static int mock_wb_patch_init_calls = 0 ;
90+ static uint8_t * mock_wb_patch_init_patch = NULL ;
91+ static uint32_t mock_wb_patch_init_psz = 0 ;
6892
6993int unit_test_wb_patch_init (WB_PATCH_CTX * bm , uint8_t * src , uint32_t ssz ,
7094 uint8_t * patch , uint32_t psz )
7195{
7296 (void )bm ;
7397 (void )src ;
7498 (void )ssz ;
75- (void )patch ;
76- (void )psz ;
7799 mock_wb_patch_init_calls ++ ;
100+ mock_wb_patch_init_patch = patch ;
101+ mock_wb_patch_init_psz = psz ;
78102 return 0 ;
79103}
80104
@@ -192,6 +216,8 @@ static void reset_mock_stats(void)
192216#endif
193217#ifdef DELTA_UPDATES
194218 mock_wb_patch_init_calls = 0 ;
219+ mock_wb_patch_init_patch = NULL ;
220+ mock_wb_patch_init_psz = 0 ;
195221#endif
196222}
197223
@@ -243,6 +269,10 @@ static int add_payload_type(uint8_t part, uint32_t version, uint32_t size,
243269 uint16_t img_type )
244270{
245271 uint32_t word ;
272+ uint32_t magic = WOLFBOOT_MAGIC ;
273+ uint32_t size_img = host_to_img_u32 (size );
274+ uint32_t version_img = host_to_img_u32 (version );
275+ uint16_t img_type_img = host_to_img_u16 (img_type );
246276 int i ;
247277 uint8_t * base = (uint8_t * )(uintptr_t )WOLFBOOT_PARTITION_BOOT_ADDRESS ;
248278 int ret ;
@@ -260,21 +290,21 @@ static int add_payload_type(uint8_t part, uint32_t version, uint32_t size,
260290
261291
262292 hal_flash_unlock ();
263- hal_flash_write ((uintptr_t )base , "WOLF" , 4 );
264- printf ("Written magic: \"WOLF\"\n" );
293+ hal_flash_write ((uintptr_t )base , ( void * ) & magic , 4 );
294+ printf ("Written magic: 0x%08X\n" , magic );
265295
266- hal_flash_write ((uintptr_t )base + 4 , (void * )& size , 4 );
296+ hal_flash_write ((uintptr_t )base + 4 , (void * )& size_img , 4 );
267297 printf ("Written size: %u\n" , size );
268298
269299 /* Headers */
270300 word = 4 << 16 | HDR_VERSION ;
271301 hal_flash_write ((uintptr_t )base + 8 , (void * )& word , 4 );
272- hal_flash_write ((uintptr_t )base + 12 , (void * )& version , 4 );
302+ hal_flash_write ((uintptr_t )base + 12 , (void * )& version_img , 4 );
273303 printf ("Written version: %u\n" , version );
274304
275305 word = 2 << 16 | HDR_IMG_TYPE ;
276306 hal_flash_write ((uintptr_t )base + 16 , (void * )& word , 4 );
277- hal_flash_write ((uintptr_t )base + 20 , (void * )& img_type , 2 );
307+ hal_flash_write ((uintptr_t )base + 20 , (void * )& img_type_img , 2 );
278308 printf ("Written img_type: %04X\n" , img_type );
279309
280310 /* Add 28B header to sha calculation */
@@ -929,7 +959,12 @@ END_TEST
929959
930960START_TEST (test_diffbase_version_reads )
931961{
962+ uint32_t magic = WOLFBOOT_MAGIC ;
932963 uint32_t word ;
964+ uint32_t word_le ;
965+ uint32_t version_le ;
966+ uint32_t delta_base_le ;
967+ uint16_t img_type_le ;
933968 uint32_t version = 0x01020304 ;
934969 uint32_t delta_base = 0x33445566 ;
935970 uint16_t img_type = HDR_IMG_TYPE_AUTH | HDR_IMG_TYPE_APP ;
@@ -939,27 +974,33 @@ START_TEST (test_diffbase_version_reads)
939974
940975 ext_flash_unlock ();
941976 ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS ,
942- (const uint8_t * )"WOLF" , 4 );
977+ (const uint8_t * )& magic , sizeof (magic ));
978+ version_le = host_to_img_u32 (version );
943979 ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 4 ,
944- (const uint8_t * )& version , sizeof (version ));
980+ (const uint8_t * )& version_le , sizeof (version_le ));
945981
946982 word = (4u << 16 ) | HDR_VERSION ;
983+ word_le = word ;
947984 ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 8 ,
948- (const uint8_t * )& word , sizeof (word ));
985+ (const uint8_t * )& word_le , sizeof (word_le ));
949986 ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 12 ,
950- (const uint8_t * )& version , sizeof (version ));
987+ (const uint8_t * )& version_le , sizeof (version_le ));
951988
952989 word = (2u << 16 ) | HDR_IMG_TYPE ;
990+ word_le = word ;
953991 ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 16 ,
954- (const uint8_t * )& word , sizeof (word ));
992+ (const uint8_t * )& word_le , sizeof (word_le ));
993+ img_type_le = host_to_img_u16 (img_type );
955994 ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 20 ,
956- (const uint8_t * )& img_type , sizeof (img_type ));
995+ (const uint8_t * )& img_type_le , sizeof (img_type_le ));
957996
958997 word = (4u << 16 ) | HDR_IMG_DELTA_BASE ;
998+ word_le = word ;
959999 ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 24 ,
960- (const uint8_t * )& word , sizeof (word ));
1000+ (const uint8_t * )& word_le , sizeof (word_le ));
1001+ delta_base_le = host_to_img_u32 (delta_base );
9611002 ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 28 ,
962- (const uint8_t * )& delta_base , sizeof (delta_base ));
1003+ (const uint8_t * )& delta_base_le , sizeof (delta_base_le ));
9631004 ext_flash_lock ();
9641005
9651006 ck_assert_uint_eq (wolfBoot_get_diffbase_version (PART_UPDATE ), delta_base );
@@ -1051,13 +1092,15 @@ START_TEST (test_delta_base_version_mismatch_rejected)
10511092 word = (4u << 16 ) | HDR_IMG_DELTA_SIZE ;
10521093 ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 64 ,
10531094 (const uint8_t * )& word , sizeof (word ));
1095+ word = host_to_img_u32 (delta_sz );
10541096 ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 68 ,
1055- (const uint8_t * )& delta_sz , sizeof (delta_sz ));
1097+ (const uint8_t * )& word , sizeof (word ));
10561098 word = (4u << 16 ) | HDR_IMG_DELTA_BASE ;
10571099 ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 72 ,
10581100 (const uint8_t * )& word , sizeof (word ));
1101+ word = host_to_img_u32 (delta_base );
10591102 ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 76 ,
1060- (const uint8_t * )& delta_base , sizeof (delta_base ));
1103+ (const uint8_t * )& word , sizeof (word ));
10611104 ext_flash_lock ();
10621105
10631106 ck_assert_int_eq (wolfBoot_open_image (& boot , PART_BOOT ), 0 );
@@ -1078,7 +1121,7 @@ START_TEST (test_delta_base_version_match_accepts)
10781121{
10791122 struct wolfBoot_image boot , update , swap ;
10801123 uint32_t word ;
1081- uint32_t delta_sz = 0 ;
1124+ uint32_t delta_sz = 0x00001020 ;
10821125 uint32_t delta_base = 1 ;
10831126 int ret ;
10841127
@@ -1092,13 +1135,15 @@ START_TEST (test_delta_base_version_match_accepts)
10921135 word = (4u << 16 ) | HDR_IMG_DELTA_SIZE ;
10931136 ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 64 ,
10941137 (const uint8_t * )& word , sizeof (word ));
1138+ word = host_to_img_u32 (delta_sz );
10951139 ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 68 ,
1096- (const uint8_t * )& delta_sz , sizeof (delta_sz ));
1140+ (const uint8_t * )& word , sizeof (word ));
10971141 word = (4u << 16 ) | HDR_IMG_DELTA_BASE ;
10981142 ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 72 ,
10991143 (const uint8_t * )& word , sizeof (word ));
1144+ word = host_to_img_u32 (delta_base );
11001145 ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 76 ,
1101- (const uint8_t * )& delta_base , sizeof (delta_base ));
1146+ (const uint8_t * )& word , sizeof (word ));
11021147 ext_flash_lock ();
11031148
11041149 ck_assert_int_eq (wolfBoot_open_image (& boot , PART_BOOT ), 0 );
@@ -1110,6 +1155,59 @@ START_TEST (test_delta_base_version_match_accepts)
11101155 ret = wolfBoot_delta_update (& boot , & update , & swap , 0 , 0 );
11111156 ck_assert_int_eq (ret , 0 );
11121157 ck_assert_int_eq (mock_wb_patch_init_calls , 1 );
1158+ ck_assert_uint_eq (mock_wb_patch_init_psz , delta_sz );
1159+
1160+ cleanup_flash ();
1161+ }
1162+ END_TEST
1163+
1164+ START_TEST (test_delta_inverse_values_passed_with_native_endian )
1165+ {
1166+ struct wolfBoot_image boot , update , swap ;
1167+ uint32_t word ;
1168+ uint32_t delta_inverse_offset = 0x00001020 ;
1169+ uint32_t delta_inverse_size = 0x00002040 ;
1170+ uint32_t delta_base = 1 ;
1171+ int ret ;
1172+
1173+ reset_mock_stats ();
1174+ prepare_flash ();
1175+
1176+ add_payload (PART_BOOT , 1 , TEST_SIZE_SMALL );
1177+ add_payload (PART_UPDATE , 2 , TEST_SIZE_SMALL );
1178+
1179+ ext_flash_unlock ();
1180+ word = (4u << 16 ) | HDR_IMG_DELTA_INVERSE ;
1181+ ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 64 ,
1182+ (const uint8_t * )& word , sizeof (word ));
1183+ word = host_to_img_u32 (delta_inverse_offset );
1184+ ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 68 ,
1185+ (const uint8_t * )& word , sizeof (word ));
1186+ word = (4u << 16 ) | HDR_IMG_DELTA_INVERSE_SIZE ;
1187+ ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 72 ,
1188+ (const uint8_t * )& word , sizeof (word ));
1189+ word = host_to_img_u32 (delta_inverse_size );
1190+ ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 76 ,
1191+ (const uint8_t * )& word , sizeof (word ));
1192+ word = (4u << 16 ) | HDR_IMG_DELTA_BASE ;
1193+ ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 80 ,
1194+ (const uint8_t * )& word , sizeof (word ));
1195+ word = host_to_img_u32 (delta_base );
1196+ ext_flash_write (WOLFBOOT_PARTITION_UPDATE_ADDRESS + 84 ,
1197+ (const uint8_t * )& word , sizeof (word ));
1198+ ext_flash_lock ();
1199+
1200+ ck_assert_int_eq (wolfBoot_open_image (& boot , PART_BOOT ), 0 );
1201+ ck_assert_int_eq (wolfBoot_open_image (& update , PART_UPDATE ), 0 );
1202+ memset (& swap , 0 , sizeof (swap ));
1203+ swap .part = PART_SWAP ;
1204+ swap .hdr = (void * )(uintptr_t )WOLFBOOT_PARTITION_SWAP_ADDRESS ;
1205+
1206+ ret = wolfBoot_delta_update (& boot , & update , & swap , 1 , 1 );
1207+ ck_assert_int_eq (ret , 0 );
1208+ ck_assert_int_eq (mock_wb_patch_init_calls , 1 );
1209+ ck_assert_ptr_eq (mock_wb_patch_init_patch , update .hdr + delta_inverse_offset );
1210+ ck_assert_uint_eq (mock_wb_patch_init_psz , delta_inverse_size );
11131211
11141212 cleanup_flash ();
11151213}
@@ -1225,6 +1323,7 @@ Suite *wolfboot_suite(void)
12251323 tcase_add_test (delta_zero_size , test_delta_zero_size_erased_header_uses_recovery_heuristic );
12261324 tcase_add_test (delta_base_version , test_delta_base_version_mismatch_rejected );
12271325 tcase_add_test (delta_base_version , test_delta_base_version_match_accepts );
1326+ tcase_add_test (delta_base_version , test_delta_inverse_values_passed_with_native_endian );
12281327#endif
12291328#ifdef RAM_CODE
12301329 tcase_add_test (self_update_sameversion , test_self_update_sameversion_erased );
0 commit comments