storage/stream: Move fill value handling to flash_sync
Move the code responsible for aligning the flash write by writing fill values - from stream_flash_buffered_write to flash_sync. This avoids having to correct buf_bytes/buf_written after the write and thus simplifies error handling. This commit also fixes an issue where the write length passed to the callback in flash_sync includes the fill length. Signed-off-by: Jonathan Nilsen <Jonathan.Nilsen@nordicsemi.no>
This commit is contained in:
parent
f000695243
commit
e1891b3e9a
2 changed files with 20 additions and 19 deletions
|
@ -108,6 +108,9 @@ static int flash_sync(struct stream_flash_ctx *ctx)
|
||||||
{
|
{
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
size_t write_addr = ctx->offset + ctx->bytes_written;
|
size_t write_addr = ctx->offset + ctx->bytes_written;
|
||||||
|
size_t buf_bytes_aligned;
|
||||||
|
size_t fill_length;
|
||||||
|
uint8_t filler;
|
||||||
|
|
||||||
|
|
||||||
if (ctx->buf_bytes == 0) {
|
if (ctx->buf_bytes == 0) {
|
||||||
|
@ -125,7 +128,18 @@ static int flash_sync(struct stream_flash_ctx *ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = flash_write(ctx->fdev, write_addr, ctx->buf, ctx->buf_bytes);
|
fill_length = flash_get_write_block_size(ctx->fdev);
|
||||||
|
if (ctx->buf_bytes % fill_length) {
|
||||||
|
fill_length -= ctx->buf_bytes % fill_length;
|
||||||
|
filler = flash_get_parameters(ctx->fdev)->erase_value;
|
||||||
|
|
||||||
|
memset(ctx->buf + ctx->buf_bytes, filler, fill_length);
|
||||||
|
} else {
|
||||||
|
fill_length = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
buf_bytes_aligned = ctx->buf_bytes + fill_length;
|
||||||
|
rc = flash_write(ctx->fdev, write_addr, ctx->buf, buf_bytes_aligned);
|
||||||
|
|
||||||
if (rc != 0) {
|
if (rc != 0) {
|
||||||
LOG_ERR("flash_write error %d offset=0x%08zx", rc,
|
LOG_ERR("flash_write error %d offset=0x%08zx", rc,
|
||||||
|
@ -167,8 +181,6 @@ int stream_flash_buffered_write(struct stream_flash_ctx *ctx, const uint8_t *dat
|
||||||
int processed = 0;
|
int processed = 0;
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
int buf_empty_bytes;
|
int buf_empty_bytes;
|
||||||
size_t fill_length;
|
|
||||||
uint8_t filler;
|
|
||||||
|
|
||||||
if (!ctx) {
|
if (!ctx) {
|
||||||
return -EFAULT;
|
return -EFAULT;
|
||||||
|
@ -201,23 +213,7 @@ int stream_flash_buffered_write(struct stream_flash_ctx *ctx, const uint8_t *dat
|
||||||
}
|
}
|
||||||
|
|
||||||
if (flush && ctx->buf_bytes > 0) {
|
if (flush && ctx->buf_bytes > 0) {
|
||||||
fill_length = flash_get_write_block_size(ctx->fdev);
|
|
||||||
if (ctx->buf_bytes % fill_length) {
|
|
||||||
fill_length -= ctx->buf_bytes % fill_length;
|
|
||||||
filler = flash_get_parameters(ctx->fdev)->erase_value;
|
|
||||||
|
|
||||||
memset(ctx->buf + ctx->buf_bytes, filler, fill_length);
|
|
||||||
ctx->buf_bytes += fill_length;
|
|
||||||
} else {
|
|
||||||
fill_length = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = flash_sync(ctx);
|
rc = flash_sync(ctx);
|
||||||
if (rc == 0) {
|
|
||||||
ctx->bytes_written -= fill_length;
|
|
||||||
} else {
|
|
||||||
ctx->buf_bytes -= fill_length;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return rc;
|
return rc;
|
||||||
|
|
|
@ -196,6 +196,11 @@ static void test_stream_flash_buffered_write_unaligned(void)
|
||||||
0, stream_flash_callback);
|
0, stream_flash_callback);
|
||||||
zassert_equal(rc, 0, "expected success");
|
zassert_equal(rc, 0, "expected success");
|
||||||
|
|
||||||
|
/* Trigger verification in callback */
|
||||||
|
cb_buf = buf;
|
||||||
|
cb_len = BUF_LEN - 1;
|
||||||
|
cb_offset = FLASH_BASE + BUF_LEN;
|
||||||
|
|
||||||
/* Test unaligned data size */
|
/* Test unaligned data size */
|
||||||
rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN - 1, true);
|
rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN - 1, true);
|
||||||
zassert_equal(rc, 0, "expected success");
|
zassert_equal(rc, 0, "expected success");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue