3333#ifndef TEST_ADDRESS
3434 #define TEST_SZ WOLFBOOT_SECTOR_SIZE
3535 #define TEST_ADDRESS WOLFBOOT_PARTITION_UPDATE_ADDRESS
36+ #define TEST_ADDRESS_BANKA WOLFBOOT_PARTITION_BOOT_ADDRESS
37+ #define TEST_ADDRESS_BANKB WOLFBOOT_PARTITION_UPDATE_ADDRESS
3638#endif
3739
3840int hal_flash_test (void )
@@ -60,8 +62,12 @@ int hal_flash_test(void)
6062 }
6163
6264 /* Write Page */
65+ hal_flash_unlock ();
6366 ret = hal_flash_write (TEST_ADDRESS , (uint8_t * )pageData , sizeof (pageData ));
67+ hal_flash_lock ();
6468 wolfBoot_printf ("Write Page: Ret %d\n" , ret );
69+ if (ret != 0 )
70+ return ret ;
6571#endif /* !TEST_FLASH_READONLY */
6672
6773 /* Compare Page */
@@ -74,4 +80,166 @@ int hal_flash_test(void)
7480 wolfBoot_printf ("Internal Flash Test Passed\n" );
7581 return ret ;
7682}
83+
84+ #ifndef TEST_FLASH_READONLY
85+ int hal_flash_test_write_once (void )
86+ {
87+ uint8_t test_byte , expected_byte ;
88+ unsigned int i , b ;
89+ int ret = 0 ;
90+
91+ /* Erase the test sector */
92+ hal_flash_unlock ();
93+ ret = hal_flash_erase (TEST_ADDRESS , TEST_SZ );
94+ hal_flash_lock ();
95+ if (ret != 0 ) {
96+ wolfBoot_printf ("Erase Sector failed: Ret %d\n" , ret );
97+ return -1 ;
98+ }
99+
100+ expected_byte = 0xFF ;
101+ /* For each bit in the byte (from LSB to MSB) */
102+ for (b = 0 ; b < 8 ; b ++ ) {
103+ /* Toggle bit from 1 to 0 */
104+ test_byte = 0xFF & ~(1 << b );
105+ expected_byte &= ~(1 << b );
106+
107+ /* Write the data */
108+ hal_flash_unlock ();
109+ ret = hal_flash_write (TEST_ADDRESS , & test_byte , sizeof (test_byte ));
110+ hal_flash_lock ();
111+
112+ if (ret != 0 ) {
113+ wolfBoot_printf ("Write failed at byte %d, bit %d: Ret %d\n" , i , b , ret );
114+ return -1 ;
115+ }
116+
117+ /* Verify the write by direct comparison */
118+ if (memcmp ((void * )TEST_ADDRESS , & expected_byte , sizeof (expected_byte )) != 0 ) {
119+ wolfBoot_printf ("Verification failed at byte %d, bit %d\n" , i , b );
120+ return -1 ;
121+ }
122+ }
123+
124+ wolfBoot_printf ("Write-once test passed\n" );
125+ return 0 ;
126+ }
127+
128+ /* Test if unaligned write works. First test writing 1 bytes at SECTOR + 1.
129+ * Then test writing 2 bytes that span the sector boundary.
130+ */
131+ int hal_flash_test_align (void )
132+ {
133+ int ret = 0 ;
134+ uint8_t test_data_1 = 0xAA ;
135+ uint8_t test_data_2 [2 ] = {0xBB , 0xCC };
136+ uint8_t read_back [2 ];
137+
138+ /* erase both sectors */
139+ hal_flash_unlock ();
140+ ret = hal_flash_erase (TEST_ADDRESS , TEST_SZ * 2 );
141+ hal_flash_lock ();
142+ if (ret != 0 ) {
143+ wolfBoot_printf ("Erase Sector failed: Ret %d\n" , ret );
144+ return -1 ;
145+ }
146+
147+ /* Write 1 byte at SECTOR + 1 */
148+ hal_flash_unlock ();
149+ ret = hal_flash_write (TEST_ADDRESS + 1 , & test_data_1 , 1 );
150+ hal_flash_lock ();
151+ if (ret != 0 ) {
152+ wolfBoot_printf ("Unaligned write (1 byte) failed: Ret %d\n" , ret );
153+ return -1 ;
154+ }
155+
156+ /* Verify 1 byte write */
157+ if (* (uint8_t * )(TEST_ADDRESS + 1 ) != test_data_1 ) {
158+ wolfBoot_printf ("Unaligned write verification (1 byte) failed\n" );
159+ return -1 ;
160+ }
161+
162+ /* Write 2 bytes spanning sector boundary */
163+ hal_flash_unlock ();
164+ ret = hal_flash_write (TEST_ADDRESS + TEST_SZ - 1 , test_data_2 , 2 );
165+ hal_flash_lock ();
166+ if (ret != 0 ) {
167+ wolfBoot_printf ("Unaligned write (2 bytes) failed: Ret %d\n" , ret );
168+ return -1 ;
169+ }
170+
171+ /* Verify 2 bytes write */
172+ memcpy (read_back , (void * )(TEST_ADDRESS + TEST_SZ - 1 ), 2 );
173+ if (read_back [0 ] != test_data_2 [0 ] || read_back [1 ] != test_data_2 [1 ]) {
174+ wolfBoot_printf ("Unaligned write verification (2 bytes) failed\n" );
175+ return -1 ;
176+ }
177+
178+ wolfBoot_printf ("Unaligned write test passed\n" );
179+ return 0 ;
180+ }
181+ #endif
182+
183+ /* This test can be run only if swapping the flash do not reboot the board */
184+ #if defined(DUALBANK_SWAP ) && !defined(TEST_FLASH_READONLY )
185+ int hal_flash_test_dualbank (void )
186+ {
187+ int ret = 0 ;
188+ uint32_t i , len ;
189+ uint8_t cur_fill = 0xb0 ;
190+ uint8_t new_fill = 0xf0 ;
191+ uint8_t fill ;
192+ uint32_t bankA = TEST_ADDRESS_BANKA ;
193+ uint32_t bankB = TEST_ADDRESS_BANKB ;
194+ uint32_t pagePtr ;
195+
196+ wolfBoot_printf ("swap flash test at 0x%x\n" , TEST_ADDRESS );
197+
198+ for (i = 0 ; i < 2 ; i ++ ) {
199+ fill = (i == 0 ) ? cur_fill : new_fill ;
200+ pagePtr = (i == 0 ) ? TEST_ADDRESS_BANKA : TEST_ADDRESS_BANKB ;
201+
202+ /* Erase sector */
203+ hal_flash_unlock ();
204+ ret = hal_flash_erase (pagePtr , WOLFBOOT_SECTOR_SIZE );
205+ hal_flash_lock ();
206+ if (ret != 0 ) {
207+ wolfBoot_printf ("Erase Sector failed: Ret %d\n" , ret );
208+ return -1 ;
209+ }
210+
211+ /* Write Page */
212+ hal_flash_unlock ();
213+ ret = hal_flash_write (pagePtr , (uint8_t * )& fill , sizeof (fill ));
214+ hal_flash_lock ();
215+ if (ret != 0 ) {
216+ wolfBoot_printf ("Write Page failed: Ret %d\n" , ret );
217+ return -1 ;
218+ }
219+ }
220+
221+ if (* ((uint8_t * )(TEST_ADDRESS_BANKA )) != cur_fill ) {
222+ wolfBoot_printf ("Bank A data mismatch: %x != %x\n" , bankA [0 ], cur_fill );
223+ return -1 ;
224+ }
225+ if (* ((uint8_t * )(TEST_ADDRESS_BANKB )) != new_fill ) {
226+ wolfBoot_printf ("Bank B data mismatch: %x != %x\n" , bankB [0 ], new_fill );
227+ return -1 ;
228+ }
229+ hal_flash_dualbank_swap ();
230+
231+ if (* ((uint8_t * )(TEST_ADDRESS_BANKA )) != new_fill ) {
232+ wolfBoot_printf ("Bank A data mismatch after swap: %x != %x\n" , bankA [0 ], new_fill );
233+ return -1 ;
234+ }
235+ if (* ((uint8_t * )(TEST_ADDRESS_BANKB )) != cur_fill ) {
236+ wolfBoot_printf ("Bank B data mismatch after swap: %x != %x\n" , bankB [0 ], cur_fill );
237+ return -1 ;
238+ }
239+
240+ wolfBoot_printf ("DUALBANK_SWAP test passed" );
241+ return 0 ;
242+ }
243+ #endif /* DUALBANK_SWAP */
244+
77245#endif /* TEST_FLASH */
0 commit comments