mirror of
https://github.com/eledio-devices/thirdparty-littlefs.git
synced 2025-11-01 00:38:29 +01:00
Standardized on externally provided config structs in the block devices
I have another branch where I tried exploring inlined config structs backed by RAM, but this did not work out due to additional RAM and ROM costs. Changing the bds to follow this was surprisingly annoying, as they had a lot of shared geometry that was previously in a single shared config object, and the way that testbd contains either of rambd and filebd made configuring all three of these a bit complicated. Ended up settling on a lfs_testbd_cfg that contains optional pointers to lfs_rambd_cfg and lfs_filebd_cfg. These can be NULL, but only if that bd goes unused.
This commit is contained in:
@@ -19,9 +19,7 @@ int lfs_filebd_createcfg(lfs_filebd_t *bd, const char *path,
|
||||
(void*)bd, path, (void*)cfg,
|
||||
cfg->read_size, cfg->prog_size, cfg->erase_size, cfg->erase_count,
|
||||
cfg->erase_value);
|
||||
|
||||
// copy over config
|
||||
bd->cfg = *cfg;
|
||||
bd->cfg = cfg;
|
||||
|
||||
// open file
|
||||
bd->fd = open(path, O_RDWR | O_CREAT, 0666);
|
||||
@@ -54,18 +52,18 @@ int lfs_filebd_read(lfs_filebd_t *bd, lfs_block_t block,
|
||||
(void*)bd, block, off, buffer, size);
|
||||
|
||||
// check if read is valid
|
||||
LFS_ASSERT(off % bd->cfg.read_size == 0);
|
||||
LFS_ASSERT(size % bd->cfg.read_size == 0);
|
||||
LFS_ASSERT(block < bd->cfg.erase_count);
|
||||
LFS_ASSERT(off % bd->cfg->read_size == 0);
|
||||
LFS_ASSERT(size % bd->cfg->read_size == 0);
|
||||
LFS_ASSERT(block < bd->cfg->erase_count);
|
||||
|
||||
// zero for reproducability (in case file is truncated)
|
||||
if (bd->cfg.erase_value != -1) {
|
||||
memset(buffer, bd->cfg.erase_value, size);
|
||||
if (bd->cfg->erase_value != -1) {
|
||||
memset(buffer, bd->cfg->erase_value, size);
|
||||
}
|
||||
|
||||
// read
|
||||
off_t res1 = lseek(bd->fd,
|
||||
(off_t)block*bd->cfg.erase_size + (off_t)off, SEEK_SET);
|
||||
(off_t)block*bd->cfg->erase_size + (off_t)off, SEEK_SET);
|
||||
if (res1 < 0) {
|
||||
int err = -errno;
|
||||
LFS_FILEBD_TRACE("lfs_filebd_read -> %d", err);
|
||||
@@ -90,14 +88,14 @@ int lfs_filebd_prog(lfs_filebd_t *bd, lfs_block_t block,
|
||||
(void*)bd, block, off, buffer, size);
|
||||
|
||||
// check if write is valid
|
||||
LFS_ASSERT(off % bd->cfg.prog_size == 0);
|
||||
LFS_ASSERT(size % bd->cfg.prog_size == 0);
|
||||
LFS_ASSERT(block < bd->cfg.erase_count);
|
||||
LFS_ASSERT(off % bd->cfg->prog_size == 0);
|
||||
LFS_ASSERT(size % bd->cfg->prog_size == 0);
|
||||
LFS_ASSERT(block < bd->cfg->erase_count);
|
||||
|
||||
// check that data was erased? only needed for testing
|
||||
if (bd->cfg.erase_value != -1) {
|
||||
if (bd->cfg->erase_value != -1) {
|
||||
off_t res1 = lseek(bd->fd,
|
||||
(off_t)block*bd->cfg.erase_size + (off_t)off, SEEK_SET);
|
||||
(off_t)block*bd->cfg->erase_size + (off_t)off, SEEK_SET);
|
||||
if (res1 < 0) {
|
||||
int err = -errno;
|
||||
LFS_FILEBD_TRACE("lfs_filebd_prog -> %d", err);
|
||||
@@ -113,13 +111,13 @@ int lfs_filebd_prog(lfs_filebd_t *bd, lfs_block_t block,
|
||||
return err;
|
||||
}
|
||||
|
||||
LFS_ASSERT(c == bd->cfg.erase_value);
|
||||
LFS_ASSERT(c == bd->cfg->erase_value);
|
||||
}
|
||||
}
|
||||
|
||||
// program data
|
||||
off_t res1 = lseek(bd->fd,
|
||||
(off_t)block*bd->cfg.erase_size + (off_t)off, SEEK_SET);
|
||||
(off_t)block*bd->cfg->erase_size + (off_t)off, SEEK_SET);
|
||||
if (res1 < 0) {
|
||||
int err = -errno;
|
||||
LFS_FILEBD_TRACE("lfs_filebd_prog -> %d", err);
|
||||
@@ -141,19 +139,19 @@ int lfs_filebd_erase(lfs_filebd_t *bd, lfs_block_t block) {
|
||||
LFS_FILEBD_TRACE("lfs_filebd_erase(%p, 0x%"PRIx32")", (void*)bd, block);
|
||||
|
||||
// check if erase is valid
|
||||
LFS_ASSERT(block < bd->cfg.erase_count);
|
||||
LFS_ASSERT(block < bd->cfg->erase_count);
|
||||
|
||||
// erase, only needed for testing
|
||||
if (bd->cfg.erase_value != -1) {
|
||||
off_t res1 = lseek(bd->fd, (off_t)block*bd->cfg.erase_size, SEEK_SET);
|
||||
if (bd->cfg->erase_value != -1) {
|
||||
off_t res1 = lseek(bd->fd, (off_t)block*bd->cfg->erase_size, SEEK_SET);
|
||||
if (res1 < 0) {
|
||||
int err = -errno;
|
||||
LFS_FILEBD_TRACE("lfs_filebd_erase -> %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
for (lfs_off_t i = 0; i < bd->cfg.erase_size; i++) {
|
||||
ssize_t res2 = write(bd->fd, &(uint8_t){bd->cfg.erase_value}, 1);
|
||||
for (lfs_off_t i = 0; i < bd->cfg->erase_size; i++) {
|
||||
ssize_t res2 = write(bd->fd, &(uint8_t){bd->cfg->erase_value}, 1);
|
||||
if (res2 < 0) {
|
||||
int err = -errno;
|
||||
LFS_FILEBD_TRACE("lfs_filebd_erase -> %d", err);
|
||||
|
||||
Reference in New Issue
Block a user