From d44f689d2f6dc50094bd1caaef0f5bcee71aebd5 Mon Sep 17 00:00:00 2001 From: Alex Lanzano Date: Tue, 5 Aug 2025 10:39:43 -0400 Subject: [PATCH 1/4] Implement ability to specify flash RamSim memory location instead of using malloc --- benchmark/config/wolfhsm_cfg.h | 2 ++ examples/posix/tcp/wh_server_tcp/wolfhsm_cfg.h | 2 ++ src/wh_flash_ramsim.c | 10 ++++++++++ test/config/wolfhsm_cfg.h | 2 ++ test/wh_test_flash_ramsim.c | 8 ++++++-- tools/whnvmtool/Makefile | 2 +- tools/whnvmtool/test/Makefile | 2 +- wolfhsm/wh_flash_ramsim.h | 1 + 8 files changed, 25 insertions(+), 4 deletions(-) diff --git a/benchmark/config/wolfhsm_cfg.h b/benchmark/config/wolfhsm_cfg.h index e2169a5c..3da0fcb9 100644 --- a/benchmark/config/wolfhsm_cfg.h +++ b/benchmark/config/wolfhsm_cfg.h @@ -41,6 +41,8 @@ #define WOLFHSM_CFG_CERTIFICATE_MANAGER #define WOLFHSM_CFG_CERTIFICATE_MANAGER_ACERT +#define WOLFHSM_CFG_FLASH_RAMSIM_MALLOC + /* Benchmark configs */ #define WOLFHSM_CFG_BENCH_ENABLE #endif /* WOLFHSM_CFG_H_ */ diff --git a/examples/posix/tcp/wh_server_tcp/wolfhsm_cfg.h b/examples/posix/tcp/wh_server_tcp/wolfhsm_cfg.h index 2ef2ce93..91e87d9a 100644 --- a/examples/posix/tcp/wh_server_tcp/wolfhsm_cfg.h +++ b/examples/posix/tcp/wh_server_tcp/wolfhsm_cfg.h @@ -44,6 +44,8 @@ #define WOLFHSM_CFG_CERTIFICATE_MANAGER #define WOLFHSM_CFG_CERTIFICATE_MANAGER_ACERT +#define WOLFHSM_CFG_FLASH_RAMSIM_MALLOC + #define XMEMFENCE() __atomic_thread_fence(__ATOMIC_SEQ_CST) #endif /* WOLFHSM_CFG_H_ */ diff --git a/src/wh_flash_ramsim.c b/src/wh_flash_ramsim.c index 3f4b0e10..10c81a0e 100644 --- a/src/wh_flash_ramsim.c +++ b/src/wh_flash_ramsim.c @@ -26,7 +26,11 @@ #include #include /* For NULL */ + +#if defined(WOLFHSM_CFG_FLASH_RAMSIM_MALLOC) #include /* For malloc/free */ +#endif + #include #include @@ -66,7 +70,11 @@ int whFlashRamsim_Init(void* context, const void* config) ctx->size = cfg->size; ctx->sectorSize = cfg->sectorSize; ctx->pageSize = cfg->pageSize; +#if defined(WOLFHSM_CFG_FLASH_RAMSIM_MALLOC) ctx->memory = (uint8_t*)malloc(ctx->size); +#else + ctx->memory = cfg->memory; +#endif ctx->erasedByte = cfg->erasedByte; ctx->writeLocked = 0; @@ -92,10 +100,12 @@ int whFlashRamsim_Cleanup(void* context) return WH_ERROR_BADARGS; } +#if defined(WOLFHSM_CFG_FLASH_RAMSIM_MALLOC) if (ctx->memory != NULL) { free(ctx->memory); ctx->memory = NULL; } +#endif return WH_ERROR_OK; } diff --git a/test/config/wolfhsm_cfg.h b/test/config/wolfhsm_cfg.h index 780bf47e..1b2a1cc7 100644 --- a/test/config/wolfhsm_cfg.h +++ b/test/config/wolfhsm_cfg.h @@ -43,6 +43,8 @@ #define WOLFHSM_CFG_CERTIFICATE_MANAGER #define WOLFHSM_CFG_CERTIFICATE_MANAGER_ACERT +#define WOLFHSM_CFG_FLASH_RAMSIM_MALLOC + /* Enable Image Manager feature */ #define WOLFHSM_CFG_SERVER_IMG_MGR diff --git a/test/wh_test_flash_ramsim.c b/test/wh_test_flash_ramsim.c index adb6c95c..ea44e5c1 100644 --- a/test/wh_test_flash_ramsim.c +++ b/test/wh_test_flash_ramsim.c @@ -65,7 +65,11 @@ int whTest_Flash_RamSim(void) whFlashRamsimCfg cfg = {.size = TEST_FLASH_SIZE, .sectorSize = TEST_SECTOR_SIZE, .pageSize = TEST_PAGE_SIZE, - .erasedByte = 0xFF}; + .erasedByte = 0xFF, +#ifndef WOLFHSM_CFG_FLASH_RAMSIM_MALLOC + .memory = WH_TEST_FLASH_RAMSIM_MEMORY_ADDR; +#endif + }; uint8_t testData[TEST_PAGE_SIZE] = {0}; uint8_t readData[TEST_PAGE_SIZE] = {0}; @@ -201,4 +205,4 @@ int whTest_Flash_RamSim(void) return 0; } -#endif /* WOLFHSM_CFG_ENABLE_SERVER */ \ No newline at end of file +#endif /* WOLFHSM_CFG_ENABLE_SERVER */ diff --git a/tools/whnvmtool/Makefile b/tools/whnvmtool/Makefile index ec86f575..0cdc0715 100644 --- a/tools/whnvmtool/Makefile +++ b/tools/whnvmtool/Makefile @@ -46,7 +46,7 @@ LIBS = \ LIB_DIRS = CFLAGS = -Wall $(INCLUDE_DIRS) -CFLAGS += -DWOLFSSL_USER_SETTINGS -DWOLFHSM_CFG_ENABLE_SERVER +CFLAGS += -DWOLFSSL_USER_SETTINGS -DWOLFHSM_CFG_ENABLE_SERVER -DWOLFHSM_CFG_FLASH_RAMSIM_MALLOC CFLAGS += -std=c90 -D_GNU_SOURCE -Wno-cpp CFLAGS_EXTRA = # Additional CFLAGS from the command line diff --git a/tools/whnvmtool/test/Makefile b/tools/whnvmtool/test/Makefile index bd1285a7..db9c1283 100644 --- a/tools/whnvmtool/test/Makefile +++ b/tools/whnvmtool/test/Makefile @@ -44,7 +44,7 @@ LIBS = \ LIB_DIRS = CFLAGS = -Wall $(INCLUDE_DIRS) -CFLAGS += -DWOLFSSL_USER_SETTINGS -DWOLFHSM_CFG_ENABLE_SERVER +CFLAGS += -DWOLFSSL_USER_SETTINGS -DWOLFHSM_CFG_ENABLE_SERVER -DWOLFHSM_CFG_FLASH_RAMSIM_MALLOC CFLAGS_EXTRA = # Additional CFLAGS from the command line LDFLAGS = $(LIB_DIRS) $(LIBS) OUT = $(TARGET) # Output executable name diff --git a/wolfhsm/wh_flash_ramsim.h b/wolfhsm/wh_flash_ramsim.h index 562e4c35..501ab1f1 100644 --- a/wolfhsm/wh_flash_ramsim.h +++ b/wolfhsm/wh_flash_ramsim.h @@ -26,6 +26,7 @@ /* Configuration and context structures */ typedef struct { + uint8_t* memory; uint32_t size; uint32_t sectorSize; uint32_t pageSize; From 7c21ba3009b83b198348f48bcbcf433c7e0822e8 Mon Sep 17 00:00:00 2001 From: Alex Lanzano Date: Tue, 5 Aug 2025 14:11:12 -0400 Subject: [PATCH 2/4] Fix integer sign for variables and fields using negative error values --- src/wh_nvm_flash.c | 30 +++++++++++++++-------- test/wh_test_flash_ramsim.c | 2 +- wolfhsm/wh_message_she.h | 48 ++++++++++++++++++------------------- 3 files changed, 45 insertions(+), 35 deletions(-) diff --git a/src/wh_nvm_flash.c b/src/wh_nvm_flash.c index 04a1c1a2..2df53e72 100644 --- a/src/wh_nvm_flash.c +++ b/src/wh_nvm_flash.c @@ -121,8 +121,8 @@ static int nfPartition_CheckDataRange(whNvmFlashContext* context, uint32_t byte_offset, uint32_t byte_count); -static uint32_t nfObject_Offset(whNvmFlashContext* context, int partition, - int object_index); +static int nfObject_Offset(whNvmFlashContext* context, int partition, + int object_index, uint32_t *out_object_offset); static int nfObject_ProgramBegin(whNvmFlashContext* context, int partition, int object_index, uint32_t epoch, uint32_t start, whNvmMetadata* meta); static int nfObject_ProgramDataBytes(whNvmFlashContext* context, int partition, @@ -512,16 +512,19 @@ static int nfPartition_CheckDataRange(whNvmFlashContext* context, return WH_ERROR_OK; } -static uint32_t nfObject_Offset(whNvmFlashContext* context, int partition, - int object_index) +static int nfObject_Offset(whNvmFlashContext* context, int partition, + int object_index, uint32_t *out_object_offset) { - if (context == NULL) { + if (context == NULL || out_object_offset == NULL) { return WH_ERROR_BADARGS; } - return nfPartition_Offset(context,partition) + - NF_PARTITION_DIRECTORY_OFFSET + - NF_DIRECTORY_OBJECT_OFFSET(object_index); + + *out_object_offset = nfPartition_Offset(context,partition) + + NF_PARTITION_DIRECTORY_OFFSET + + NF_DIRECTORY_OBJECT_OFFSET(object_index); + + return WH_ERROR_OK; } static int nfObject_ProgramBegin(whNvmFlashContext* context, int partition, @@ -539,7 +542,10 @@ static int nfObject_ProgramBegin(whNvmFlashContext* context, int partition, return WH_ERROR_BADARGS; } - object_offset = nfObject_Offset(context, partition, object_index); + rc = nfObject_Offset(context, partition, object_index, &object_offset); + if (rc != WH_ERROR_OK) { + return rc; + } /* Program the object epoch */ rc = wh_FlashUnit_Program( @@ -601,6 +607,7 @@ static int nfObject_ProgramDataBytes(whNvmFlashContext* context, int partition, static int nfObject_ProgramFinish(whNvmFlashContext* context, int partition, int object_index, uint32_t byte_count) { + int rc; uint32_t object_offset = 0; whFlashUnit state_count = BASE_STATE | WHFU_BYTES2UNITS(byte_count); @@ -608,7 +615,10 @@ static int nfObject_ProgramFinish(whNvmFlashContext* context, int partition, return WH_ERROR_BADARGS; } - object_offset = nfObject_Offset(context, partition, object_index); + rc = nfObject_Offset(context, partition, object_index, &object_offset); + if (rc != WH_ERROR_OK) { + return rc; + } /* Program the object flag->state_count */ return wh_FlashUnit_Program( diff --git a/test/wh_test_flash_ramsim.c b/test/wh_test_flash_ramsim.c index ea44e5c1..14fc0896 100644 --- a/test/wh_test_flash_ramsim.c +++ b/test/wh_test_flash_ramsim.c @@ -67,7 +67,7 @@ int whTest_Flash_RamSim(void) .pageSize = TEST_PAGE_SIZE, .erasedByte = 0xFF, #ifndef WOLFHSM_CFG_FLASH_RAMSIM_MALLOC - .memory = WH_TEST_FLASH_RAMSIM_MEMORY_ADDR; + .memory = WOLFHSM_CFG_TEST_FLASH_RAMSIM_MEMORY_ADDR; #endif }; diff --git a/wolfhsm/wh_message_she.h b/wolfhsm/wh_message_she.h index ad3e0879..0e28ee78 100644 --- a/wolfhsm/wh_message_she.h +++ b/wolfhsm/wh_message_she.h @@ -43,7 +43,7 @@ typedef struct { /* Set UID Response */ typedef struct { - uint32_t rc; + int32_t rc; uint8_t WH_PAD[4]; } whMessageShe_SetUidResponse; @@ -64,8 +64,8 @@ typedef struct { /* Secure Boot Init Response */ typedef struct { - uint32_t rc; - uint32_t status; + int32_t rc; + int32_t status; } whMessageShe_SecureBootInitResponse; /* Secure Boot Init translation functions */ @@ -88,8 +88,8 @@ typedef struct { /* Secure Boot Update Response */ typedef struct { - uint32_t rc; - uint32_t status; + int32_t rc; + int32_t status; } whMessageShe_SecureBootUpdateResponse; /* Secure Boot Update translation functions */ @@ -103,8 +103,8 @@ int wh_MessageShe_TranslateSecureBootUpdateResponse( /* Secure Boot Finish Response */ typedef struct { - uint32_t rc; - uint32_t status; + int32_t rc; + int32_t status; } whMessageShe_SecureBootFinishResponse; /* Secure Boot Finish translation function */ @@ -114,7 +114,7 @@ int wh_MessageShe_TranslateSecureBootFinishResponse( /* Get Status Response */ typedef struct { - uint32_t rc; + int32_t rc; uint8_t sreg; uint8_t WH_PAD[7]; } whMessageShe_GetStatusResponse; @@ -133,7 +133,7 @@ typedef struct { /* Load Key Response */ typedef struct { - uint32_t rc; + int32_t rc; uint8_t messageFour[WH_SHE_M4_SZ]; uint8_t messageFive[WH_SHE_M5_SZ]; } whMessageShe_LoadKeyResponse; @@ -154,7 +154,7 @@ typedef struct { /* Load Plain Key Response */ typedef struct { - uint32_t rc; + int32_t rc; } whMessageShe_LoadPlainKeyResponse; /* Load Plain Key translation function */ @@ -168,7 +168,7 @@ int wh_MessageShe_TranslateLoadPlainKeyResponse( /* Export RAM Key Response */ typedef struct { - uint32_t rc; + int32_t rc; uint8_t messageOne[WH_SHE_M1_SZ]; uint8_t messageTwo[WH_SHE_M2_SZ]; uint8_t messageThree[WH_SHE_M3_SZ]; @@ -183,8 +183,8 @@ int wh_MessageShe_TranslateExportRamKeyResponse( /* Init RNG Response */ typedef struct { - uint32_t rc; - uint32_t status; + int32_t rc; + int32_t status; } whMessageShe_InitRngResponse; /* Init RNG translation function */ @@ -194,7 +194,7 @@ int wh_MessageShe_TranslateInitRngResponse( /* RND Response */ typedef struct { - uint32_t rc; + int32_t rc; uint8_t rnd[WH_SHE_KEY_SZ]; } whMessageShe_RndResponse; @@ -210,8 +210,8 @@ typedef struct { /* Extend Seed Response */ typedef struct { - uint32_t rc; - uint32_t status; + int32_t rc; + int32_t status; } whMessageShe_ExtendSeedResponse; /* Extend Seed translation functions */ @@ -235,7 +235,7 @@ typedef struct { /* Encrypt ECB Response */ typedef struct { - uint32_t rc; + int32_t rc; uint32_t sz; /* Data follows: * uint8_t out[sz] @@ -264,7 +264,7 @@ typedef struct { /* Encrypt CBC Response */ typedef struct { - uint32_t rc; + int32_t rc; uint32_t sz; /* Data follows: * uint8_t out[sz] @@ -292,7 +292,7 @@ typedef struct { /* Decrypt ECB Response */ typedef struct { - uint32_t rc; + int32_t rc; uint32_t sz; /* Data follows: * uint8_t out[sz] @@ -321,7 +321,7 @@ typedef struct { /* Decrypt CBC Response */ typedef struct { - uint32_t rc; + int32_t rc; uint32_t sz; /* Data follows: * uint8_t out[sz] @@ -348,7 +348,7 @@ typedef struct { /* Generate MAC Response */ typedef struct { - uint32_t rc; + int32_t rc; uint8_t mac[WH_SHE_KEY_SZ]; } whMessageShe_GenMacResponse; @@ -375,8 +375,8 @@ typedef struct { /* Verify MAC Response */ typedef struct { - uint32_t rc; - uint8_t status; + int32_t rc; + int8_t status; uint8_t WH_PAD[7]; } whMessageShe_VerifyMacResponse; @@ -391,4 +391,4 @@ int wh_MessageShe_TranslateVerifyMacResponse( #endif /* WOLFHSM_CFG_SHE_EXTENSION */ -#endif /* !WOLFHSM_WH_MESSAGE_SHE_H_ */ \ No newline at end of file +#endif /* !WOLFHSM_WH_MESSAGE_SHE_H_ */ From 6420f3b5549469c22abb0b4ee25882b970f279c6 Mon Sep 17 00:00:00 2001 From: Alex Lanzano Date: Mon, 18 Aug 2025 15:37:45 -0400 Subject: [PATCH 3/4] Remove malloc from flash ram sim. Update tests to use stack memory for flash ram sim --- benchmark/config/wolfhsm_cfg.h | 2 -- benchmark/wh_bench.c | 4 +++- examples/posix/tcp/wh_server_tcp/wh_server_tcp.c | 3 +++ examples/posix/tcp/wh_server_tcp/wolfhsm_cfg.h | 2 -- src/wh_flash_ramsim.c | 4 ---- test/config/wolfhsm_cfg.h | 2 -- test/wh_test_cert.c | 12 +++++++++--- test/wh_test_clientserver.c | 14 +++++++++++--- test/wh_test_crypto.c | 12 +++++++++--- test/wh_test_flash_ramsim.c | 5 ++--- test/wh_test_nvm_flash.c | 14 ++++++++++---- test/wh_test_server_img_mgr.c | 14 ++++++++++---- test/wh_test_she.c | 12 +++++++++--- test/wh_test_wolfcrypt_test.c | 2 ++ tools/whnvmtool/Makefile | 2 +- tools/whnvmtool/test/Makefile | 2 +- tools/whnvmtool/test/test_whnvmtool.c | 2 ++ 17 files changed, 72 insertions(+), 36 deletions(-) diff --git a/benchmark/config/wolfhsm_cfg.h b/benchmark/config/wolfhsm_cfg.h index 3da0fcb9..e2169a5c 100644 --- a/benchmark/config/wolfhsm_cfg.h +++ b/benchmark/config/wolfhsm_cfg.h @@ -41,8 +41,6 @@ #define WOLFHSM_CFG_CERTIFICATE_MANAGER #define WOLFHSM_CFG_CERTIFICATE_MANAGER_ACERT -#define WOLFHSM_CFG_FLASH_RAMSIM_MALLOC - /* Benchmark configs */ #define WOLFHSM_CFG_BENCH_ENABLE #endif /* WOLFHSM_CFG_H_ */ diff --git a/benchmark/wh_bench.c b/benchmark/wh_bench.c index 63aa2fe0..3caeffda 100644 --- a/benchmark/wh_bench.c +++ b/benchmark/wh_bench.c @@ -562,6 +562,7 @@ int wh_Bench_ClientServer_Posix(void) { uint8_t req[BUFFER_SIZE] = {0}; uint8_t resp[BUFFER_SIZE] = {0}; + uint8_t memory[FLASH_RAM_SIZE] = {0}; /* Transport memory configuration */ whTransportMemConfig tmcf[1] = {{ @@ -601,6 +602,7 @@ int wh_Bench_ClientServer_Posix(void) .sectorSize = FLASH_RAM_SIZE / 2, .pageSize = 8, .erasedByte = (uint8_t)0, + .memory = memory, }}; const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB}; @@ -679,4 +681,4 @@ int wh_Bench_ClientServer_Posix(void) #endif /* WOLFHSM_CFG_TEST_POSIX */ -#endif /* WOLFHSM_CFG_BENCH_ENABLE */ \ No newline at end of file +#endif /* WOLFHSM_CFG_BENCH_ENABLE */ diff --git a/examples/posix/tcp/wh_server_tcp/wh_server_tcp.c b/examples/posix/tcp/wh_server_tcp/wh_server_tcp.c index cbd53184..26ae2acf 100644 --- a/examples/posix/tcp/wh_server_tcp/wh_server_tcp.c +++ b/examples/posix/tcp/wh_server_tcp/wh_server_tcp.c @@ -607,6 +607,8 @@ int main(int argc, char** argv) const char* nvmInitFilePath = NULL; int keyId = WH_KEYID_ERASED; /* Default key ID if none provided */ int clientId = 12; /* Default client ID if none provided */ + uint8_t memory[FLASH_RAM_SIZE] = {0}; + /* Parse command-line arguments */ for (int i = 1; i < argc; i++) { @@ -645,6 +647,7 @@ int main(int argc, char** argv) .sectorSize = FLASH_RAM_SIZE / 2, .pageSize = 8, .erasedByte = (uint8_t)0, + .memory = memory, }}; const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB}; diff --git a/examples/posix/tcp/wh_server_tcp/wolfhsm_cfg.h b/examples/posix/tcp/wh_server_tcp/wolfhsm_cfg.h index 91e87d9a..2ef2ce93 100644 --- a/examples/posix/tcp/wh_server_tcp/wolfhsm_cfg.h +++ b/examples/posix/tcp/wh_server_tcp/wolfhsm_cfg.h @@ -44,8 +44,6 @@ #define WOLFHSM_CFG_CERTIFICATE_MANAGER #define WOLFHSM_CFG_CERTIFICATE_MANAGER_ACERT -#define WOLFHSM_CFG_FLASH_RAMSIM_MALLOC - #define XMEMFENCE() __atomic_thread_fence(__ATOMIC_SEQ_CST) #endif /* WOLFHSM_CFG_H_ */ diff --git a/src/wh_flash_ramsim.c b/src/wh_flash_ramsim.c index 10c81a0e..b5adb767 100644 --- a/src/wh_flash_ramsim.c +++ b/src/wh_flash_ramsim.c @@ -70,11 +70,7 @@ int whFlashRamsim_Init(void* context, const void* config) ctx->size = cfg->size; ctx->sectorSize = cfg->sectorSize; ctx->pageSize = cfg->pageSize; -#if defined(WOLFHSM_CFG_FLASH_RAMSIM_MALLOC) - ctx->memory = (uint8_t*)malloc(ctx->size); -#else ctx->memory = cfg->memory; -#endif ctx->erasedByte = cfg->erasedByte; ctx->writeLocked = 0; diff --git a/test/config/wolfhsm_cfg.h b/test/config/wolfhsm_cfg.h index 1b2a1cc7..780bf47e 100644 --- a/test/config/wolfhsm_cfg.h +++ b/test/config/wolfhsm_cfg.h @@ -43,8 +43,6 @@ #define WOLFHSM_CFG_CERTIFICATE_MANAGER #define WOLFHSM_CFG_CERTIFICATE_MANAGER_ACERT -#define WOLFHSM_CFG_FLASH_RAMSIM_MALLOC - /* Enable Image Manager feature */ #define WOLFHSM_CFG_SERVER_IMG_MGR diff --git a/test/wh_test_cert.c b/test/wh_test_cert.c index 3950b89a..4a3a2aa5 100644 --- a/test/wh_test_cert.c +++ b/test/wh_test_cert.c @@ -53,6 +53,10 @@ static int whTest_CertNonExportable(whClientContext* client); #endif +#define FLASH_RAM_SIZE (1024 * 1024) /* 1MB */ +#define FLASH_SECTOR_SIZE (128 * 1024) /* 128KB */ +#define FLASH_PAGE_SIZE (8) /* 8B */ + #ifdef WOLFHSM_CFG_ENABLE_SERVER /* Run certificate configuration tests */ int whTest_CertServerCfg(whServerConfig* serverCfg) @@ -599,12 +603,14 @@ int whTest_CertRamSim(void) .server_id = 124, }}; /* RamSim Flash state and configuration */ + uint8_t memory[FLASH_RAM_SIZE] = {0}; whFlashRamsimCtx fc[1] = {0}; whFlashRamsimCfg fc_conf[1] = {{ - .size = 1024 * 1024, /* 1MB Flash */ - .sectorSize = 128 * 1024, /* 128KB Sector Size */ - .pageSize = 8, /* 8B Page Size */ + .size = FLASH_RAM_SIZE, /* 1MB Flash */ + .sectorSize = FLASH_SECTOR_SIZE, /* 128KB Sector Size */ + .pageSize = FLASH_PAGE_SIZE, /* 8B Page Size */ .erasedByte = ~(uint8_t)0, + .memory = memory, }}; const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB}; diff --git a/test/wh_test_clientserver.c b/test/wh_test_clientserver.c index 9e218eda..8e953f61 100644 --- a/test/wh_test_clientserver.c +++ b/test/wh_test_clientserver.c @@ -61,6 +61,8 @@ #define REPEAT_COUNT 10 #define ONE_MS 1000 #define FLASH_RAM_SIZE (1024 * 1024) /* 1MB */ +#define FLASH_SECTOR_SIZE (128 * 1024) /* 128KB */ +#define FLASH_PAGE_SIZE (8) /* 8B */ #ifdef WOLFHSM_CFG_DMA #define DMA_TEST_MEM_NWORDS 3 @@ -708,12 +710,14 @@ int whTest_ClientServerSequential(void) }}; /* RamSim Flash state and configuration */ + uint8_t memory[FLASH_RAM_SIZE] = {0}; whFlashRamsimCtx fc[1] = {0}; whFlashRamsimCfg fc_conf[1] = {{ - .size = 1024 * 1024, /* 1MB Flash */ - .sectorSize = 128 * 1024, /* 128KB Sector Size */ - .pageSize = 8, /* 8B Page Size */ + .size = FLASH_RAM_SIZE, /* 1MB Flash */ + .sectorSize = FLASH_SECTOR_SIZE, /* 128KB Sector Size */ + .pageSize = FLASH_PAGE_SIZE, /* 8B Page Size */ .erasedByte = ~(uint8_t)0, + .memory = memory, }}; const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB}; @@ -1734,12 +1738,14 @@ static int wh_ClientServer_MemThreadTest(void) }}; /* RamSim Flash state and configuration */ + uint8_t memory[FLASH_RAM_SIZE] = {0}; whFlashRamsimCtx fc[1] = {0}; whFlashRamsimCfg fc_conf[1] = {{ .size = FLASH_RAM_SIZE, .sectorSize = FLASH_RAM_SIZE/2, .pageSize = 8, .erasedByte = (uint8_t)0, + .memory = memory, }}; const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB}; @@ -1825,12 +1831,14 @@ static int wh_ClientServer_PosixMemMapThreadTest(void) }}; /* RamSim Flash state and configuration */ + uint8_t memory[FLASH_RAM_SIZE] = {0}; whFlashRamsimCtx fc[1] = {0}; whFlashRamsimCfg fc_conf[1] = {{ .size = FLASH_RAM_SIZE, .sectorSize = FLASH_RAM_SIZE / 2, .pageSize = 8, .erasedByte = (uint8_t)0, + .memory = memory, }}; const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB}; diff --git a/test/wh_test_crypto.c b/test/wh_test_crypto.c index f90ba44d..37f307ca 100644 --- a/test/wh_test_crypto.c +++ b/test/wh_test_crypto.c @@ -64,6 +64,10 @@ #include "port/posix/posix_flash_file.h" #endif +#define FLASH_RAM_SIZE (1024 * 1024) /* 1MB */ +#define FLASH_SECTOR_SIZE (128 * 1024) /* 128KB */ +#define FLASH_PAGE_SIZE (8) /* 8B */ + enum { /* Total size needs to fit: * - Transport CSR (whTransportMemCsr) @@ -3028,12 +3032,14 @@ static int wh_ClientServer_MemThreadTest(void) }}; /* RamSim Flash state and configuration */ + uint8_t memory[FLASH_RAM_SIZE] = {0}; whFlashRamsimCtx fc[1] = {0}; whFlashRamsimCfg fc_conf[1] = {{ - .size = 1024 * 1024, /* 1MB Flash */ - .sectorSize = 128 * 1024, /* 128KB Sector Size */ - .pageSize = 8, /* 8B Page Size */ + .size = FLASH_RAM_SIZE, /* 1MB Flash */ + .sectorSize = FLASH_SECTOR_SIZE, /* 128KB Sector Size */ + .pageSize = FLASH_PAGE_SIZE, /* 8B Page Size */ .erasedByte = ~(uint8_t)0, + .memory = memory, }}; const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB}; diff --git a/test/wh_test_flash_ramsim.c b/test/wh_test_flash_ramsim.c index 14fc0896..bec38e65 100644 --- a/test/wh_test_flash_ramsim.c +++ b/test/wh_test_flash_ramsim.c @@ -62,13 +62,12 @@ int whTest_Flash_RamSim(void) { int ret; whFlashRamsimCtx ctx; + uint8_t memory[TEST_FLASH_SIZE] = {0}; whFlashRamsimCfg cfg = {.size = TEST_FLASH_SIZE, .sectorSize = TEST_SECTOR_SIZE, .pageSize = TEST_PAGE_SIZE, .erasedByte = 0xFF, -#ifndef WOLFHSM_CFG_FLASH_RAMSIM_MALLOC - .memory = WOLFHSM_CFG_TEST_FLASH_RAMSIM_MEMORY_ADDR; -#endif + .memory = memory, }; uint8_t testData[TEST_PAGE_SIZE] = {0}; diff --git a/test/wh_test_nvm_flash.c b/test/wh_test_nvm_flash.c index 6c2ec5da..2f83934a 100644 --- a/test/wh_test_nvm_flash.c +++ b/test/wh_test_nvm_flash.c @@ -41,6 +41,10 @@ #include "port/posix/posix_flash_file.h" #endif +#define FLASH_RAM_SIZE (1024 * 1024) /* 1MB */ +#define FLASH_SECTOR_SIZE (4096) /* 4KB */ +#define FLASH_PAGE_SIZE (8) /* 8B */ + #if defined(WOLFHSM_CFG_TEST_VERBOSE) static void _HexDump(const char* p, size_t data_len) { @@ -469,13 +473,15 @@ int whTest_NvmFlashCfg(whNvmFlashConfig* cfg) int whTest_NvmFlash_RamSim(void) { /* HAL Flash state and configuration */ + uint8_t memory[FLASH_RAM_SIZE] = {0}; const whFlashCb myCb[1] = {WH_FLASH_RAMSIM_CB}; whFlashRamsimCtx myHalFlashCtx[1] = {0}; whFlashRamsimCfg myHalFlashCfg[1] = {{ - .size = 1024 * 1024, /* 1MB Flash */ - .sectorSize = 4096, /* 4KB Sector Size */ - .pageSize = 8, /* 8B Page Size */ + .size = FLASH_RAM_SIZE, /* 1MB Flash */ + .sectorSize = FLASH_SECTOR_SIZE, /* 4KB Sector Size */ + .pageSize = FLASH_PAGE_SIZE, /* 8B Page Size */ .erasedByte = (uint8_t)0, + .memory = memory, }}; WH_TEST_RETURN_ON_FAIL(whTest_Flash(myCb, myHalFlashCtx, myHalFlashCfg)); @@ -538,4 +544,4 @@ int whTest_NvmFlash(void) return 0; } -#endif /* WOLFHSM_CFG_ENABLE_SERVER */ \ No newline at end of file +#endif /* WOLFHSM_CFG_ENABLE_SERVER */ diff --git a/test/wh_test_server_img_mgr.c b/test/wh_test_server_img_mgr.c index f901b58d..45b50d4b 100644 --- a/test/wh_test_server_img_mgr.c +++ b/test/wh_test_server_img_mgr.c @@ -54,6 +54,10 @@ #include "wh_test_common.h" +#define FLASH_RAM_SIZE (1024 * 1024) /* 1MB */ +#define FLASH_SECTOR_SIZE (128 * 1024) /* 128KB */ +#define FLASH_PAGE_SIZE (8) /* 8B */ + /* Test data to be "verified" */ static const uint8_t testData[] = { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, @@ -1214,12 +1218,14 @@ int whTest_ServerImgMgr(void) }}; /* RamSim Flash state and configuration */ + uint8_t memory[FLASH_RAM_SIZE] = {0}; whFlashRamsimCtx fc[1] = {0}; whFlashRamsimCfg fc_conf[1] = {{ - .size = 1024 * 1024, /* 1MB Flash */ - .sectorSize = 128 * 1024, /* 128KB Sector Size */ - .pageSize = 8, /* 8B Page Size */ + .size = FLASH_RAM_SIZE, /* 1MB Flash */ + .sectorSize = FLASH_SECTOR_SIZE, /* 128KB Sector Size */ + .pageSize = FLASH_PAGE_SIZE, /* 8B Page Size */ .erasedByte = ~(uint8_t)0, + .memory = memory, }}; const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB}; @@ -1297,4 +1303,4 @@ int whTest_ServerImgMgr(void) #endif /* WOLFHSM_CFG_NO_CRYPTO */ #endif /* WOLFHSM_CFG_SERVER_IMG_MGR && WOLFHSM_CFG_ENABLE_SERVER && - !WOLFHSM_CFG_NO_CRYPTO */ \ No newline at end of file + !WOLFHSM_CFG_NO_CRYPTO */ diff --git a/test/wh_test_she.c b/test/wh_test_she.c index 171de40f..7e577782 100644 --- a/test/wh_test_she.c +++ b/test/wh_test_she.c @@ -77,6 +77,10 @@ enum { BUFFER_SIZE = 4096, }; +#define FLASH_RAM_SIZE (1024 * 1024) /* 1MB */ +#define FLASH_SECTOR_SIZE (128 * 1024) /* 128KB */ +#define FLASH_PAGE_SIZE (8) /* 8B */ + #ifdef WOLFHSM_CFG_ENABLE_CLIENT /* Helper function to destroy a SHE key so the unit tests don't * leak NVM objects across invocations. Necessary, as SHE doesn't expose a @@ -535,12 +539,14 @@ static int wh_ClientServer_MemThreadTest(void) }}; /* RamSim Flash state and configuration */ + uint8_t memory[FLASH_RAM_SIZE] = {0}; whFlashRamsimCtx fc[1] = {0}; whFlashRamsimCfg fc_conf[1] = {{ - .size = 1024 * 1024, /* 1MB Flash */ - .sectorSize = 128 * 1024, /* 128KB Sector Size */ - .pageSize = 8, /* 8B Page Size */ + .size = FLASH_RAM_SIZE, /* 1MB Flash */ + .sectorSize = FLASH_SECTOR_SIZE, /* 128KB Sector Size */ + .pageSize = FLASH_PAGE_SIZE, /* 8B Page Size */ .erasedByte = ~(uint8_t)0, + .memory = memory, }}; const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB}; diff --git a/test/wh_test_wolfcrypt_test.c b/test/wh_test_wolfcrypt_test.c index 8fba34d1..6eae7a62 100644 --- a/test/wh_test_wolfcrypt_test.c +++ b/test/wh_test_wolfcrypt_test.c @@ -205,12 +205,14 @@ static int wh_ClientServer_MemThreadTest(void) }}; /* RamSim Flash state and configuration */ + uint8_t memory[FLASH_RAM_SIZE] = {0}; whFlashRamsimCtx fc[1] = {0}; whFlashRamsimCfg fc_conf[1] = {{ .size = FLASH_RAM_SIZE, .sectorSize = FLASH_RAM_SIZE / 2, .pageSize = 8, .erasedByte = (uint8_t)0, + .memory = memory, }}; const whFlashCb fcb[1] = {WH_FLASH_RAMSIM_CB}; diff --git a/tools/whnvmtool/Makefile b/tools/whnvmtool/Makefile index 0cdc0715..ec86f575 100644 --- a/tools/whnvmtool/Makefile +++ b/tools/whnvmtool/Makefile @@ -46,7 +46,7 @@ LIBS = \ LIB_DIRS = CFLAGS = -Wall $(INCLUDE_DIRS) -CFLAGS += -DWOLFSSL_USER_SETTINGS -DWOLFHSM_CFG_ENABLE_SERVER -DWOLFHSM_CFG_FLASH_RAMSIM_MALLOC +CFLAGS += -DWOLFSSL_USER_SETTINGS -DWOLFHSM_CFG_ENABLE_SERVER CFLAGS += -std=c90 -D_GNU_SOURCE -Wno-cpp CFLAGS_EXTRA = # Additional CFLAGS from the command line diff --git a/tools/whnvmtool/test/Makefile b/tools/whnvmtool/test/Makefile index db9c1283..bd1285a7 100644 --- a/tools/whnvmtool/test/Makefile +++ b/tools/whnvmtool/test/Makefile @@ -44,7 +44,7 @@ LIBS = \ LIB_DIRS = CFLAGS = -Wall $(INCLUDE_DIRS) -CFLAGS += -DWOLFSSL_USER_SETTINGS -DWOLFHSM_CFG_ENABLE_SERVER -DWOLFHSM_CFG_FLASH_RAMSIM_MALLOC +CFLAGS += -DWOLFSSL_USER_SETTINGS -DWOLFHSM_CFG_ENABLE_SERVER CFLAGS_EXTRA = # Additional CFLAGS from the command line LDFLAGS = $(LIB_DIRS) $(LIBS) OUT = $(TARGET) # Output executable name diff --git a/tools/whnvmtool/test/test_whnvmtool.c b/tools/whnvmtool/test/test_whnvmtool.c index d766fd34..da550884 100644 --- a/tools/whnvmtool/test/test_whnvmtool.c +++ b/tools/whnvmtool/test/test_whnvmtool.c @@ -79,6 +79,7 @@ whCommServerConfig gCommServerConfig[1] = {{ /* Global NVM Configurations that should be checked */ /* RamSim Flash state and configuration */ +uint8_t memory[FLASH_PARTITION_SIZE * 2] = {0}; whFlashRamsimCtx gFlashRamsimContext[1] = {0}; whFlashRamsimCfg gFlashRamsimConfig[1] = {{ .size = FLASH_PARTITION_SIZE * 2, @@ -86,6 +87,7 @@ whFlashRamsimCfg gFlashRamsimConfig[1] = {{ .pageSize = 8, .erasedByte = FLASH_ERASED_BYTE, .initData = NULL, /* Init data will be set dynamically */ + .memory = memory, }}; const whFlashCb gFlashRamsimCb[1] = {WH_FLASH_RAMSIM_CB}; #define INIT_RAMSIM_NVM_FLASH_CONFIG \ From e6c148686204a21b2fe505781a544126f55ef4fb Mon Sep 17 00:00:00 2001 From: Alex Lanzano Date: Tue, 19 Aug 2025 09:47:16 -0400 Subject: [PATCH 4/4] Remove all traces of WOLFHSM_CFG_FLASH_RAMSIM_MALLOC. Move arg checks to top of function. --- src/wh_flash_ramsim.c | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/wh_flash_ramsim.c b/src/wh_flash_ramsim.c index b5adb767..e364be99 100644 --- a/src/wh_flash_ramsim.c +++ b/src/wh_flash_ramsim.c @@ -27,10 +27,6 @@ #include #include /* For NULL */ -#if defined(WOLFHSM_CFG_FLASH_RAMSIM_MALLOC) -#include /* For malloc/free */ -#endif - #include #include @@ -63,10 +59,12 @@ int whFlashRamsim_Init(void* context, const void* config) const whFlashRamsimCfg* cfg = (const whFlashRamsimCfg*)config; if (ctx == NULL || cfg == NULL || (cfg->sectorSize == 0) || - (cfg->pageSize == 0) || (cfg->sectorSize % cfg->pageSize != 0)) { + (cfg->pageSize == 0) || (cfg->sectorSize % cfg->pageSize != 0) || + cfg->memory == NULL || cfg->size == 0) { return WH_ERROR_BADARGS; } + memset(ctx, 0, sizeof(*ctx)); ctx->size = cfg->size; ctx->sectorSize = cfg->sectorSize; ctx->pageSize = cfg->pageSize; @@ -74,10 +72,6 @@ int whFlashRamsim_Init(void* context, const void* config) ctx->erasedByte = cfg->erasedByte; ctx->writeLocked = 0; - if (!ctx->memory) { - return WH_ERROR_BADARGS; - } - /* Initialize memory based on initData or simulate starting from erased flash */ if (cfg->initData != NULL) { memcpy(ctx->memory, cfg->initData, ctx->size); @@ -96,13 +90,6 @@ int whFlashRamsim_Cleanup(void* context) return WH_ERROR_BADARGS; } -#if defined(WOLFHSM_CFG_FLASH_RAMSIM_MALLOC) - if (ctx->memory != NULL) { - free(ctx->memory); - ctx->memory = NULL; - } -#endif - return WH_ERROR_OK; }