From f7fef9937f9a05c7534dcb8d346cc328014c1154 Mon Sep 17 00:00:00 2001 From: Lucjan Bryndza Date: Fri, 15 Jan 2021 20:08:16 +0100 Subject: [PATCH] [EGD-5259] Fix littlefs format for block devices Currently genlittlefs is unable to format block devices currently it works only on the file images. This patch resolves problem with bd format. --- host-tools/genlittlefs/lfs_ioaccess.c | 15 +++++++++++++++ host-tools/genlittlefs/mklfs.c | 18 ++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/host-tools/genlittlefs/lfs_ioaccess.c b/host-tools/genlittlefs/lfs_ioaccess.c index fc8ad0bd6f4379a9bc26b8ea1bcdfeb25ae832d8..5fcd2e7601ccf9f95e0ae7a01aa73699563dfbea 100644 --- a/host-tools/genlittlefs/lfs_ioaccess.c +++ b/host-tools/genlittlefs/lfs_ioaccess.c @@ -11,6 +11,8 @@ #include #include #include +#include +#include struct lfs_ioaccess_context { @@ -179,6 +181,19 @@ struct lfs_ioaccess_context *lfs_ioaccess_open(struct lfs_config *cfg, free(ret); return NULL; } + if (S_ISBLK(statbuf.st_mode)) { + uint64_t blk_size; + err = ioctl(ret->file_des, BLKGETSIZE64, &blk_size); + if (err < 0) { + close(ret->file_des); + free(ret); + return NULL; + } + else { + statbuf.st_size = blk_size; + } + } + off_t start_pos = 0; ret->last_offs = statbuf.st_size; if (partition) { diff --git a/host-tools/genlittlefs/mklfs.c b/host-tools/genlittlefs/mklfs.c index cef6b394a8e672aefa6af9e00662cb412f2e6b50..96340046f0945887491274ce84cece5ce5d671ba 100644 --- a/host-tools/genlittlefs/mklfs.c +++ b/host-tools/genlittlefs/mklfs.c @@ -14,6 +14,7 @@ #include #include #include +#include #include struct lfs_info_summary @@ -211,15 +212,20 @@ static int add_to_lfs(lfs_t *lfs, const char *dir, struct lfs_info_summary *summ return err; } -static void print_error(const char *str, int error) __attribute__((nonnull(1))); -static void print_error(const char *str, int error) +static void print_error(int error, const char *format, ...) __attribute__((nonnull(2))); +static void print_error(int error, const char *format, ...) { + va_list arglist; + va_start(arglist, format); + vfprintf(stderr, format, arglist); + va_end(arglist); + if (error == -1) { char buf[1024]; - fprintf(stderr, "system_error %s %s\n", str, strerror_r(errno, buf, sizeof buf)); + fprintf(stderr, " system_error: %s\n", strerror_r(errno, buf, sizeof buf)); } else { - fprintf(stderr, "lfs_error %s %i\n", str, error); + fprintf(stderr, " lfs_error: %i\n", error); } } @@ -279,7 +285,7 @@ int main(int argc, char **argv) cfg.block_count = (curr_part->end - curr_part->start) / lopts.block_size; ioctx = lfs_ioaccess_open(&cfg, lopts.dst_image, curr_part); if (!ioctx) { - perror("Unable to open file:"); + fprintf(stderr, "Unable to open file: %s error %s\n", lopts.dst_image, strerror(errno)); free(parts); free(lopts.src_dirs); return EXIT_FAILURE; @@ -345,7 +351,7 @@ int main(int argc, char **argv) for (size_t ndir = 0; ndir < lopts.src_dirs_siz; ++ndir) { err = add_to_lfs(&lfs, lopts.src_dirs[ndir], &prog_summary, lopts.verbose); if (err) { - print_error("Unable to open file:", err); + print_error(err, "Unable to open file: %s", lopts.src_dirs[ndir]); lfs_ioaccess_close(ioctx); free(lopts.src_dirs); lfs_unmount(&lfs);