Skip to content

Commit 21ea606

Browse files
authored
Fix bug in NVM flash layer where erased flash was unconditionally read (#181)
from, preventing proper partition initialization.
1 parent 5a5f35b commit 21ea606

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

src/wh_nvm_flash.c

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -189,27 +189,21 @@ static int nfMemState_Read(whNvmFlashContext* context, uint32_t offset,
189189
return blank_count;
190190
}
191191

192-
/* No errors blank checking. Read all the nfState from flash */
193-
ret = wh_FlashUnit_Read(
194-
context->cb,
195-
context->flash,
196-
offset,
197-
NF_UNITS_PER_STATE,
198-
(whFlashUnit*) &buffer);
199-
if (ret != 0) {
200-
/* Error reading state*/
201-
return ret;
202-
}
203-
204-
/* Ok to copy all data members into memState, even if blank */
205-
state->epoch = buffer.epoch;
206-
state->start = buffer.start;
207-
state->count = buffer.count;
208-
209192
/* Compute status based on which state members are blank */
210193
if ( (blank_epoch == WH_ERROR_NOTBLANK) &&
211194
(blank_start == WH_ERROR_NOTBLANK) &&
212195
(blank_count == WH_ERROR_NOTBLANK)) {
196+
ret = wh_FlashUnit_Read(context->cb, context->flash, offset,
197+
NF_UNITS_PER_STATE, (whFlashUnit*)&buffer);
198+
if (ret != 0) {
199+
/* Error reading state*/
200+
return ret;
201+
}
202+
203+
state->epoch = buffer.epoch;
204+
state->start = buffer.start;
205+
state->count = buffer.count;
206+
213207
/* Used */
214208
state->status = NF_STATUS_USED;
215209
} else if ( (blank_epoch == WH_ERROR_NOTBLANK) &&
@@ -852,7 +846,7 @@ int wh_NvmFlash_Init(void* c, const void* cf)
852846
{
853847
whNvmFlashContext* context = c;
854848
const whNvmFlashConfig* config = cf;
855-
int ret = 0;
849+
int ret = WH_ERROR_OK;
856850

857851
if ( (context == NULL) ||
858852
(config == NULL) ||
@@ -863,7 +857,7 @@ int wh_NvmFlash_Init(void* c, const void* cf)
863857
if (config->cb->Init != NULL) {
864858
ret = config->cb->Init(config->context, config->config);
865859
}
866-
if(ret == 0) {
860+
if (ret == WH_ERROR_OK) {
867861
/* Initialize and setup context */
868862
memset(context, 0, sizeof(*context));
869863
context->cb = config->cb;
@@ -877,14 +871,16 @@ int wh_NvmFlash_Init(void* c, const void* cf)
877871
}
878872

879873
/* Unlock the both partitions */
880-
nfPartition_WriteUnlock(context, 0);
881-
nfPartition_WriteUnlock(context, 1);
874+
(void)nfPartition_WriteUnlock(context, 0);
875+
(void)nfPartition_WriteUnlock(context, 1);
882876

883877
nfMemState part_states[2];
884878

885-
/* Recover the partition states to determine which should be active */
886-
nfPartition_ReadMemState(context, 0 , &part_states[0]);
887-
nfPartition_ReadMemState(context, 1 , &part_states[1]);
879+
/* Recover the partition states to determine which should be active.
880+
* No need to check error returns, since output state is initialized
881+
* to unknown */
882+
(void)nfPartition_ReadMemState(context, 0, &part_states[0]);
883+
(void)nfPartition_ReadMemState(context, 1, &part_states[1]);
888884

889885
/* Decide which directory should be active */
890886
if ( (part_states[0].status == NF_STATUS_USED) &&
@@ -905,18 +901,26 @@ int wh_NvmFlash_Init(void* c, const void* cf)
905901
(part_states[1].status == NF_STATUS_FREE)) {
906902
/* Both are blank. Set active to 0 and initialize */
907903
context->active = 0;
908-
nfPartition_ProgramInit(context,
909-
context->active);
904+
ret = nfPartition_ProgramInit(context, context->active);
905+
}
906+
else {
907+
/* Both are corrupted or one partition is corrupted and another is
908+
* free. Attempt to reinitialize and set active. Same behavior as
909+
* blank for now */
910+
context->active = 0;
911+
ret = nfPartition_ProgramInit(context, context->active);
910912
}
911913

912-
ret = nfPartition_ReadMemDirectory(
913-
context,
914-
context->active,
915-
&context->directory);
916-
ret = nfMemDirectory_Parse(&context->directory);
917-
918-
context->initialized = 1;
919-
return 0;
914+
if (ret == WH_ERROR_OK) {
915+
ret = nfPartition_ReadMemDirectory(context, context->active,
916+
&context->directory);
917+
if (ret == WH_ERROR_OK) {
918+
ret = nfMemDirectory_Parse(&context->directory);
919+
if (ret == WH_ERROR_OK) {
920+
context->initialized = 1;
921+
}
922+
}
923+
}
920924
}
921925
return ret;
922926
}

0 commit comments

Comments
 (0)