Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion benchmark/wh_bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {{
Expand Down Expand Up @@ -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};

Expand Down Expand Up @@ -679,4 +681,4 @@ int wh_Bench_ClientServer_Posix(void)

#endif /* WOLFHSM_CFG_TEST_POSIX */

#endif /* WOLFHSM_CFG_BENCH_ENABLE */
#endif /* WOLFHSM_CFG_BENCH_ENABLE */
3 changes: 3 additions & 0 deletions examples/posix/tcp/wh_server_tcp/wh_server_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down Expand Up @@ -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};

Expand Down
17 changes: 5 additions & 12 deletions src/wh_flash_ramsim.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#include <stdint.h>
#include <stddef.h> /* For NULL */
#include <stdlib.h> /* For malloc/free */

#include <string.h>
#include <stdbool.h>

Expand Down Expand Up @@ -59,21 +59,19 @@ 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;
ctx->memory = (uint8_t*)malloc(ctx->size);
ctx->memory = cfg->memory;
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);
Expand All @@ -92,11 +90,6 @@ int whFlashRamsim_Cleanup(void* context)
return WH_ERROR_BADARGS;
}

if (ctx->memory != NULL) {
free(ctx->memory);
ctx->memory = NULL;
}

return WH_ERROR_OK;
}

Expand Down
30 changes: 20 additions & 10 deletions src/wh_nvm_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand Down Expand Up @@ -601,14 +607,18 @@ 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);

if ((context == NULL) || (context->cb == NULL)) {
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(
Expand Down
12 changes: 9 additions & 3 deletions test/wh_test_cert.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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};

Expand Down
14 changes: 11 additions & 3 deletions test/wh_test_clientserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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};

Expand Down Expand Up @@ -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};

Expand Down Expand Up @@ -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};

Expand Down
12 changes: 9 additions & 3 deletions test/wh_test_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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};

Expand Down
7 changes: 5 additions & 2 deletions test/wh_test_flash_ramsim.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ 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};
.erasedByte = 0xFF,
.memory = memory,
};

uint8_t testData[TEST_PAGE_SIZE] = {0};
uint8_t readData[TEST_PAGE_SIZE] = {0};
Expand Down Expand Up @@ -201,4 +204,4 @@ int whTest_Flash_RamSim(void)
return 0;
}

#endif /* WOLFHSM_CFG_ENABLE_SERVER */
#endif /* WOLFHSM_CFG_ENABLE_SERVER */
14 changes: 10 additions & 4 deletions test/wh_test_nvm_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -538,4 +544,4 @@ int whTest_NvmFlash(void)
return 0;
}

#endif /* WOLFHSM_CFG_ENABLE_SERVER */
#endif /* WOLFHSM_CFG_ENABLE_SERVER */
14 changes: 10 additions & 4 deletions test/wh_test_server_img_mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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};

Expand Down Expand Up @@ -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 */
!WOLFHSM_CFG_NO_CRYPTO */
12 changes: 9 additions & 3 deletions test/wh_test_she.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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};

Expand Down
Loading