From 667ba85f0823c0a987a6746eda32061c81fac730 Mon Sep 17 00:00:00 2001 From: Lucjan Bryndza Date: Wed, 7 Jul 2021 14:47:46 +0200 Subject: [PATCH] [CP-314] Allow genlittlefs to create empty part Allow genlittlefs tool to create empty partition without any data on the partition. It will allow to create empty backup partition --- host-tools/genlittlefs/parse_args.c | 6 +++--- host-tools/genlittlefs/parse_partitions.c | 15 +++++++++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/host-tools/genlittlefs/parse_args.c b/host-tools/genlittlefs/parse_args.c index 030c9ce6cdaba02318053a5f74ffb194270bbab7..2205ea198dbdf312d5879db831f347913d53b693 100644 --- a/host-tools/genlittlefs/parse_args.c +++ b/host-tools/genlittlefs/parse_args.c @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include "parse_args.h" @@ -295,8 +295,8 @@ int parse_program_args(int argc, char **argv, struct littlefs_opts *opts) } } else { - fprintf(stderr, "source directories not specified\n"); - return -1; + opts->src_dirs_siz = 0; + opts->src_dirs = NULL; } if (!opts->dst_image) { fprintf(stderr, "--image is not specified\n"); diff --git a/host-tools/genlittlefs/parse_partitions.c b/host-tools/genlittlefs/parse_partitions.c index 4c72a1a94da8353f56afda93609a2413eec99213..8cf76b842567c9dd4f515c8b1bf80484b3d0bdcf 100644 --- a/host-tools/genlittlefs/parse_partitions.c +++ b/host-tools/genlittlefs/parse_partitions.c @@ -12,7 +12,7 @@ #include #include #include -static const size_t sector_size = 512; +static const size_t sector_size = 512; static const size_t bootstrap_offset = 0x00E0; struct partition *find_partitions(const char *filename, part_type_t ptype, size_t *nelems) @@ -91,7 +91,7 @@ static ssize_t sector_block_size(int filedes) if (err < 0) { return err; } - uint64_t blk_sz; + uint64_t blk_sz = 0; if (S_ISBLK(statbuf.st_mode)) { err = ioctl(filedes, BLKSSZGET, &blk_sz); if (err < 0) { @@ -115,8 +115,15 @@ int write_partition_bootunit(const char *filename, int part_num, uint32_t block_ return -1; } const ssize_t sector_size = sector_block_size(fd); - char *const sect_buf = malloc(sector_size); - if (read(fd, sect_buf, sector_size) != sector_size) { + if (sector_size <= 0) { + return -1; + } + char *const sect_buf = malloc(sector_size); + if (!sect_buf) { + return -1; + } + int ret; + if ((ret = read(fd, sect_buf, sector_size)) != sector_size) { close(fd); free(sect_buf); return -1;