Skip to content

Commit 18f270b

Browse files
authored
nvm: add flash log implementation (#179)
* nvm: nvm flash log based implementation it can be used when the write granularity is too big to be used with the current wh_nvm_flash implementation. * test: generalize whTest_NvmFlashCfg * test: test nvm flash log backend * test: use nvm_flash_log to do test if compiled it * test: generalize tests to use both NVM backends * nvm_flash_log: fix and refactor List callback * nvm_flash_log: rename and add arg checking to local functions * nvm_flash_log: check context is initialized in public functions * nvm_flash_log: fix mem clean on obj destroy + minors * nvm_flash_log: allow NULL metadata in GetMetadata * nvm_flash_log: read-back current partition if update fails * nvm_flash_log: add performance consideration in header * nvm_flash_log: verify program and erase * nvm_flash_log: fix from cppcheck * widetree: fix clang-format error * nvm_flash_log: move configuration define in test/config/wolfhsm_cfg.h * nvm_flash_log: better naming for test structure and functions * nvm_flash_log: erase inactive partition on partition commit * nvm_flash_log: fix object limit in addObject * git-clang-format fixes * nvm_flash_log: test: remove useless assert * nvm_flash_log: test: rename nvm backend enums
1 parent 77285da commit 18f270b

18 files changed

+1085
-148
lines changed

src/wh_nvm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ int wh_Nvm_DestroyObjects(whNvmContext* context, whNvmId list_count,
192192

193193

194194
int wh_Nvm_Read(whNvmContext* context, whNvmId id, whNvmSize offset,
195-
whNvmSize data_len, uint8_t* data)
195+
whNvmSize data_len, uint8_t* data)
196196
{
197197
if ( (context == NULL) ||
198198
(context->cb == NULL) ) {

src/wh_nvm_flash.c

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ static int nfObject_Program(whNvmFlashContext* context, int partition,
133133
int object_index, uint32_t epoch, whNvmMetadata* meta, uint32_t start,
134134
const uint8_t* data);
135135
static int nfObject_ReadDataBytes(whNvmFlashContext* context, int partition,
136-
int object_index, uint32_t byte_offset, uint32_t byte_count,
137-
uint8_t* out_data);
136+
int object_index, uint32_t byte_offset,
137+
uint32_t byte_count, uint8_t* out_data);
138138
static int nfObject_Copy(whNvmFlashContext* context, int object_index,
139139
int partition, uint32_t *inout_next_object, uint32_t *inout_next_data);
140140

@@ -652,8 +652,8 @@ static int nfObject_Program(whNvmFlashContext* context, int partition,
652652
}
653653

654654
static int nfObject_ReadDataBytes(whNvmFlashContext* context, int partition,
655-
int object_index,
656-
uint32_t byte_offset, uint32_t byte_count, uint8_t* out_data)
655+
int object_index, uint32_t byte_offset,
656+
uint32_t byte_count, uint8_t* out_data)
657657
{
658658
int start = 0;
659659
uint32_t startOffset = 0;
@@ -676,18 +676,16 @@ static int nfObject_ReadDataBytes(whNvmFlashContext* context, int partition,
676676
}
677677

678678
/* Ensure we don't read off the end of the active partition */
679-
if (WH_ERROR_OK != nfPartition_CheckDataRange(context, partition,
680-
startOffset * WHFU_BYTES_PER_UNIT + byte_offset,
681-
byte_count)) {
679+
if (WH_ERROR_OK != nfPartition_CheckDataRange(
680+
context, partition,
681+
startOffset * WHFU_BYTES_PER_UNIT + byte_offset,
682+
byte_count)) {
682683
return WH_ERROR_BADARGS;
683684
}
684685

685686
return wh_FlashUnit_ReadBytes(
686-
context->cb,
687-
context->flash,
688-
startOffset * WHFU_BYTES_PER_UNIT + byte_offset,
689-
byte_count,
690-
out_data);
687+
context->cb, context->flash,
688+
startOffset * WHFU_BYTES_PER_UNIT + byte_offset, byte_count, out_data);
691689
}
692690

693691
static int nfObject_Copy(whNvmFlashContext* context, int object_index,
@@ -728,13 +726,8 @@ static int nfObject_Copy(whNvmFlashContext* context, int object_index,
728726
}
729727

730728
/* Read the data from the old object. */
731-
ret = nfObject_ReadDataBytes(
732-
context,
733-
context->active,
734-
object_index,
735-
data_offset,
736-
this_len,
737-
buffer);
729+
ret = nfObject_ReadDataBytes(context, context->active, object_index,
730+
data_offset, this_len, buffer);
738731
if (ret != 0) return ret;
739732

740733
/* Write the data to the new object. */
@@ -1243,30 +1236,23 @@ int wh_NvmFlash_DestroyObjects(void* c, whNvmId list_count,
12431236
}
12441237

12451238
/* Read the data of the object starting at the byte offset */
1246-
int wh_NvmFlash_Read(void* c, whNvmId id, whNvmSize offset,
1247-
whNvmSize data_len, uint8_t* data)
1239+
int wh_NvmFlash_Read(void* c, whNvmId id, whNvmSize offset, whNvmSize data_len,
1240+
uint8_t* data)
12481241
{
12491242
whNvmFlashContext* context = c;
12501243
int ret = 0;
1251-
int object_index = -1;
1244+
int object_index = -1;
12521245

12531246
if ( (context == NULL) ||
12541247
((data_len > 0) && (data == NULL)) ){
12551248
return WH_ERROR_BADARGS;
12561249
}
12571250

1258-
ret = nfMemDirectory_FindObjectIndexById(
1259-
&context->directory,
1260-
id,
1261-
&object_index);
1251+
ret = nfMemDirectory_FindObjectIndexById(&context->directory, id,
1252+
&object_index);
12621253
if (ret == 0) {
1263-
ret = nfObject_ReadDataBytes(
1264-
context,
1265-
context->active,
1266-
object_index,
1267-
offset,
1268-
data_len,
1269-
data);
1254+
ret = nfObject_ReadDataBytes(context, context->active, object_index,
1255+
offset, data_len, data);
12701256
}
12711257
return ret;
12721258
}

0 commit comments

Comments
 (0)