From d0aa98b64625e6de4ad6c15bf69c24a924fa6903 Mon Sep 17 00:00:00 2001 From: dangfan Date: Sun, 5 Nov 2023 10:58:51 +0800 Subject: [PATCH 1/6] upgrade littlefs --- littlefs | 2 +- src/fs.c | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/littlefs b/littlefs index 9c7e2320..c733d9ec 160000 --- a/littlefs +++ b/littlefs @@ -1 +1 @@ -Subproject commit 9c7e232086f865cff0bb96fe753deb66431d91fd +Subproject commit c733d9ec5776dfc949ec6dc71fa9ce3ff71de6e5 diff --git a/src/fs.c b/src/fs.c index 0e652cd3..45c55f8c 100644 --- a/src/fs.c +++ b/src/fs.c @@ -4,6 +4,12 @@ static lfs_t lfs; +static uint8_t file_buffer[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); } @@ -11,7 +17,7 @@ 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; @@ -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; @@ -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; @@ -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; @@ -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) { From 169c894baa933dedf526f36666458459736ad888 Mon Sep 17 00:00:00 2001 From: dangfan Date: Sun, 5 Nov 2023 11:13:55 +0800 Subject: [PATCH 2/6] move cache_size to fs.h --- include/fs.h | 2 ++ src/fs.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/include/fs.h b/include/fs.h index eeb2b3b7..f8a67c6a 100644 --- a/include/fs.h +++ b/include/fs.h @@ -4,6 +4,8 @@ #include +#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); diff --git a/src/fs.c b/src/fs.c index 45c55f8c..75c89407 100644 --- a/src/fs.c +++ b/src/fs.c @@ -4,7 +4,7 @@ static lfs_t lfs; -static uint8_t file_buffer[CACHE_SIZE]; +static uint8_t file_buffer[LFS_CACHE_SIZE]; static struct lfs_file_config file_config = { .buffer = file_buffer From b6c1bae2c9a0d2a978fdf9637cf3e38aea0578fa Mon Sep 17 00:00:00 2001 From: Fan DANG Date: Sun, 5 Nov 2023 14:30:54 +0800 Subject: [PATCH 3/6] fix fabrication for tests --- virt-card/fabrication.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/virt-card/fabrication.c b/virt-card/fabrication.c index f724889c..9c690167 100644 --- a/virt-card/fabrication.c +++ b/virt-card/fabrication.c @@ -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, @@ -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; @@ -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 From 360a162a6c0c9d35e7407f28e0cbb7cd27ea1f9f Mon Sep 17 00:00:00 2001 From: Fan DANG Date: Sun, 5 Nov 2023 14:39:42 +0800 Subject: [PATCH 4/6] fix compilation errors --- test/test_key.c | 14 ++++++++------ test/test_oath.c | 14 ++++++++------ test/test_openpgp.c | 14 ++++++++------ test/test_piv.c | 14 ++++++++------ 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/test/test_key.c b/test/test_key.c index 0c2d49a4..a417f890 100644 --- a/test/test_key.c +++ b/test/test_key.c @@ -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 = 256; + cfg.lookahead_size = 32; + lfs_filebd_create(&cfg, "lfs-root", &bdcfg); fs_format(&cfg); fs_mount(&cfg); diff --git a/test/test_oath.c b/test/test_oath.c index ed7bd3e3..00176ea6 100644 --- a/test/test_oath.c +++ b/test/test_oath.c @@ -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 = 256; + cfg.lookahead_size = 32; + lfs_filebd_create(&cfg, "lfs-root", &bdcfg); fs_format(&cfg); fs_mount(&cfg); diff --git a/test/test_openpgp.c b/test/test_openpgp.c index 331bff6c..f6432aa6 100644 --- a/test/test_openpgp.c +++ b/test/test_openpgp.c @@ -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 = 256; + cfg.lookahead_size = 32; + lfs_filebd_create(&cfg, "lfs-root", &bdcfg); fs_format(&cfg); fs_mount(&cfg); diff --git a/test/test_piv.c b/test/test_piv.c index a9be694f..59ba892e 100644 --- a/test/test_piv.c +++ b/test/test_piv.c @@ -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 = 256; + cfg.lookahead_size = 32; + lfs_filebd_create(&cfg, "lfs-root", &bdcfg); fs_format(&cfg); fs_mount(&cfg); From 28c580f61638ca2483d6266f3a91961b4e121901 Mon Sep 17 00:00:00 2001 From: Fan DANG Date: Sun, 5 Nov 2023 15:05:57 +0800 Subject: [PATCH 5/6] fix cache size in tests --- test/test_key.c | 2 +- test/test_oath.c | 2 +- test/test_openpgp.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_key.c b/test/test_key.c index a417f890..459d7869 100644 --- a/test/test_key.c +++ b/test/test_key.c @@ -184,7 +184,7 @@ int main() { cfg.block_size = 512; cfg.block_count = 256; cfg.block_cycles = 50000; - cfg.cache_size = 256; + cfg.cache_size = 512; cfg.lookahead_size = 32; lfs_filebd_create(&cfg, "lfs-root", &bdcfg); diff --git a/test/test_oath.c b/test/test_oath.c index 00176ea6..53fbe5ac 100644 --- a/test/test_oath.c +++ b/test/test_oath.c @@ -444,7 +444,7 @@ int main() { cfg.block_size = 512; cfg.block_count = 256; cfg.block_cycles = 50000; - cfg.cache_size = 256; + cfg.cache_size = 512; cfg.lookahead_size = 32; lfs_filebd_create(&cfg, "lfs-root", &bdcfg); diff --git a/test/test_openpgp.c b/test/test_openpgp.c index f6432aa6..18659a6f 100644 --- a/test/test_openpgp.c +++ b/test/test_openpgp.c @@ -224,7 +224,7 @@ int main() { cfg.block_size = 512; cfg.block_count = 256; cfg.block_cycles = 50000; - cfg.cache_size = 256; + cfg.cache_size = 512; cfg.lookahead_size = 32; lfs_filebd_create(&cfg, "lfs-root", &bdcfg); From 4623707762772186a968b62999aa35188c583715 Mon Sep 17 00:00:00 2001 From: Fan DANG Date: Sun, 5 Nov 2023 15:11:12 +0800 Subject: [PATCH 6/6] fix cache size for test_piv --- test/test_piv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_piv.c b/test/test_piv.c index 59ba892e..ed7533d6 100644 --- a/test/test_piv.c +++ b/test/test_piv.c @@ -134,7 +134,7 @@ int main() { cfg.block_size = 512; cfg.block_count = 256; cfg.block_cycles = 50000; - cfg.cache_size = 256; + cfg.cache_size = 512; cfg.lookahead_size = 32; lfs_filebd_create(&cfg, "lfs-root", &bdcfg);