Skip to content

Commit a19079c

Browse files
committed
nvm_flash_log: read-back current partition if update fails
1 parent 7af507f commit a19079c

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

src/wh_nvm_flash_log.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,23 @@ static int nfl_PartitionNewEpoch(whNvmFlashLogContext* ctx)
340340
return WH_ERROR_OK;
341341
}
342342

343+
static int nfl_PartitionNewEpochOrFallback(whNvmFlashLogContext* ctx)
344+
{
345+
int ret;
346+
347+
if (ctx == NULL)
348+
return WH_ERROR_BADARGS;
349+
ret = nfl_PartitionNewEpoch(ctx);
350+
351+
if (ret != WH_ERROR_OK) {
352+
/* swtiching to new partition failed for a reason, try to restore
353+
* back active partition. */
354+
nfl_PartitionRead(ctx);
355+
}
356+
357+
return ret;
358+
}
359+
343360
/* Initialization function */
344361
int wh_NvmFlashLog_Init(void* c, const void* cf)
345362
{
@@ -525,7 +542,7 @@ int wh_NvmFlashLog_AddObject(void* c, whNvmMetadata* meta, whNvmSize data_len,
525542
whNvmFlashLogContext* ctx = (whNvmFlashLogContext*)c;
526543
whNvmFlashLogMetadata *obj, *old_obj;
527544
uint32_t available_space;
528-
uint32_t ret;
545+
int ret;
529546
uint32_t count;
530547

531548
if (ctx == NULL || !ctx->is_initialized || meta == NULL || (data_len > 0 && data == NULL))
@@ -562,7 +579,7 @@ int wh_NvmFlashLog_AddObject(void* c, whNvmMetadata* meta, whNvmSize data_len,
562579
ctx->directory.header.size +=
563580
sizeof(whNvmFlashLogMetadata) + PAD_SIZE(data_len);
564581

565-
return nfl_PartitionNewEpoch(ctx);
582+
return nfl_PartitionNewEpochOrFallback(ctx);
566583
}
567584

568585
/* Destroy objects by id list */
@@ -585,7 +602,7 @@ int wh_NvmFlashLog_DestroyObjects(void* c, whNvmId list_count,
585602
return ret;
586603
}
587604

588-
return nfl_PartitionNewEpoch(ctx);
605+
return nfl_PartitionNewEpochOrFallback(ctx);
589606
}
590607

591608
/* Read object data */

0 commit comments

Comments
 (0)