2929#include "wolfhsm/wh_error.h"
3030#include "wolfhsm/wh_nvm.h"
3131#include "wolfhsm/wh_nvm_flash.h"
32+ #include "wolfhsm/wh_flash_unit.h"
3233
3334/* NVM simulator backends to use for testing NVM module */
3435#include "wolfhsm/wh_flash_ramsim.h"
@@ -180,6 +181,131 @@ static int destroyObjectWithReadBackCheck(const whNvmCb* cb,
180181 return 0 ;
181182}
182183
184+ int whTest_Flash (const whFlashCb * fcb , void * fctx , const void * cfg )
185+ {
186+ uint8_t write_bytes [8 ] = { 0xF0 , 0xE1 , 0xD2 , 0xC3 , 0xB4 , 0xA5 , 0x96 , 0x87 };
187+ uint8_t read_bytes [8 ] = {0 };
188+ whFlashUnit write_buffer [4 ] = {0 };
189+ whFlashUnit read_buffer [4 ] = {0 };
190+
191+ uint32_t partition_units = 0 ;
192+
193+ WH_TEST_RETURN_ON_FAIL (fcb -> Init (fctx , cfg ));
194+
195+ partition_units = wh_FlashUnit_Bytes2Units (fcb -> PartitionSize (fctx )) ;
196+
197+ /* Unlock the first partition */
198+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_WriteUnlock (fcb , fctx ,
199+ 0 , partition_units ));
200+
201+ /* Erase the first partition */
202+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_Erase (fcb , fctx ,
203+ 0 , partition_units ));
204+
205+ /* Blank check the first partition */
206+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_BlankCheck (fcb , fctx ,
207+ 0 , partition_units ));
208+
209+ /* Program a few different unit sizes */
210+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_Program (fcb , fctx ,
211+ 0 , 1 , write_buffer ));
212+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_Program (fcb , fctx ,
213+ 1 , 2 , write_buffer ));
214+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_Program (fcb , fctx ,
215+ 3 , 3 , write_buffer ));
216+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_Program (fcb , fctx ,
217+ 6 , 4 , write_buffer ));
218+
219+ /* Read back and check */
220+ memset (read_buffer , 0 , sizeof (read_buffer ));
221+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_Read (fcb , fctx ,
222+ 0 , 1 , read_buffer ));
223+ WH_TEST_RETURN_ON_FAIL (memcmp (write_buffer , read_buffer ,
224+ 1 * WHFU_BYTES_PER_UNIT ));
225+ memset (read_buffer , 0 , sizeof (read_buffer ));
226+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_Read (fcb , fctx ,
227+ 1 , 2 , read_buffer ));
228+ WH_TEST_RETURN_ON_FAIL (memcmp (write_buffer , read_buffer ,
229+ 2 * WHFU_BYTES_PER_UNIT ));
230+ memset (read_buffer , 0 , sizeof (read_buffer ));
231+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_Read (fcb , fctx ,
232+ 3 , 3 , read_buffer ));
233+ WH_TEST_RETURN_ON_FAIL (memcmp (write_buffer , read_buffer ,
234+ 3 * WHFU_BYTES_PER_UNIT ));
235+ memset (read_buffer , 0 , sizeof (read_buffer ));
236+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_Read (fcb , fctx ,
237+ 6 , 4 , read_buffer ));
238+ WH_TEST_RETURN_ON_FAIL (memcmp (write_buffer , read_buffer ,
239+ 4 * WHFU_BYTES_PER_UNIT ));
240+
241+ /* Program a few different byte sizes */
242+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_ProgramBytes (fcb , fctx ,
243+ 10 * WHFU_BYTES_PER_UNIT , 1 , write_bytes ));
244+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_ProgramBytes (fcb , fctx ,
245+ 11 * WHFU_BYTES_PER_UNIT , 2 , write_bytes ));
246+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_ProgramBytes (fcb , fctx ,
247+ 12 * WHFU_BYTES_PER_UNIT , 3 , write_bytes ));
248+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_ProgramBytes (fcb , fctx ,
249+ 13 * WHFU_BYTES_PER_UNIT , 4 , write_bytes ));
250+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_ProgramBytes (fcb , fctx ,
251+ 14 * WHFU_BYTES_PER_UNIT , 5 , write_bytes ));
252+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_ProgramBytes (fcb , fctx ,
253+ 15 * WHFU_BYTES_PER_UNIT , 6 , write_bytes ));
254+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_ProgramBytes (fcb , fctx ,
255+ 16 * WHFU_BYTES_PER_UNIT , 7 , write_bytes ));
256+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_ProgramBytes (fcb , fctx ,
257+ 17 * WHFU_BYTES_PER_UNIT , 8 , write_bytes ));
258+
259+ /* Read back and compare */
260+ memset (read_bytes , 0 , sizeof (read_bytes ));
261+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_ReadBytes (fcb , fctx ,
262+ 10 * WHFU_BYTES_PER_UNIT , 1 , read_bytes ));
263+ WH_TEST_RETURN_ON_FAIL (memcmp (write_bytes , read_bytes , 1 ));
264+ memset (read_bytes , 0 , sizeof (read_bytes ));
265+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_ReadBytes (fcb , fctx ,
266+ 11 * WHFU_BYTES_PER_UNIT , 2 , read_bytes ));
267+ WH_TEST_RETURN_ON_FAIL (memcmp (write_bytes , read_bytes , 2 ));
268+ memset (read_bytes , 0 , sizeof (read_bytes ));
269+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_ReadBytes (fcb , fctx ,
270+ 12 * WHFU_BYTES_PER_UNIT , 3 , read_bytes ));
271+ WH_TEST_RETURN_ON_FAIL (memcmp (write_bytes , read_bytes , 3 ));
272+ memset (read_bytes , 0 , sizeof (read_bytes ));
273+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_ReadBytes (fcb , fctx ,
274+ 13 * WHFU_BYTES_PER_UNIT , 4 , read_bytes ));
275+ WH_TEST_RETURN_ON_FAIL (memcmp (write_bytes , read_bytes , 4 ));
276+ memset (read_bytes , 0 , sizeof (read_bytes ));
277+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_ReadBytes (fcb , fctx ,
278+ 14 * WHFU_BYTES_PER_UNIT , 5 , read_bytes ));
279+ WH_TEST_RETURN_ON_FAIL (memcmp (write_bytes , read_bytes , 5 ));
280+ memset (read_bytes , 0 , sizeof (read_bytes ));
281+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_ReadBytes (fcb , fctx ,
282+ 15 * WHFU_BYTES_PER_UNIT , 6 , read_bytes ));
283+ WH_TEST_RETURN_ON_FAIL (memcmp (write_bytes , read_bytes , 6 ));
284+ memset (read_bytes , 0 , sizeof (read_bytes ));
285+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_ReadBytes (fcb , fctx ,
286+ 16 * WHFU_BYTES_PER_UNIT , 7 , read_bytes ));
287+ WH_TEST_RETURN_ON_FAIL (memcmp (write_bytes , read_bytes , 7 ));
288+ memset (read_bytes , 0 , sizeof (read_bytes ));
289+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_ReadBytes (fcb , fctx ,
290+ 17 * WHFU_BYTES_PER_UNIT , 8 , read_bytes ));
291+ WH_TEST_RETURN_ON_FAIL (memcmp (write_bytes , read_bytes , 8 ));
292+
293+ /* Erase the first partition */
294+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_Erase (fcb , fctx ,
295+ 0 , partition_units ));
296+
297+ /* Blank check the first partition */
298+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_BlankCheck (fcb , fctx ,
299+ 0 , partition_units ));
300+
301+ /* Lock the first partition */
302+ WH_TEST_RETURN_ON_FAIL (wh_FlashUnit_WriteLock (fcb , fctx ,
303+ 0 , partition_units ));
304+
305+ WH_TEST_RETURN_ON_FAIL (fcb -> Cleanup (fctx ));
306+
307+ return 0 ;
308+ }
183309
184310int whTest_NvmFlashCfg (whNvmFlashConfig * cfg )
185311{
@@ -350,6 +476,8 @@ int whTest_NvmFlash_RamSim(void)
350476 .erasedByte = (uint8_t )0 ,
351477 }};
352478
479+ WH_TEST_RETURN_ON_FAIL (whTest_Flash (myCb , myHalFlashCtx , myHalFlashCfg ));
480+
353481 /* NVM Configuration using PosixSim HAL Flash */
354482 whNvmFlashConfig myNvmCfg = {
355483 .cb = myCb ,
@@ -375,6 +503,8 @@ int whTest_NvmFlash_PosixFileSim(void)
375503 .erased_byte = (~(uint8_t )0 ),
376504 }};
377505
506+ WH_TEST_RETURN_ON_FAIL (whTest_Flash (myCb , myHalFlashContext ,
507+ myHalFlashConfig ));
378508
379509 /* NVM Configuration using PosixSim HAL Flash */
380510 whNvmFlashConfig myNvmCfg = {
@@ -383,7 +513,6 @@ int whTest_NvmFlash_PosixFileSim(void)
383513 .config = myHalFlashConfig ,
384514 };
385515
386-
387516 WH_TEST_ASSERT (0 == whTest_NvmFlashCfg (& myNvmCfg ));
388517
389518 /* Remove the configured file on success*/
0 commit comments