Generated v2 prefixes
This commit is contained in:
+662
@@ -0,0 +1,662 @@
|
||||
/*
|
||||
* Emulating block device, wraps filebd and rambd while providing a bunch
|
||||
* of hooks for testing littlefs in various conditions.
|
||||
*
|
||||
* Copyright (c) 2022, The littlefs authors.
|
||||
* Copyright (c) 2017, Arm Limited. All rights reserved.
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef _POSIX_C_SOURCE
|
||||
#define _POSIX_C_SOURCE 199309L
|
||||
#endif
|
||||
|
||||
#include "bd/lfs2_emubd.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <time.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
|
||||
// access to lazily-allocated/copy-on-write blocks
|
||||
//
|
||||
// Note we can only modify a block if we have exclusive access to it (rc == 1)
|
||||
//
|
||||
|
||||
static lfs2_emubd_block_t *lfs2_emubd_incblock(lfs2_emubd_block_t *block) {
|
||||
if (block) {
|
||||
block->rc += 1;
|
||||
}
|
||||
return block;
|
||||
}
|
||||
|
||||
static void lfs2_emubd_decblock(lfs2_emubd_block_t *block) {
|
||||
if (block) {
|
||||
block->rc -= 1;
|
||||
if (block->rc == 0) {
|
||||
free(block);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static lfs2_emubd_block_t *lfs2_emubd_mutblock(
|
||||
const struct lfs2_config *cfg,
|
||||
lfs2_emubd_block_t **block) {
|
||||
lfs2_emubd_block_t *block_ = *block;
|
||||
if (block_ && block_->rc == 1) {
|
||||
// rc == 1? can modify
|
||||
return block_;
|
||||
|
||||
} else if (block_) {
|
||||
// rc > 1? need to create a copy
|
||||
lfs2_emubd_block_t *nblock = malloc(
|
||||
sizeof(lfs2_emubd_block_t) + cfg->block_size);
|
||||
if (!nblock) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy(nblock, block_,
|
||||
sizeof(lfs2_emubd_block_t) + cfg->block_size);
|
||||
nblock->rc = 1;
|
||||
|
||||
lfs2_emubd_decblock(block_);
|
||||
*block = nblock;
|
||||
return nblock;
|
||||
|
||||
} else {
|
||||
// no block? need to allocate
|
||||
lfs2_emubd_block_t *nblock = malloc(
|
||||
sizeof(lfs2_emubd_block_t) + cfg->block_size);
|
||||
if (!nblock) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
nblock->rc = 1;
|
||||
nblock->wear = 0;
|
||||
|
||||
// zero for consistency
|
||||
lfs2_emubd_t *bd = cfg->context;
|
||||
memset(nblock->data,
|
||||
(bd->cfg->erase_value != -1) ? bd->cfg->erase_value : 0,
|
||||
cfg->block_size);
|
||||
|
||||
*block = nblock;
|
||||
return nblock;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// emubd create/destroy
|
||||
|
||||
int lfs2_emubd_createcfg(const struct lfs2_config *cfg, const char *path,
|
||||
const struct lfs2_emubd_config *bdcfg) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_createcfg(%p {.context=%p, "
|
||||
".read=%p, .prog=%p, .erase=%p, .sync=%p, "
|
||||
".read_size=%"PRIu32", .prog_size=%"PRIu32", "
|
||||
".block_size=%"PRIu32", .block_count=%"PRIu32"}, "
|
||||
"\"%s\", "
|
||||
"%p {.erase_value=%"PRId32", .erase_cycles=%"PRIu32", "
|
||||
".badblock_behavior=%"PRIu8", .power_cycles=%"PRIu32", "
|
||||
".powerloss_behavior=%"PRIu8", .powerloss_cb=%p, "
|
||||
".powerloss_data=%p, .track_branches=%d})",
|
||||
(void*)cfg, cfg->context,
|
||||
(void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog,
|
||||
(void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync,
|
||||
cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count,
|
||||
path, (void*)bdcfg, bdcfg->erase_value, bdcfg->erase_cycles,
|
||||
bdcfg->badblock_behavior, bdcfg->power_cycles,
|
||||
bdcfg->powerloss_behavior, (void*)(uintptr_t)bdcfg->powerloss_cb,
|
||||
bdcfg->powerloss_data, bdcfg->track_branches);
|
||||
lfs2_emubd_t *bd = cfg->context;
|
||||
bd->cfg = bdcfg;
|
||||
|
||||
// allocate our block array, all blocks start as uninitialized
|
||||
bd->blocks = malloc(cfg->block_count * sizeof(lfs2_emubd_block_t*));
|
||||
if (!bd->blocks) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_createcfg -> %d", LFS2_ERR_NOMEM);
|
||||
return LFS2_ERR_NOMEM;
|
||||
}
|
||||
memset(bd->blocks, 0, cfg->block_count * sizeof(lfs2_emubd_block_t*));
|
||||
|
||||
// setup testing things
|
||||
bd->readed = 0;
|
||||
bd->proged = 0;
|
||||
bd->erased = 0;
|
||||
bd->power_cycles = bd->cfg->power_cycles;
|
||||
bd->disk = NULL;
|
||||
|
||||
if (bd->cfg->disk_path) {
|
||||
bd->disk = malloc(sizeof(lfs2_emubd_disk_t));
|
||||
if (!bd->disk) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_createcfg -> %d", LFS2_ERR_NOMEM);
|
||||
return LFS2_ERR_NOMEM;
|
||||
}
|
||||
bd->disk->rc = 1;
|
||||
bd->disk->scratch = NULL;
|
||||
|
||||
#ifdef _WIN32
|
||||
bd->disk->fd = open(bd->cfg->disk_path,
|
||||
O_RDWR | O_CREAT | O_BINARY, 0666);
|
||||
#else
|
||||
bd->disk->fd = open(bd->cfg->disk_path,
|
||||
O_RDWR | O_CREAT, 0666);
|
||||
#endif
|
||||
if (bd->disk->fd < 0) {
|
||||
int err = -errno;
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_create -> %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
// if we're emulating erase values, we can keep a block around in
|
||||
// memory of just the erase state to speed up emulated erases
|
||||
if (bd->cfg->erase_value != -1) {
|
||||
bd->disk->scratch = malloc(cfg->block_size);
|
||||
if (!bd->disk->scratch) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_createcfg -> %d", LFS2_ERR_NOMEM);
|
||||
return LFS2_ERR_NOMEM;
|
||||
}
|
||||
memset(bd->disk->scratch,
|
||||
bd->cfg->erase_value,
|
||||
cfg->block_size);
|
||||
|
||||
// go ahead and erase all of the disk, otherwise the file will not
|
||||
// match our internal representation
|
||||
for (size_t i = 0; i < cfg->block_count; i++) {
|
||||
ssize_t res = write(bd->disk->fd,
|
||||
bd->disk->scratch,
|
||||
cfg->block_size);
|
||||
if (res < 0) {
|
||||
int err = -errno;
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_create -> %d", err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_createcfg -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lfs2_emubd_create(const struct lfs2_config *cfg, const char *path) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_create(%p {.context=%p, "
|
||||
".read=%p, .prog=%p, .erase=%p, .sync=%p, "
|
||||
".read_size=%"PRIu32", .prog_size=%"PRIu32", "
|
||||
".block_size=%"PRIu32", .block_count=%"PRIu32"}, "
|
||||
"\"%s\")",
|
||||
(void*)cfg, cfg->context,
|
||||
(void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog,
|
||||
(void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync,
|
||||
cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count,
|
||||
path);
|
||||
static const struct lfs2_emubd_config defaults = {.erase_value=-1};
|
||||
int err = lfs2_emubd_createcfg(cfg, path, &defaults);
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_create -> %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
int lfs2_emubd_destroy(const struct lfs2_config *cfg) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_destroy(%p)", (void*)cfg);
|
||||
lfs2_emubd_t *bd = cfg->context;
|
||||
|
||||
// decrement reference counts
|
||||
for (lfs2_block_t i = 0; i < cfg->block_count; i++) {
|
||||
lfs2_emubd_decblock(bd->blocks[i]);
|
||||
}
|
||||
free(bd->blocks);
|
||||
|
||||
// clean up other resources
|
||||
if (bd->disk) {
|
||||
bd->disk->rc -= 1;
|
||||
if (bd->disk->rc == 0) {
|
||||
close(bd->disk->fd);
|
||||
free(bd->disk->scratch);
|
||||
free(bd->disk);
|
||||
}
|
||||
}
|
||||
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_destroy -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// block device API
|
||||
|
||||
int lfs2_emubd_read(const struct lfs2_config *cfg, lfs2_block_t block,
|
||||
lfs2_off_t off, void *buffer, lfs2_size_t size) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_read(%p, "
|
||||
"0x%"PRIx32", %"PRIu32", %p, %"PRIu32")",
|
||||
(void*)cfg, block, off, buffer, size);
|
||||
lfs2_emubd_t *bd = cfg->context;
|
||||
|
||||
// check if read is valid
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
LFS2_ASSERT(off % cfg->read_size == 0);
|
||||
LFS2_ASSERT(size % cfg->read_size == 0);
|
||||
LFS2_ASSERT(off+size <= cfg->block_size);
|
||||
|
||||
// get the block
|
||||
const lfs2_emubd_block_t *b = bd->blocks[block];
|
||||
if (b) {
|
||||
// block bad?
|
||||
if (bd->cfg->erase_cycles && b->wear >= bd->cfg->erase_cycles &&
|
||||
bd->cfg->badblock_behavior == LFS2_EMUBD_BADBLOCK_READERROR) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_read -> %d", LFS2_ERR_CORRUPT);
|
||||
return LFS2_ERR_CORRUPT;
|
||||
}
|
||||
|
||||
// read data
|
||||
memcpy(buffer, &b->data[off], size);
|
||||
} else {
|
||||
// zero for consistency
|
||||
memset(buffer,
|
||||
(bd->cfg->erase_value != -1) ? bd->cfg->erase_value : 0,
|
||||
size);
|
||||
}
|
||||
|
||||
// track reads
|
||||
bd->readed += size;
|
||||
if (bd->cfg->read_sleep) {
|
||||
int err = nanosleep(&(struct timespec){
|
||||
.tv_sec=bd->cfg->read_sleep/1000000000,
|
||||
.tv_nsec=bd->cfg->read_sleep%1000000000},
|
||||
NULL);
|
||||
if (err) {
|
||||
err = -errno;
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_read -> %d", err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_read -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lfs2_emubd_prog(const struct lfs2_config *cfg, lfs2_block_t block,
|
||||
lfs2_off_t off, const void *buffer, lfs2_size_t size) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_prog(%p, "
|
||||
"0x%"PRIx32", %"PRIu32", %p, %"PRIu32")",
|
||||
(void*)cfg, block, off, buffer, size);
|
||||
lfs2_emubd_t *bd = cfg->context;
|
||||
|
||||
// check if write is valid
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
LFS2_ASSERT(off % cfg->prog_size == 0);
|
||||
LFS2_ASSERT(size % cfg->prog_size == 0);
|
||||
LFS2_ASSERT(off+size <= cfg->block_size);
|
||||
|
||||
// get the block
|
||||
lfs2_emubd_block_t *b = lfs2_emubd_mutblock(cfg, &bd->blocks[block]);
|
||||
if (!b) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_prog -> %d", LFS2_ERR_NOMEM);
|
||||
return LFS2_ERR_NOMEM;
|
||||
}
|
||||
|
||||
// block bad?
|
||||
if (bd->cfg->erase_cycles && b->wear >= bd->cfg->erase_cycles) {
|
||||
if (bd->cfg->badblock_behavior ==
|
||||
LFS2_EMUBD_BADBLOCK_PROGERROR) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_prog -> %d", LFS2_ERR_CORRUPT);
|
||||
return LFS2_ERR_CORRUPT;
|
||||
} else if (bd->cfg->badblock_behavior ==
|
||||
LFS2_EMUBD_BADBLOCK_PROGNOOP ||
|
||||
bd->cfg->badblock_behavior ==
|
||||
LFS2_EMUBD_BADBLOCK_ERASENOOP) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_prog -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// were we erased properly?
|
||||
if (bd->cfg->erase_value != -1) {
|
||||
for (lfs2_off_t i = 0; i < size; i++) {
|
||||
LFS2_ASSERT(b->data[off+i] == bd->cfg->erase_value);
|
||||
}
|
||||
}
|
||||
|
||||
// prog data
|
||||
memcpy(&b->data[off], buffer, size);
|
||||
|
||||
// mirror to disk file?
|
||||
if (bd->disk) {
|
||||
off_t res1 = lseek(bd->disk->fd,
|
||||
(off_t)block*cfg->block_size + (off_t)off,
|
||||
SEEK_SET);
|
||||
if (res1 < 0) {
|
||||
int err = -errno;
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_prog -> %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
ssize_t res2 = write(bd->disk->fd, buffer, size);
|
||||
if (res2 < 0) {
|
||||
int err = -errno;
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_prog -> %d", err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
// track progs
|
||||
bd->proged += size;
|
||||
if (bd->cfg->prog_sleep) {
|
||||
int err = nanosleep(&(struct timespec){
|
||||
.tv_sec=bd->cfg->prog_sleep/1000000000,
|
||||
.tv_nsec=bd->cfg->prog_sleep%1000000000},
|
||||
NULL);
|
||||
if (err) {
|
||||
err = -errno;
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_prog -> %d", err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
// lose power?
|
||||
if (bd->power_cycles > 0) {
|
||||
bd->power_cycles -= 1;
|
||||
if (bd->power_cycles == 0) {
|
||||
// simulate power loss
|
||||
bd->cfg->powerloss_cb(bd->cfg->powerloss_data);
|
||||
}
|
||||
}
|
||||
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_prog -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lfs2_emubd_erase(const struct lfs2_config *cfg, lfs2_block_t block) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_erase(%p, 0x%"PRIx32" (%"PRIu32"))",
|
||||
(void*)cfg, block, cfg->block_size);
|
||||
lfs2_emubd_t *bd = cfg->context;
|
||||
|
||||
// check if erase is valid
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
|
||||
// get the block
|
||||
lfs2_emubd_block_t *b = lfs2_emubd_mutblock(cfg, &bd->blocks[block]);
|
||||
if (!b) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_prog -> %d", LFS2_ERR_NOMEM);
|
||||
return LFS2_ERR_NOMEM;
|
||||
}
|
||||
|
||||
// block bad?
|
||||
if (bd->cfg->erase_cycles) {
|
||||
if (b->wear >= bd->cfg->erase_cycles) {
|
||||
if (bd->cfg->badblock_behavior ==
|
||||
LFS2_EMUBD_BADBLOCK_ERASEERROR) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_erase -> %d", LFS2_ERR_CORRUPT);
|
||||
return LFS2_ERR_CORRUPT;
|
||||
} else if (bd->cfg->badblock_behavior ==
|
||||
LFS2_EMUBD_BADBLOCK_ERASENOOP) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_erase -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
// mark wear
|
||||
b->wear += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// emulate an erase value?
|
||||
if (bd->cfg->erase_value != -1) {
|
||||
memset(b->data, bd->cfg->erase_value, cfg->block_size);
|
||||
|
||||
// mirror to disk file?
|
||||
if (bd->disk) {
|
||||
off_t res1 = lseek(bd->disk->fd,
|
||||
(off_t)block*cfg->block_size,
|
||||
SEEK_SET);
|
||||
if (res1 < 0) {
|
||||
int err = -errno;
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_erase -> %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
ssize_t res2 = write(bd->disk->fd,
|
||||
bd->disk->scratch,
|
||||
cfg->block_size);
|
||||
if (res2 < 0) {
|
||||
int err = -errno;
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_erase -> %d", err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// track erases
|
||||
bd->erased += cfg->block_size;
|
||||
if (bd->cfg->erase_sleep) {
|
||||
int err = nanosleep(&(struct timespec){
|
||||
.tv_sec=bd->cfg->erase_sleep/1000000000,
|
||||
.tv_nsec=bd->cfg->erase_sleep%1000000000},
|
||||
NULL);
|
||||
if (err) {
|
||||
err = -errno;
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_erase -> %d", err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
// lose power?
|
||||
if (bd->power_cycles > 0) {
|
||||
bd->power_cycles -= 1;
|
||||
if (bd->power_cycles == 0) {
|
||||
// simulate power loss
|
||||
bd->cfg->powerloss_cb(bd->cfg->powerloss_data);
|
||||
}
|
||||
}
|
||||
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_erase -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lfs2_emubd_sync(const struct lfs2_config *cfg) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_sync(%p)", (void*)cfg);
|
||||
|
||||
// do nothing
|
||||
(void)cfg;
|
||||
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_sync -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// Additional extended API for driving test features ///
|
||||
|
||||
static int lfs2_emubd_rawcrc(const struct lfs2_config *cfg,
|
||||
lfs2_block_t block, uint32_t *crc) {
|
||||
lfs2_emubd_t *bd = cfg->context;
|
||||
|
||||
// check if crc is valid
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
|
||||
// crc the block
|
||||
uint32_t crc_ = 0xffffffff;
|
||||
const lfs2_emubd_block_t *b = bd->blocks[block];
|
||||
if (b) {
|
||||
crc_ = lfs2_crc(crc_, b->data, cfg->block_size);
|
||||
} else {
|
||||
uint8_t erase_value = (bd->cfg->erase_value != -1)
|
||||
? bd->cfg->erase_value
|
||||
: 0;
|
||||
for (lfs2_size_t i = 0; i < cfg->block_size; i++) {
|
||||
crc_ = lfs2_crc(crc_, &erase_value, 1);
|
||||
}
|
||||
}
|
||||
*crc = 0xffffffff ^ crc_;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lfs2_emubd_crc(const struct lfs2_config *cfg,
|
||||
lfs2_block_t block, uint32_t *crc) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_crc(%p, %"PRIu32", %p)",
|
||||
(void*)cfg, block, crc);
|
||||
int err = lfs2_emubd_rawcrc(cfg, block, crc);
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_crc -> %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
int lfs2_emubd_bdcrc(const struct lfs2_config *cfg, uint32_t *crc) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_bdcrc(%p, %p)", (void*)cfg, crc);
|
||||
|
||||
uint32_t crc_ = 0xffffffff;
|
||||
for (lfs2_block_t i = 0; i < cfg->block_count; i++) {
|
||||
uint32_t i_crc;
|
||||
int err = lfs2_emubd_rawcrc(cfg, i, &i_crc);
|
||||
if (err) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_bdcrc -> %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
crc_ = lfs2_crc(crc_, &i_crc, sizeof(uint32_t));
|
||||
}
|
||||
*crc = 0xffffffff ^ crc_;
|
||||
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_bdcrc -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
lfs2_emubd_sio_t lfs2_emubd_readed(const struct lfs2_config *cfg) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_readed(%p)", (void*)cfg);
|
||||
lfs2_emubd_t *bd = cfg->context;
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_readed -> %"PRIu64, bd->readed);
|
||||
return bd->readed;
|
||||
}
|
||||
|
||||
lfs2_emubd_sio_t lfs2_emubd_proged(const struct lfs2_config *cfg) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_proged(%p)", (void*)cfg);
|
||||
lfs2_emubd_t *bd = cfg->context;
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_proged -> %"PRIu64, bd->proged);
|
||||
return bd->proged;
|
||||
}
|
||||
|
||||
lfs2_emubd_sio_t lfs2_emubd_erased(const struct lfs2_config *cfg) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_erased(%p)", (void*)cfg);
|
||||
lfs2_emubd_t *bd = cfg->context;
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_erased -> %"PRIu64, bd->erased);
|
||||
return bd->erased;
|
||||
}
|
||||
|
||||
int lfs2_emubd_setreaded(const struct lfs2_config *cfg, lfs2_emubd_io_t readed) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_setreaded(%p, %"PRIu64")", (void*)cfg, readed);
|
||||
lfs2_emubd_t *bd = cfg->context;
|
||||
bd->readed = readed;
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_setreaded -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lfs2_emubd_setproged(const struct lfs2_config *cfg, lfs2_emubd_io_t proged) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_setproged(%p, %"PRIu64")", (void*)cfg, proged);
|
||||
lfs2_emubd_t *bd = cfg->context;
|
||||
bd->proged = proged;
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_setproged -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lfs2_emubd_seterased(const struct lfs2_config *cfg, lfs2_emubd_io_t erased) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_seterased(%p, %"PRIu64")", (void*)cfg, erased);
|
||||
lfs2_emubd_t *bd = cfg->context;
|
||||
bd->erased = erased;
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_seterased -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
lfs2_emubd_swear_t lfs2_emubd_wear(const struct lfs2_config *cfg,
|
||||
lfs2_block_t block) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_wear(%p, %"PRIu32")", (void*)cfg, block);
|
||||
lfs2_emubd_t *bd = cfg->context;
|
||||
|
||||
// check if block is valid
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
|
||||
// get the wear
|
||||
lfs2_emubd_wear_t wear;
|
||||
const lfs2_emubd_block_t *b = bd->blocks[block];
|
||||
if (b) {
|
||||
wear = b->wear;
|
||||
} else {
|
||||
wear = 0;
|
||||
}
|
||||
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_wear -> %"PRIi32, wear);
|
||||
return wear;
|
||||
}
|
||||
|
||||
int lfs2_emubd_setwear(const struct lfs2_config *cfg,
|
||||
lfs2_block_t block, lfs2_emubd_wear_t wear) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_setwear(%p, %"PRIu32", %"PRIi32")",
|
||||
(void*)cfg, block, wear);
|
||||
lfs2_emubd_t *bd = cfg->context;
|
||||
|
||||
// check if block is valid
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
|
||||
// set the wear
|
||||
lfs2_emubd_block_t *b = lfs2_emubd_mutblock(cfg, &bd->blocks[block]);
|
||||
if (!b) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_setwear -> %d", LFS2_ERR_NOMEM);
|
||||
return LFS2_ERR_NOMEM;
|
||||
}
|
||||
b->wear = wear;
|
||||
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_setwear -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
lfs2_emubd_spowercycles_t lfs2_emubd_powercycles(
|
||||
const struct lfs2_config *cfg) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_powercycles(%p)", (void*)cfg);
|
||||
lfs2_emubd_t *bd = cfg->context;
|
||||
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_powercycles -> %"PRIi32, bd->power_cycles);
|
||||
return bd->power_cycles;
|
||||
}
|
||||
|
||||
int lfs2_emubd_setpowercycles(const struct lfs2_config *cfg,
|
||||
lfs2_emubd_powercycles_t power_cycles) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_setpowercycles(%p, %"PRIi32")",
|
||||
(void*)cfg, power_cycles);
|
||||
lfs2_emubd_t *bd = cfg->context;
|
||||
|
||||
bd->power_cycles = power_cycles;
|
||||
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_powercycles -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lfs2_emubd_copy(const struct lfs2_config *cfg, lfs2_emubd_t *copy) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_copy(%p, %p)", (void*)cfg, (void*)copy);
|
||||
lfs2_emubd_t *bd = cfg->context;
|
||||
|
||||
// lazily copy over our block array
|
||||
copy->blocks = malloc(cfg->block_count * sizeof(lfs2_emubd_block_t*));
|
||||
if (!copy->blocks) {
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_copy -> %d", LFS2_ERR_NOMEM);
|
||||
return LFS2_ERR_NOMEM;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < cfg->block_count; i++) {
|
||||
copy->blocks[i] = lfs2_emubd_incblock(bd->blocks[i]);
|
||||
}
|
||||
|
||||
// other state
|
||||
copy->readed = bd->readed;
|
||||
copy->proged = bd->proged;
|
||||
copy->erased = bd->erased;
|
||||
copy->power_cycles = bd->power_cycles;
|
||||
copy->disk = bd->disk;
|
||||
if (copy->disk) {
|
||||
copy->disk->rc += 1;
|
||||
}
|
||||
copy->cfg = bd->cfg;
|
||||
|
||||
LFS2_EMUBD_TRACE("lfs2_emubd_copy -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
+233
@@ -0,0 +1,233 @@
|
||||
/*
|
||||
* Emulating block device, wraps filebd and rambd while providing a bunch
|
||||
* of hooks for testing littlefs in various conditions.
|
||||
*
|
||||
* Copyright (c) 2022, The littlefs authors.
|
||||
* Copyright (c) 2017, Arm Limited. All rights reserved.
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
#ifndef LFS2_EMUBD_H
|
||||
#define LFS2_EMUBD_H
|
||||
|
||||
#include "lfs2.h"
|
||||
#include "lfs2_util.h"
|
||||
#include "bd/lfs2_rambd.h"
|
||||
#include "bd/lfs2_filebd.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
|
||||
// Block device specific tracing
|
||||
#ifndef LFS2_EMUBD_TRACE
|
||||
#ifdef LFS2_EMUBD_YES_TRACE
|
||||
#define LFS2_EMUBD_TRACE(...) LFS2_TRACE(__VA_ARGS__)
|
||||
#else
|
||||
#define LFS2_EMUBD_TRACE(...)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Mode determining how "bad-blocks" behave during testing. This simulates
|
||||
// some real-world circumstances such as progs not sticking (prog-noop),
|
||||
// a readonly disk (erase-noop), and ECC failures (read-error).
|
||||
//
|
||||
// Not that read-noop is not allowed. Read _must_ return a consistent (but
|
||||
// may be arbitrary) value on every read.
|
||||
typedef enum lfs2_emubd_badblock_behavior {
|
||||
LFS2_EMUBD_BADBLOCK_PROGERROR,
|
||||
LFS2_EMUBD_BADBLOCK_ERASEERROR,
|
||||
LFS2_EMUBD_BADBLOCK_READERROR,
|
||||
LFS2_EMUBD_BADBLOCK_PROGNOOP,
|
||||
LFS2_EMUBD_BADBLOCK_ERASENOOP,
|
||||
} lfs2_emubd_badblock_behavior_t;
|
||||
|
||||
// Mode determining how power-loss behaves during testing. For now this
|
||||
// only supports a noop behavior, leaving the data on-disk untouched.
|
||||
typedef enum lfs2_emubd_powerloss_behavior {
|
||||
LFS2_EMUBD_POWERLOSS_NOOP,
|
||||
} lfs2_emubd_powerloss_behavior_t;
|
||||
|
||||
// Type for measuring read/program/erase operations
|
||||
typedef uint64_t lfs2_emubd_io_t;
|
||||
typedef int64_t lfs2_emubd_sio_t;
|
||||
|
||||
// Type for measuring wear
|
||||
typedef uint32_t lfs2_emubd_wear_t;
|
||||
typedef int32_t lfs2_emubd_swear_t;
|
||||
|
||||
// Type for tracking power-cycles
|
||||
typedef uint32_t lfs2_emubd_powercycles_t;
|
||||
typedef int32_t lfs2_emubd_spowercycles_t;
|
||||
|
||||
// Type for delays in nanoseconds
|
||||
typedef uint64_t lfs2_emubd_sleep_t;
|
||||
typedef int64_t lfs2_emubd_ssleep_t;
|
||||
|
||||
// emubd config, this is required for testing
|
||||
struct lfs2_emubd_config {
|
||||
// 8-bit erase value to use for simulating erases. -1 does not simulate
|
||||
// erases, which can speed up testing by avoiding the extra block-device
|
||||
// operations to store the erase value.
|
||||
int32_t erase_value;
|
||||
|
||||
// Number of erase cycles before a block becomes "bad". The exact behavior
|
||||
// of bad blocks is controlled by badblock_behavior.
|
||||
uint32_t erase_cycles;
|
||||
|
||||
// The mode determining how bad-blocks fail
|
||||
lfs2_emubd_badblock_behavior_t badblock_behavior;
|
||||
|
||||
// Number of write operations (erase/prog) before triggering a power-loss.
|
||||
// power_cycles=0 disables this. The exact behavior of power-loss is
|
||||
// controlled by a combination of powerloss_behavior and powerloss_cb.
|
||||
lfs2_emubd_powercycles_t power_cycles;
|
||||
|
||||
// The mode determining how power-loss affects disk
|
||||
lfs2_emubd_powerloss_behavior_t powerloss_behavior;
|
||||
|
||||
// Function to call to emulate power-loss. The exact behavior of power-loss
|
||||
// is up to the runner to provide.
|
||||
void (*powerloss_cb)(void*);
|
||||
|
||||
// Data for power-loss callback
|
||||
void *powerloss_data;
|
||||
|
||||
// True to track when power-loss could have occured. Note this involves
|
||||
// heavy memory usage!
|
||||
bool track_branches;
|
||||
|
||||
// Path to file to use as a mirror of the disk. This provides a way to view
|
||||
// the current state of the block device.
|
||||
const char *disk_path;
|
||||
|
||||
// Artificial delay in nanoseconds, there is no purpose for this other
|
||||
// than slowing down the simulation.
|
||||
lfs2_emubd_sleep_t read_sleep;
|
||||
|
||||
// Artificial delay in nanoseconds, there is no purpose for this other
|
||||
// than slowing down the simulation.
|
||||
lfs2_emubd_sleep_t prog_sleep;
|
||||
|
||||
// Artificial delay in nanoseconds, there is no purpose for this other
|
||||
// than slowing down the simulation.
|
||||
lfs2_emubd_sleep_t erase_sleep;
|
||||
};
|
||||
|
||||
// A reference counted block
|
||||
typedef struct lfs2_emubd_block {
|
||||
uint32_t rc;
|
||||
lfs2_emubd_wear_t wear;
|
||||
|
||||
uint8_t data[];
|
||||
} lfs2_emubd_block_t;
|
||||
|
||||
// Disk mirror
|
||||
typedef struct lfs2_emubd_disk {
|
||||
uint32_t rc;
|
||||
int fd;
|
||||
uint8_t *scratch;
|
||||
} lfs2_emubd_disk_t;
|
||||
|
||||
// emubd state
|
||||
typedef struct lfs2_emubd {
|
||||
// array of copy-on-write blocks
|
||||
lfs2_emubd_block_t **blocks;
|
||||
|
||||
// some other test state
|
||||
lfs2_emubd_io_t readed;
|
||||
lfs2_emubd_io_t proged;
|
||||
lfs2_emubd_io_t erased;
|
||||
lfs2_emubd_powercycles_t power_cycles;
|
||||
lfs2_emubd_disk_t *disk;
|
||||
|
||||
const struct lfs2_emubd_config *cfg;
|
||||
} lfs2_emubd_t;
|
||||
|
||||
|
||||
/// Block device API ///
|
||||
|
||||
// Create an emulating block device using the geometry in lfs2_config
|
||||
//
|
||||
// Note that filebd is used if a path is provided, if path is NULL
|
||||
// emubd will use rambd which can be much faster.
|
||||
int lfs2_emubd_create(const struct lfs2_config *cfg, const char *path);
|
||||
int lfs2_emubd_createcfg(const struct lfs2_config *cfg, const char *path,
|
||||
const struct lfs2_emubd_config *bdcfg);
|
||||
|
||||
// Clean up memory associated with block device
|
||||
int lfs2_emubd_destroy(const struct lfs2_config *cfg);
|
||||
|
||||
// Read a block
|
||||
int lfs2_emubd_read(const struct lfs2_config *cfg, lfs2_block_t block,
|
||||
lfs2_off_t off, void *buffer, lfs2_size_t size);
|
||||
|
||||
// Program a block
|
||||
//
|
||||
// The block must have previously been erased.
|
||||
int lfs2_emubd_prog(const struct lfs2_config *cfg, lfs2_block_t block,
|
||||
lfs2_off_t off, const void *buffer, lfs2_size_t size);
|
||||
|
||||
// Erase a block
|
||||
//
|
||||
// A block must be erased before being programmed. The
|
||||
// state of an erased block is undefined.
|
||||
int lfs2_emubd_erase(const struct lfs2_config *cfg, lfs2_block_t block);
|
||||
|
||||
// Sync the block device
|
||||
int lfs2_emubd_sync(const struct lfs2_config *cfg);
|
||||
|
||||
|
||||
/// Additional extended API for driving test features ///
|
||||
|
||||
// A CRC of a block for debugging purposes
|
||||
int lfs2_emubd_crc(const struct lfs2_config *cfg,
|
||||
lfs2_block_t block, uint32_t *crc);
|
||||
|
||||
// A CRC of the entire block device for debugging purposes
|
||||
int lfs2_emubd_bdcrc(const struct lfs2_config *cfg, uint32_t *crc);
|
||||
|
||||
// Get total amount of bytes read
|
||||
lfs2_emubd_sio_t lfs2_emubd_readed(const struct lfs2_config *cfg);
|
||||
|
||||
// Get total amount of bytes programmed
|
||||
lfs2_emubd_sio_t lfs2_emubd_proged(const struct lfs2_config *cfg);
|
||||
|
||||
// Get total amount of bytes erased
|
||||
lfs2_emubd_sio_t lfs2_emubd_erased(const struct lfs2_config *cfg);
|
||||
|
||||
// Manually set amount of bytes read
|
||||
int lfs2_emubd_setreaded(const struct lfs2_config *cfg, lfs2_emubd_io_t readed);
|
||||
|
||||
// Manually set amount of bytes programmed
|
||||
int lfs2_emubd_setproged(const struct lfs2_config *cfg, lfs2_emubd_io_t proged);
|
||||
|
||||
// Manually set amount of bytes erased
|
||||
int lfs2_emubd_seterased(const struct lfs2_config *cfg, lfs2_emubd_io_t erased);
|
||||
|
||||
// Get simulated wear on a given block
|
||||
lfs2_emubd_swear_t lfs2_emubd_wear(const struct lfs2_config *cfg,
|
||||
lfs2_block_t block);
|
||||
|
||||
// Manually set simulated wear on a given block
|
||||
int lfs2_emubd_setwear(const struct lfs2_config *cfg,
|
||||
lfs2_block_t block, lfs2_emubd_wear_t wear);
|
||||
|
||||
// Get the remaining power-cycles
|
||||
lfs2_emubd_spowercycles_t lfs2_emubd_powercycles(
|
||||
const struct lfs2_config *cfg);
|
||||
|
||||
// Manually set the remaining power-cycles
|
||||
int lfs2_emubd_setpowercycles(const struct lfs2_config *cfg,
|
||||
lfs2_emubd_powercycles_t power_cycles);
|
||||
|
||||
// Create a copy-on-write copy of the state of this block device
|
||||
int lfs2_emubd_copy(const struct lfs2_config *cfg, lfs2_emubd_t *copy);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+30
-87
@@ -15,39 +15,6 @@
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
int lfs2_filebd_createcfg(const struct lfs2_config *cfg, const char *path,
|
||||
const struct lfs2_filebd_config *bdcfg) {
|
||||
LFS2_FILEBD_TRACE("lfs2_filebd_createcfg(%p {.context=%p, "
|
||||
".read=%p, .prog=%p, .erase=%p, .sync=%p, "
|
||||
".read_size=%"PRIu32", .prog_size=%"PRIu32", "
|
||||
".block_size=%"PRIu32", .block_count=%"PRIu32"}, "
|
||||
"\"%s\", "
|
||||
"%p {.erase_value=%"PRId32"})",
|
||||
(void*)cfg, cfg->context,
|
||||
(void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog,
|
||||
(void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync,
|
||||
cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count,
|
||||
path, (void*)bdcfg, bdcfg->erase_value);
|
||||
lfs2_filebd_t *bd = cfg->context;
|
||||
bd->cfg = bdcfg;
|
||||
|
||||
// open file
|
||||
#ifdef _WIN32
|
||||
bd->fd = open(path, O_RDWR | O_CREAT | O_BINARY, 0666);
|
||||
#else
|
||||
bd->fd = open(path, O_RDWR | O_CREAT, 0666);
|
||||
#endif
|
||||
|
||||
if (bd->fd < 0) {
|
||||
int err = -errno;
|
||||
LFS2_FILEBD_TRACE("lfs2_filebd_createcfg -> %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
LFS2_FILEBD_TRACE("lfs2_filebd_createcfg -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lfs2_filebd_create(const struct lfs2_config *cfg, const char *path) {
|
||||
LFS2_FILEBD_TRACE("lfs2_filebd_create(%p {.context=%p, "
|
||||
".read=%p, .prog=%p, .erase=%p, .sync=%p, "
|
||||
@@ -58,11 +25,24 @@ int lfs2_filebd_create(const struct lfs2_config *cfg, const char *path) {
|
||||
(void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog,
|
||||
(void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync,
|
||||
cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count,
|
||||
path);
|
||||
static const struct lfs2_filebd_config defaults = {.erase_value=-1};
|
||||
int err = lfs2_filebd_createcfg(cfg, path, &defaults);
|
||||
LFS2_FILEBD_TRACE("lfs2_filebd_create -> %d", err);
|
||||
return err;
|
||||
path, (void*)bdcfg, bdcfg->erase_value);
|
||||
lfs2_filebd_t *bd = cfg->context;
|
||||
|
||||
// open file
|
||||
#ifdef _WIN32
|
||||
bd->fd = open(path, O_RDWR | O_CREAT | O_BINARY, 0666);
|
||||
#else
|
||||
bd->fd = open(path, O_RDWR | O_CREAT, 0666);
|
||||
#endif
|
||||
|
||||
if (bd->fd < 0) {
|
||||
int err = -errno;
|
||||
LFS2_FILEBD_TRACE("lfs2_filebd_create -> %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
LFS2_FILEBD_TRACE("lfs2_filebd_create -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lfs2_filebd_destroy(const struct lfs2_config *cfg) {
|
||||
@@ -86,14 +66,13 @@ int lfs2_filebd_read(const struct lfs2_config *cfg, lfs2_block_t block,
|
||||
lfs2_filebd_t *bd = cfg->context;
|
||||
|
||||
// check if read is valid
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
LFS2_ASSERT(off % cfg->read_size == 0);
|
||||
LFS2_ASSERT(size % cfg->read_size == 0);
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
LFS2_ASSERT(off+size <= cfg->block_size);
|
||||
|
||||
// zero for reproducibility (in case file is truncated)
|
||||
if (bd->cfg->erase_value != -1) {
|
||||
memset(buffer, bd->cfg->erase_value, size);
|
||||
}
|
||||
memset(buffer, 0, size);
|
||||
|
||||
// read
|
||||
off_t res1 = lseek(bd->fd,
|
||||
@@ -117,37 +96,16 @@ int lfs2_filebd_read(const struct lfs2_config *cfg, lfs2_block_t block,
|
||||
|
||||
int lfs2_filebd_prog(const struct lfs2_config *cfg, lfs2_block_t block,
|
||||
lfs2_off_t off, const void *buffer, lfs2_size_t size) {
|
||||
LFS2_FILEBD_TRACE("lfs2_filebd_prog(%p, 0x%"PRIx32", %"PRIu32", %p, %"PRIu32")",
|
||||
LFS2_FILEBD_TRACE("lfs2_filebd_prog(%p, "
|
||||
"0x%"PRIx32", %"PRIu32", %p, %"PRIu32")",
|
||||
(void*)cfg, block, off, buffer, size);
|
||||
lfs2_filebd_t *bd = cfg->context;
|
||||
|
||||
// check if write is valid
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
LFS2_ASSERT(off % cfg->prog_size == 0);
|
||||
LFS2_ASSERT(size % cfg->prog_size == 0);
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
|
||||
// check that data was erased? only needed for testing
|
||||
if (bd->cfg->erase_value != -1) {
|
||||
off_t res1 = lseek(bd->fd,
|
||||
(off_t)block*cfg->block_size + (off_t)off, SEEK_SET);
|
||||
if (res1 < 0) {
|
||||
int err = -errno;
|
||||
LFS2_FILEBD_TRACE("lfs2_filebd_prog -> %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
for (lfs2_off_t i = 0; i < size; i++) {
|
||||
uint8_t c;
|
||||
ssize_t res2 = read(bd->fd, &c, 1);
|
||||
if (res2 < 0) {
|
||||
int err = -errno;
|
||||
LFS2_FILEBD_TRACE("lfs2_filebd_prog -> %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
LFS2_ASSERT(c == bd->cfg->erase_value);
|
||||
}
|
||||
}
|
||||
LFS2_ASSERT(off+size <= cfg->block_size);
|
||||
|
||||
// program data
|
||||
off_t res1 = lseek(bd->fd,
|
||||
@@ -170,30 +128,14 @@ int lfs2_filebd_prog(const struct lfs2_config *cfg, lfs2_block_t block,
|
||||
}
|
||||
|
||||
int lfs2_filebd_erase(const struct lfs2_config *cfg, lfs2_block_t block) {
|
||||
LFS2_FILEBD_TRACE("lfs2_filebd_erase(%p, 0x%"PRIx32")", (void*)cfg, block);
|
||||
lfs2_filebd_t *bd = cfg->context;
|
||||
LFS2_FILEBD_TRACE("lfs2_filebd_erase(%p, 0x%"PRIx32" (%"PRIu32"))",
|
||||
(void*)cfg, block, cfg->block_size);
|
||||
|
||||
// check if erase is valid
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
|
||||
// erase, only needed for testing
|
||||
if (bd->cfg->erase_value != -1) {
|
||||
off_t res1 = lseek(bd->fd, (off_t)block*cfg->block_size, SEEK_SET);
|
||||
if (res1 < 0) {
|
||||
int err = -errno;
|
||||
LFS2_FILEBD_TRACE("lfs2_filebd_erase -> %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
for (lfs2_off_t i = 0; i < cfg->block_size; i++) {
|
||||
ssize_t res2 = write(bd->fd, &(uint8_t){bd->cfg->erase_value}, 1);
|
||||
if (res2 < 0) {
|
||||
int err = -errno;
|
||||
LFS2_FILEBD_TRACE("lfs2_filebd_erase -> %d", err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
}
|
||||
// erase is a noop
|
||||
(void)block;
|
||||
|
||||
LFS2_FILEBD_TRACE("lfs2_filebd_erase -> %d", 0);
|
||||
return 0;
|
||||
@@ -201,6 +143,7 @@ int lfs2_filebd_erase(const struct lfs2_config *cfg, lfs2_block_t block) {
|
||||
|
||||
int lfs2_filebd_sync(const struct lfs2_config *cfg) {
|
||||
LFS2_FILEBD_TRACE("lfs2_filebd_sync(%p)", (void*)cfg);
|
||||
|
||||
// file sync
|
||||
lfs2_filebd_t *bd = cfg->context;
|
||||
#ifdef _WIN32
|
||||
|
||||
+2
-11
@@ -18,31 +18,22 @@ extern "C"
|
||||
|
||||
|
||||
// Block device specific tracing
|
||||
#ifndef LFS2_FILEBD_TRACE
|
||||
#ifdef LFS2_FILEBD_YES_TRACE
|
||||
#define LFS2_FILEBD_TRACE(...) LFS2_TRACE(__VA_ARGS__)
|
||||
#else
|
||||
#define LFS2_FILEBD_TRACE(...)
|
||||
#endif
|
||||
|
||||
// filebd config (optional)
|
||||
struct lfs2_filebd_config {
|
||||
// 8-bit erase value to use for simulating erases. -1 does not simulate
|
||||
// erases, which can speed up testing by avoiding all the extra block-device
|
||||
// operations to store the erase value.
|
||||
int32_t erase_value;
|
||||
};
|
||||
#endif
|
||||
|
||||
// filebd state
|
||||
typedef struct lfs2_filebd {
|
||||
int fd;
|
||||
const struct lfs2_filebd_config *cfg;
|
||||
} lfs2_filebd_t;
|
||||
|
||||
|
||||
// Create a file block device using the geometry in lfs2_config
|
||||
int lfs2_filebd_create(const struct lfs2_config *cfg, const char *path);
|
||||
int lfs2_filebd_createcfg(const struct lfs2_config *cfg, const char *path,
|
||||
const struct lfs2_filebd_config *bdcfg);
|
||||
|
||||
// Clean up memory associated with block device
|
||||
int lfs2_filebd_destroy(const struct lfs2_config *cfg);
|
||||
|
||||
+16
-28
@@ -13,12 +13,12 @@ int lfs2_rambd_createcfg(const struct lfs2_config *cfg,
|
||||
".read=%p, .prog=%p, .erase=%p, .sync=%p, "
|
||||
".read_size=%"PRIu32", .prog_size=%"PRIu32", "
|
||||
".block_size=%"PRIu32", .block_count=%"PRIu32"}, "
|
||||
"%p {.erase_value=%"PRId32", .buffer=%p})",
|
||||
"%p {.buffer=%p})",
|
||||
(void*)cfg, cfg->context,
|
||||
(void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog,
|
||||
(void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync,
|
||||
cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count,
|
||||
(void*)bdcfg, bdcfg->erase_value, bdcfg->buffer);
|
||||
(void*)bdcfg, bdcfg->buffer);
|
||||
lfs2_rambd_t *bd = cfg->context;
|
||||
bd->cfg = bdcfg;
|
||||
|
||||
@@ -33,13 +33,8 @@ int lfs2_rambd_createcfg(const struct lfs2_config *cfg,
|
||||
}
|
||||
}
|
||||
|
||||
// zero for reproducibility?
|
||||
if (bd->cfg->erase_value != -1) {
|
||||
memset(bd->buffer, bd->cfg->erase_value,
|
||||
cfg->block_size * cfg->block_count);
|
||||
} else {
|
||||
memset(bd->buffer, 0, cfg->block_size * cfg->block_count);
|
||||
}
|
||||
// zero for reproducibility
|
||||
memset(bd->buffer, 0, cfg->block_size * cfg->block_count);
|
||||
|
||||
LFS2_RAMBD_TRACE("lfs2_rambd_createcfg -> %d", 0);
|
||||
return 0;
|
||||
@@ -54,7 +49,7 @@ int lfs2_rambd_create(const struct lfs2_config *cfg) {
|
||||
(void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog,
|
||||
(void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync,
|
||||
cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count);
|
||||
static const struct lfs2_rambd_config defaults = {.erase_value=-1};
|
||||
static const struct lfs2_rambd_config defaults = {0};
|
||||
int err = lfs2_rambd_createcfg(cfg, &defaults);
|
||||
LFS2_RAMBD_TRACE("lfs2_rambd_create -> %d", err);
|
||||
return err;
|
||||
@@ -79,9 +74,10 @@ int lfs2_rambd_read(const struct lfs2_config *cfg, lfs2_block_t block,
|
||||
lfs2_rambd_t *bd = cfg->context;
|
||||
|
||||
// check if read is valid
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
LFS2_ASSERT(off % cfg->read_size == 0);
|
||||
LFS2_ASSERT(size % cfg->read_size == 0);
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
LFS2_ASSERT(off+size <= cfg->block_size);
|
||||
|
||||
// read data
|
||||
memcpy(buffer, &bd->buffer[block*cfg->block_size + off], size);
|
||||
@@ -98,17 +94,10 @@ int lfs2_rambd_prog(const struct lfs2_config *cfg, lfs2_block_t block,
|
||||
lfs2_rambd_t *bd = cfg->context;
|
||||
|
||||
// check if write is valid
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
LFS2_ASSERT(off % cfg->prog_size == 0);
|
||||
LFS2_ASSERT(size % cfg->prog_size == 0);
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
|
||||
// check that data was erased? only needed for testing
|
||||
if (bd->cfg->erase_value != -1) {
|
||||
for (lfs2_off_t i = 0; i < size; i++) {
|
||||
LFS2_ASSERT(bd->buffer[block*cfg->block_size + off + i] ==
|
||||
bd->cfg->erase_value);
|
||||
}
|
||||
}
|
||||
LFS2_ASSERT(off+size <= cfg->block_size);
|
||||
|
||||
// program data
|
||||
memcpy(&bd->buffer[block*cfg->block_size + off], buffer, size);
|
||||
@@ -118,17 +107,14 @@ int lfs2_rambd_prog(const struct lfs2_config *cfg, lfs2_block_t block,
|
||||
}
|
||||
|
||||
int lfs2_rambd_erase(const struct lfs2_config *cfg, lfs2_block_t block) {
|
||||
LFS2_RAMBD_TRACE("lfs2_rambd_erase(%p, 0x%"PRIx32")", (void*)cfg, block);
|
||||
lfs2_rambd_t *bd = cfg->context;
|
||||
LFS2_RAMBD_TRACE("lfs2_rambd_erase(%p, 0x%"PRIx32" (%"PRIu32"))",
|
||||
(void*)cfg, block, cfg->block_size);
|
||||
|
||||
// check if erase is valid
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
|
||||
// erase, only needed for testing
|
||||
if (bd->cfg->erase_value != -1) {
|
||||
memset(&bd->buffer[block*cfg->block_size],
|
||||
bd->cfg->erase_value, cfg->block_size);
|
||||
}
|
||||
// erase is a noop
|
||||
(void)block;
|
||||
|
||||
LFS2_RAMBD_TRACE("lfs2_rambd_erase -> %d", 0);
|
||||
return 0;
|
||||
@@ -136,8 +122,10 @@ int lfs2_rambd_erase(const struct lfs2_config *cfg, lfs2_block_t block) {
|
||||
|
||||
int lfs2_rambd_sync(const struct lfs2_config *cfg) {
|
||||
LFS2_RAMBD_TRACE("lfs2_rambd_sync(%p)", (void*)cfg);
|
||||
// sync does nothing because we aren't backed by anything real
|
||||
|
||||
// sync is a noop
|
||||
(void)cfg;
|
||||
|
||||
LFS2_RAMBD_TRACE("lfs2_rambd_sync -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
+2
-4
@@ -18,18 +18,16 @@ extern "C"
|
||||
|
||||
|
||||
// Block device specific tracing
|
||||
#ifndef LFS2_RAMBD_TRACE
|
||||
#ifdef LFS2_RAMBD_YES_TRACE
|
||||
#define LFS2_RAMBD_TRACE(...) LFS2_TRACE(__VA_ARGS__)
|
||||
#else
|
||||
#define LFS2_RAMBD_TRACE(...)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// rambd config (optional)
|
||||
struct lfs2_rambd_config {
|
||||
// 8-bit erase value to simulate erasing with. -1 indicates no erase
|
||||
// occurs, which is still a valid block device
|
||||
int32_t erase_value;
|
||||
|
||||
// Optional statically allocated buffer for the block device.
|
||||
void *buffer;
|
||||
};
|
||||
|
||||
@@ -1,303 +0,0 @@
|
||||
/*
|
||||
* Testing block device, wraps filebd and rambd while providing a bunch
|
||||
* of hooks for testing littlefs in various conditions.
|
||||
*
|
||||
* Copyright (c) 2022, The littlefs authors.
|
||||
* Copyright (c) 2017, Arm Limited. All rights reserved.
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
#include "bd/lfs2_testbd.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
int lfs2_testbd_createcfg(const struct lfs2_config *cfg, const char *path,
|
||||
const struct lfs2_testbd_config *bdcfg) {
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_createcfg(%p {.context=%p, "
|
||||
".read=%p, .prog=%p, .erase=%p, .sync=%p, "
|
||||
".read_size=%"PRIu32", .prog_size=%"PRIu32", "
|
||||
".block_size=%"PRIu32", .block_count=%"PRIu32"}, "
|
||||
"\"%s\", "
|
||||
"%p {.erase_value=%"PRId32", .erase_cycles=%"PRIu32", "
|
||||
".badblock_behavior=%"PRIu8", .power_cycles=%"PRIu32", "
|
||||
".buffer=%p, .wear_buffer=%p})",
|
||||
(void*)cfg, cfg->context,
|
||||
(void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog,
|
||||
(void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync,
|
||||
cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count,
|
||||
path, (void*)bdcfg, bdcfg->erase_value, bdcfg->erase_cycles,
|
||||
bdcfg->badblock_behavior, bdcfg->power_cycles,
|
||||
bdcfg->buffer, bdcfg->wear_buffer);
|
||||
lfs2_testbd_t *bd = cfg->context;
|
||||
bd->cfg = bdcfg;
|
||||
|
||||
// setup testing things
|
||||
bd->persist = path;
|
||||
bd->power_cycles = bd->cfg->power_cycles;
|
||||
|
||||
if (bd->cfg->erase_cycles) {
|
||||
if (bd->cfg->wear_buffer) {
|
||||
bd->wear = bd->cfg->wear_buffer;
|
||||
} else {
|
||||
bd->wear = lfs2_malloc(sizeof(lfs2_testbd_wear_t)*cfg->block_count);
|
||||
if (!bd->wear) {
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_createcfg -> %d", LFS2_ERR_NOMEM);
|
||||
return LFS2_ERR_NOMEM;
|
||||
}
|
||||
}
|
||||
|
||||
memset(bd->wear, 0, sizeof(lfs2_testbd_wear_t) * cfg->block_count);
|
||||
}
|
||||
|
||||
// create underlying block device
|
||||
if (bd->persist) {
|
||||
bd->u.file.cfg = (struct lfs2_filebd_config){
|
||||
.erase_value = bd->cfg->erase_value,
|
||||
};
|
||||
int err = lfs2_filebd_createcfg(cfg, path, &bd->u.file.cfg);
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_createcfg -> %d", err);
|
||||
return err;
|
||||
} else {
|
||||
bd->u.ram.cfg = (struct lfs2_rambd_config){
|
||||
.erase_value = bd->cfg->erase_value,
|
||||
.buffer = bd->cfg->buffer,
|
||||
};
|
||||
int err = lfs2_rambd_createcfg(cfg, &bd->u.ram.cfg);
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_createcfg -> %d", err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
int lfs2_testbd_create(const struct lfs2_config *cfg, const char *path) {
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_create(%p {.context=%p, "
|
||||
".read=%p, .prog=%p, .erase=%p, .sync=%p, "
|
||||
".read_size=%"PRIu32", .prog_size=%"PRIu32", "
|
||||
".block_size=%"PRIu32", .block_count=%"PRIu32"}, "
|
||||
"\"%s\")",
|
||||
(void*)cfg, cfg->context,
|
||||
(void*)(uintptr_t)cfg->read, (void*)(uintptr_t)cfg->prog,
|
||||
(void*)(uintptr_t)cfg->erase, (void*)(uintptr_t)cfg->sync,
|
||||
cfg->read_size, cfg->prog_size, cfg->block_size, cfg->block_count,
|
||||
path);
|
||||
static const struct lfs2_testbd_config defaults = {.erase_value=-1};
|
||||
int err = lfs2_testbd_createcfg(cfg, path, &defaults);
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_create -> %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
int lfs2_testbd_destroy(const struct lfs2_config *cfg) {
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_destroy(%p)", (void*)cfg);
|
||||
lfs2_testbd_t *bd = cfg->context;
|
||||
if (bd->cfg->erase_cycles && !bd->cfg->wear_buffer) {
|
||||
lfs2_free(bd->wear);
|
||||
}
|
||||
|
||||
if (bd->persist) {
|
||||
int err = lfs2_filebd_destroy(cfg);
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_destroy -> %d", err);
|
||||
return err;
|
||||
} else {
|
||||
int err = lfs2_rambd_destroy(cfg);
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_destroy -> %d", err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
/// Internal mapping to block devices ///
|
||||
static int lfs2_testbd_rawread(const struct lfs2_config *cfg, lfs2_block_t block,
|
||||
lfs2_off_t off, void *buffer, lfs2_size_t size) {
|
||||
lfs2_testbd_t *bd = cfg->context;
|
||||
if (bd->persist) {
|
||||
return lfs2_filebd_read(cfg, block, off, buffer, size);
|
||||
} else {
|
||||
return lfs2_rambd_read(cfg, block, off, buffer, size);
|
||||
}
|
||||
}
|
||||
|
||||
static int lfs2_testbd_rawprog(const struct lfs2_config *cfg, lfs2_block_t block,
|
||||
lfs2_off_t off, const void *buffer, lfs2_size_t size) {
|
||||
lfs2_testbd_t *bd = cfg->context;
|
||||
if (bd->persist) {
|
||||
return lfs2_filebd_prog(cfg, block, off, buffer, size);
|
||||
} else {
|
||||
return lfs2_rambd_prog(cfg, block, off, buffer, size);
|
||||
}
|
||||
}
|
||||
|
||||
static int lfs2_testbd_rawerase(const struct lfs2_config *cfg,
|
||||
lfs2_block_t block) {
|
||||
lfs2_testbd_t *bd = cfg->context;
|
||||
if (bd->persist) {
|
||||
return lfs2_filebd_erase(cfg, block);
|
||||
} else {
|
||||
return lfs2_rambd_erase(cfg, block);
|
||||
}
|
||||
}
|
||||
|
||||
static int lfs2_testbd_rawsync(const struct lfs2_config *cfg) {
|
||||
lfs2_testbd_t *bd = cfg->context;
|
||||
if (bd->persist) {
|
||||
return lfs2_filebd_sync(cfg);
|
||||
} else {
|
||||
return lfs2_rambd_sync(cfg);
|
||||
}
|
||||
}
|
||||
|
||||
/// block device API ///
|
||||
int lfs2_testbd_read(const struct lfs2_config *cfg, lfs2_block_t block,
|
||||
lfs2_off_t off, void *buffer, lfs2_size_t size) {
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_read(%p, "
|
||||
"0x%"PRIx32", %"PRIu32", %p, %"PRIu32")",
|
||||
(void*)cfg, block, off, buffer, size);
|
||||
lfs2_testbd_t *bd = cfg->context;
|
||||
|
||||
// check if read is valid
|
||||
LFS2_ASSERT(off % cfg->read_size == 0);
|
||||
LFS2_ASSERT(size % cfg->read_size == 0);
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
|
||||
// block bad?
|
||||
if (bd->cfg->erase_cycles && bd->wear[block] >= bd->cfg->erase_cycles &&
|
||||
bd->cfg->badblock_behavior == LFS2_TESTBD_BADBLOCK_READERROR) {
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_read -> %d", LFS2_ERR_CORRUPT);
|
||||
return LFS2_ERR_CORRUPT;
|
||||
}
|
||||
|
||||
// read
|
||||
int err = lfs2_testbd_rawread(cfg, block, off, buffer, size);
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_read -> %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
int lfs2_testbd_prog(const struct lfs2_config *cfg, lfs2_block_t block,
|
||||
lfs2_off_t off, const void *buffer, lfs2_size_t size) {
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_prog(%p, "
|
||||
"0x%"PRIx32", %"PRIu32", %p, %"PRIu32")",
|
||||
(void*)cfg, block, off, buffer, size);
|
||||
lfs2_testbd_t *bd = cfg->context;
|
||||
|
||||
// check if write is valid
|
||||
LFS2_ASSERT(off % cfg->prog_size == 0);
|
||||
LFS2_ASSERT(size % cfg->prog_size == 0);
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
|
||||
// block bad?
|
||||
if (bd->cfg->erase_cycles && bd->wear[block] >= bd->cfg->erase_cycles) {
|
||||
if (bd->cfg->badblock_behavior ==
|
||||
LFS2_TESTBD_BADBLOCK_PROGERROR) {
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_prog -> %d", LFS2_ERR_CORRUPT);
|
||||
return LFS2_ERR_CORRUPT;
|
||||
} else if (bd->cfg->badblock_behavior ==
|
||||
LFS2_TESTBD_BADBLOCK_PROGNOOP ||
|
||||
bd->cfg->badblock_behavior ==
|
||||
LFS2_TESTBD_BADBLOCK_ERASENOOP) {
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_prog -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// prog
|
||||
int err = lfs2_testbd_rawprog(cfg, block, off, buffer, size);
|
||||
if (err) {
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_prog -> %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
// lose power?
|
||||
if (bd->power_cycles > 0) {
|
||||
bd->power_cycles -= 1;
|
||||
if (bd->power_cycles == 0) {
|
||||
// sync to make sure we persist the last changes
|
||||
LFS2_ASSERT(lfs2_testbd_rawsync(cfg) == 0);
|
||||
// simulate power loss
|
||||
exit(33);
|
||||
}
|
||||
}
|
||||
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_prog -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lfs2_testbd_erase(const struct lfs2_config *cfg, lfs2_block_t block) {
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_erase(%p, 0x%"PRIx32")", (void*)cfg, block);
|
||||
lfs2_testbd_t *bd = cfg->context;
|
||||
|
||||
// check if erase is valid
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
|
||||
// block bad?
|
||||
if (bd->cfg->erase_cycles) {
|
||||
if (bd->wear[block] >= bd->cfg->erase_cycles) {
|
||||
if (bd->cfg->badblock_behavior ==
|
||||
LFS2_TESTBD_BADBLOCK_ERASEERROR) {
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_erase -> %d", LFS2_ERR_CORRUPT);
|
||||
return LFS2_ERR_CORRUPT;
|
||||
} else if (bd->cfg->badblock_behavior ==
|
||||
LFS2_TESTBD_BADBLOCK_ERASENOOP) {
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_erase -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
// mark wear
|
||||
bd->wear[block] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
// erase
|
||||
int err = lfs2_testbd_rawerase(cfg, block);
|
||||
if (err) {
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_erase -> %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
// lose power?
|
||||
if (bd->power_cycles > 0) {
|
||||
bd->power_cycles -= 1;
|
||||
if (bd->power_cycles == 0) {
|
||||
// sync to make sure we persist the last changes
|
||||
LFS2_ASSERT(lfs2_testbd_rawsync(cfg) == 0);
|
||||
// simulate power loss
|
||||
exit(33);
|
||||
}
|
||||
}
|
||||
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_prog -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lfs2_testbd_sync(const struct lfs2_config *cfg) {
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_sync(%p)", (void*)cfg);
|
||||
int err = lfs2_testbd_rawsync(cfg);
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_sync -> %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
/// simulated wear operations ///
|
||||
lfs2_testbd_swear_t lfs2_testbd_getwear(const struct lfs2_config *cfg,
|
||||
lfs2_block_t block) {
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_getwear(%p, %"PRIu32")", (void*)cfg, block);
|
||||
lfs2_testbd_t *bd = cfg->context;
|
||||
|
||||
// check if block is valid
|
||||
LFS2_ASSERT(bd->cfg->erase_cycles);
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_getwear -> %"PRIu32, bd->wear[block]);
|
||||
return bd->wear[block];
|
||||
}
|
||||
|
||||
int lfs2_testbd_setwear(const struct lfs2_config *cfg,
|
||||
lfs2_block_t block, lfs2_testbd_wear_t wear) {
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_setwear(%p, %"PRIu32")", (void*)cfg, block);
|
||||
lfs2_testbd_t *bd = cfg->context;
|
||||
|
||||
// check if block is valid
|
||||
LFS2_ASSERT(bd->cfg->erase_cycles);
|
||||
LFS2_ASSERT(block < cfg->block_count);
|
||||
|
||||
bd->wear[block] = wear;
|
||||
|
||||
LFS2_TESTBD_TRACE("lfs2_testbd_setwear -> %d", 0);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,142 +0,0 @@
|
||||
/*
|
||||
* Testing block device, wraps filebd and rambd while providing a bunch
|
||||
* of hooks for testing littlefs in various conditions.
|
||||
*
|
||||
* Copyright (c) 2022, The littlefs authors.
|
||||
* Copyright (c) 2017, Arm Limited. All rights reserved.
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
#ifndef LFS2_TESTBD_H
|
||||
#define LFS2_TESTBD_H
|
||||
|
||||
#include "lfs2.h"
|
||||
#include "lfs2_util.h"
|
||||
#include "bd/lfs2_rambd.h"
|
||||
#include "bd/lfs2_filebd.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
|
||||
// Block device specific tracing
|
||||
#ifdef LFS2_TESTBD_YES_TRACE
|
||||
#define LFS2_TESTBD_TRACE(...) LFS2_TRACE(__VA_ARGS__)
|
||||
#else
|
||||
#define LFS2_TESTBD_TRACE(...)
|
||||
#endif
|
||||
|
||||
// Mode determining how "bad blocks" behave during testing. This simulates
|
||||
// some real-world circumstances such as progs not sticking (prog-noop),
|
||||
// a readonly disk (erase-noop), and ECC failures (read-error).
|
||||
//
|
||||
// Not that read-noop is not allowed. Read _must_ return a consistent (but
|
||||
// may be arbitrary) value on every read.
|
||||
enum lfs2_testbd_badblock_behavior {
|
||||
LFS2_TESTBD_BADBLOCK_PROGERROR,
|
||||
LFS2_TESTBD_BADBLOCK_ERASEERROR,
|
||||
LFS2_TESTBD_BADBLOCK_READERROR,
|
||||
LFS2_TESTBD_BADBLOCK_PROGNOOP,
|
||||
LFS2_TESTBD_BADBLOCK_ERASENOOP,
|
||||
};
|
||||
|
||||
// Type for measuring wear
|
||||
typedef uint32_t lfs2_testbd_wear_t;
|
||||
typedef int32_t lfs2_testbd_swear_t;
|
||||
|
||||
// testbd config, this is required for testing
|
||||
struct lfs2_testbd_config {
|
||||
// 8-bit erase value to use for simulating erases. -1 does not simulate
|
||||
// erases, which can speed up testing by avoiding all the extra block-device
|
||||
// operations to store the erase value.
|
||||
int32_t erase_value;
|
||||
|
||||
// Number of erase cycles before a block becomes "bad". The exact behavior
|
||||
// of bad blocks is controlled by the badblock_mode.
|
||||
uint32_t erase_cycles;
|
||||
|
||||
// The mode determining how bad blocks fail
|
||||
uint8_t badblock_behavior;
|
||||
|
||||
// Number of write operations (erase/prog) before forcefully killing
|
||||
// the program with exit. Simulates power-loss. 0 disables.
|
||||
uint32_t power_cycles;
|
||||
|
||||
// Optional buffer for RAM block device.
|
||||
void *buffer;
|
||||
|
||||
// Optional buffer for wear
|
||||
void *wear_buffer;
|
||||
};
|
||||
|
||||
// testbd state
|
||||
typedef struct lfs2_testbd {
|
||||
union {
|
||||
struct {
|
||||
lfs2_filebd_t bd;
|
||||
struct lfs2_filebd_config cfg;
|
||||
} file;
|
||||
struct {
|
||||
lfs2_rambd_t bd;
|
||||
struct lfs2_rambd_config cfg;
|
||||
} ram;
|
||||
} u;
|
||||
|
||||
bool persist;
|
||||
uint32_t power_cycles;
|
||||
lfs2_testbd_wear_t *wear;
|
||||
|
||||
const struct lfs2_testbd_config *cfg;
|
||||
} lfs2_testbd_t;
|
||||
|
||||
|
||||
/// Block device API ///
|
||||
|
||||
// Create a test block device using the geometry in lfs2_config
|
||||
//
|
||||
// Note that filebd is used if a path is provided, if path is NULL
|
||||
// testbd will use rambd which can be much faster.
|
||||
int lfs2_testbd_create(const struct lfs2_config *cfg, const char *path);
|
||||
int lfs2_testbd_createcfg(const struct lfs2_config *cfg, const char *path,
|
||||
const struct lfs2_testbd_config *bdcfg);
|
||||
|
||||
// Clean up memory associated with block device
|
||||
int lfs2_testbd_destroy(const struct lfs2_config *cfg);
|
||||
|
||||
// Read a block
|
||||
int lfs2_testbd_read(const struct lfs2_config *cfg, lfs2_block_t block,
|
||||
lfs2_off_t off, void *buffer, lfs2_size_t size);
|
||||
|
||||
// Program a block
|
||||
//
|
||||
// The block must have previously been erased.
|
||||
int lfs2_testbd_prog(const struct lfs2_config *cfg, lfs2_block_t block,
|
||||
lfs2_off_t off, const void *buffer, lfs2_size_t size);
|
||||
|
||||
// Erase a block
|
||||
//
|
||||
// A block must be erased before being programmed. The
|
||||
// state of an erased block is undefined.
|
||||
int lfs2_testbd_erase(const struct lfs2_config *cfg, lfs2_block_t block);
|
||||
|
||||
// Sync the block device
|
||||
int lfs2_testbd_sync(const struct lfs2_config *cfg);
|
||||
|
||||
|
||||
/// Additional extended API for driving test features ///
|
||||
|
||||
// Get simulated wear on a given block
|
||||
lfs2_testbd_swear_t lfs2_testbd_getwear(const struct lfs2_config *cfg,
|
||||
lfs2_block_t block);
|
||||
|
||||
// Manually set simulated wear on a given block
|
||||
int lfs2_testbd_setwear(const struct lfs2_config *cfg,
|
||||
lfs2_block_t block, lfs2_testbd_wear_t wear);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user