Skip to content

Commit

Permalink
Merge pull request #70 from canokeys/feature/lfs2.8
Browse files Browse the repository at this point in the history
Feature/lfs2.8
  • Loading branch information
z4yx authored Nov 9, 2023
2 parents 7a67cb4 + 9dd2077 commit a8aef6c
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 32 deletions.
2 changes: 2 additions & 0 deletions include/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <lfs.h>

#define LFS_CACHE_SIZE 512

int fs_format(const struct lfs_config *cfg);
int fs_mount(const struct lfs_config *cfg);
int read_file(const char *path, void *buf, lfs_soff_t off, lfs_size_t len);
Expand Down
2 changes: 1 addition & 1 deletion littlefs
16 changes: 11 additions & 5 deletions src/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@

static lfs_t lfs;

static uint8_t file_buffer[LFS_CACHE_SIZE];

static struct lfs_file_config file_config = {
.buffer = file_buffer
};

int fs_format(const struct lfs_config *cfg) { return lfs_format(&lfs, cfg); }

int fs_mount(const struct lfs_config *cfg) { return lfs_mount(&lfs, cfg); }

int read_file(const char *path, void *buf, lfs_soff_t off, lfs_size_t len) {
lfs_file_t f;
lfs_ssize_t read_length;
int err = lfs_file_open(&lfs, &f, path, LFS_O_RDONLY);
int err = lfs_file_opencfg(&lfs, &f, path, LFS_O_RDONLY, &file_config);
if (err < 0) return err;
err = lfs_file_seek(&lfs, &f, off, LFS_SEEK_SET);
if (err < 0) goto err_close;
Expand All @@ -37,7 +43,7 @@ int write_file(const char *path, const void *buf, lfs_soff_t off, lfs_size_t len
#endif
int flags = LFS_O_WRONLY | LFS_O_CREAT;
if (trunc) flags |= LFS_O_TRUNC;
int err = lfs_file_open(&lfs, &f, path, flags);
int err = lfs_file_opencfg(&lfs, &f, path, flags, &file_config);
if (err < 0) return err;
err = lfs_file_seek(&lfs, &f, off, LFS_SEEK_SET);
if (err < 0) goto err_close;
Expand All @@ -55,7 +61,7 @@ int write_file(const char *path, const void *buf, lfs_soff_t off, lfs_size_t len

int append_file(const char *path, const void *buf, lfs_size_t len) {
lfs_file_t f;
int err = lfs_file_open(&lfs, &f, path, LFS_O_WRONLY | LFS_O_CREAT);
int err = lfs_file_opencfg(&lfs, &f, path, LFS_O_WRONLY | LFS_O_CREAT, &file_config);
if (err < 0) return err;
err = lfs_file_seek(&lfs, &f, 0, LFS_SEEK_END);
if (err < 0) goto err_close;
Expand All @@ -74,7 +80,7 @@ int append_file(const char *path, const void *buf, lfs_size_t len) {
int truncate_file(const char *path, lfs_size_t len) {
lfs_file_t f;
int flags = LFS_O_WRONLY | LFS_O_CREAT;
int err = lfs_file_open(&lfs, &f, path, flags);
int err = lfs_file_opencfg(&lfs, &f, path, flags, &file_config);
if (err < 0) return err;
err = lfs_file_truncate(&lfs, &f, len);
if (err < 0) goto err_close;
Expand All @@ -96,7 +102,7 @@ int write_attr(const char *path, uint8_t attr, const void *buf, lfs_size_t len)

int get_file_size(const char *path) {
lfs_file_t f;
int err = lfs_file_open(&lfs, &f, path, LFS_O_RDONLY);
int err = lfs_file_opencfg(&lfs, &f, path, LFS_O_RDONLY, &file_config);
if (err < 0) return err;
int size = lfs_file_size(&lfs, &f);
if (size < 0) {
Expand Down
14 changes: 8 additions & 6 deletions test/test_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,22 @@ static void test_encode_eddsa(void **state) {
int main() {
struct lfs_config cfg;
lfs_filebd_t bd;
struct lfs_filebd_config bdcfg = {.read_size = 1, .prog_size = 512, .erase_size = 512, .erase_count = 256};
bd.cfg = &bdcfg;
memset(&cfg, 0, sizeof(cfg));
cfg.context = &bd;
cfg.read = &lfs_filebd_read;
cfg.prog = &lfs_filebd_prog;
cfg.erase = &lfs_filebd_erase;
cfg.sync = &lfs_filebd_sync;
cfg.read_size = 16;
cfg.prog_size = 16;
cfg.read_size = 1;
cfg.prog_size = 512;
cfg.block_size = 512;
cfg.block_count = 400;
cfg.block_count = 256;
cfg.block_cycles = 50000;
cfg.cache_size = 128;
cfg.lookahead_size = 16;
lfs_filebd_create(&cfg, "lfs-root");
cfg.cache_size = 512;
cfg.lookahead_size = 32;
lfs_filebd_create(&cfg, "lfs-root", &bdcfg);

fs_format(&cfg);
fs_mount(&cfg);
Expand Down
14 changes: 8 additions & 6 deletions test/test_oath.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,20 +431,22 @@ static void test_space_full(void **state) {
int main() {
struct lfs_config cfg;
lfs_filebd_t bd;
struct lfs_filebd_config bdcfg = {.read_size = 1, .prog_size = 512, .erase_size = 512, .erase_count = 256};
bd.cfg = &bdcfg;
memset(&cfg, 0, sizeof(cfg));
cfg.context = &bd;
cfg.read = &lfs_filebd_read;
cfg.prog = &lfs_filebd_prog;
cfg.erase = &lfs_filebd_erase;
cfg.sync = &lfs_filebd_sync;
cfg.read_size = 16;
cfg.prog_size = 16;
cfg.read_size = 1;
cfg.prog_size = 512;
cfg.block_size = 512;
cfg.block_count = 400;
cfg.block_count = 256;
cfg.block_cycles = 50000;
cfg.cache_size = 128;
cfg.lookahead_size = 16;
lfs_filebd_create(&cfg, "lfs-root");
cfg.cache_size = 512;
cfg.lookahead_size = 32;
lfs_filebd_create(&cfg, "lfs-root", &bdcfg);

fs_format(&cfg);
fs_mount(&cfg);
Expand Down
14 changes: 8 additions & 6 deletions test/test_openpgp.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,22 @@ static void test_special(void **state) {
int main() {
struct lfs_config cfg;
lfs_filebd_t bd;
struct lfs_filebd_config bdcfg = {.read_size = 1, .prog_size = 512, .erase_size = 512, .erase_count = 256};
bd.cfg = &bdcfg;
memset(&cfg, 0, sizeof(cfg));
cfg.context = &bd;
cfg.read = &lfs_filebd_read;
cfg.prog = &lfs_filebd_prog;
cfg.erase = &lfs_filebd_erase;
cfg.sync = &lfs_filebd_sync;
cfg.read_size = 16;
cfg.prog_size = 16;
cfg.read_size = 1;
cfg.prog_size = 512;
cfg.block_size = 512;
cfg.block_count = 400;
cfg.block_count = 256;
cfg.block_cycles = 50000;
cfg.cache_size = 128;
cfg.lookahead_size = 16;
lfs_filebd_create(&cfg, "lfs-root");
cfg.cache_size = 512;
cfg.lookahead_size = 32;
lfs_filebd_create(&cfg, "lfs-root", &bdcfg);

fs_format(&cfg);
fs_mount(&cfg);
Expand Down
14 changes: 8 additions & 6 deletions test/test_piv.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,22 @@ static void test_regression_fuzz(void **state) {
int main() {
struct lfs_config cfg;
lfs_filebd_t bd;
struct lfs_filebd_config bdcfg = {.read_size = 1, .prog_size = 512, .erase_size = 512, .erase_count = 256};
bd.cfg = &bdcfg;
memset(&cfg, 0, sizeof(cfg));
cfg.context = &bd;
cfg.read = &lfs_filebd_read;
cfg.prog = &lfs_filebd_prog;
cfg.erase = &lfs_filebd_erase;
cfg.sync = &lfs_filebd_sync;
cfg.read_size = 16;
cfg.prog_size = 16;
cfg.read_size = 1;
cfg.prog_size = 512;
cfg.block_size = 512;
cfg.block_count = 400;
cfg.block_count = 256;
cfg.block_cycles = 50000;
cfg.cache_size = 128;
cfg.lookahead_size = 16;
lfs_filebd_create(&cfg, "lfs-root");
cfg.cache_size = 512;
cfg.lookahead_size = 32;
lfs_filebd_create(&cfg, "lfs-root", &bdcfg);

fs_format(&cfg);
fs_mount(&cfg);
Expand Down
6 changes: 4 additions & 2 deletions virt-card/fabrication.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

static struct lfs_config cfg;
static lfs_filebd_t bd;
static struct lfs_filebd_config bdcfg = {.read_size = 1, .prog_size = 512, .erase_size = 512, .erase_count = 256};

uint8_t private_key[] = {0x46, 0x5b, 0x44, 0x5d, 0x8e, 0x78, 0x34, 0x53, 0xf7, 0x4b, 0x90,
0x00, 0xd2, 0x20, 0x32, 0x51, 0x99, 0x5e, 0x12, 0xdc, 0xd1, 0x21,
Expand Down Expand Up @@ -103,6 +104,7 @@ static void oath_init() {
}

int card_fs_init(const char *lfs_root) {
bd.cfg = &bdcfg;
memset(&cfg, 0, sizeof(cfg));
cfg.context = &bd;
cfg.read = &lfs_filebd_read;
Expand All @@ -115,8 +117,8 @@ int card_fs_init(const char *lfs_root) {
cfg.block_count = 256;
cfg.block_cycles = 50000;
cfg.cache_size = 512;
cfg.lookahead_size = 16;
if (lfs_filebd_create(&cfg, lfs_root)) return 1;
cfg.lookahead_size = 32;
if (lfs_filebd_create(&cfg, lfs_root, &bdcfg)) return 1;

int err = fs_mount(&cfg);
if (err) { // should happen for the first boot
Expand Down

0 comments on commit a8aef6c

Please sign in to comment.