From f4ec4511d0729802e783b1757d6bcd556737510e Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 1 Sep 2016 15:27:38 +0200 Subject: Merge rest of ubiutils-common into libmtd common This patch moves the remaining 3 functions from ubiutils-common.{c,h} into libmtd common.{c,h}. The functions are only generic utility functions that other mtd-utils programs may also find usefull and every program that uses libubi links against libmtd anyway so there is no real reason for keeping around a seperate ubiutils-common with only generic helper functions. Signed-off-by: David Oberhollenzer --- include/common.h | 4 + lib/Makemodule.am | 1 + lib/common.c | 158 ++++++++++++++++++++++++++++++++++++ ubi-utils/Makemodule.am | 2 +- ubi-utils/include/ubiutils-common.h | 34 -------- ubi-utils/mtdinfo.c | 7 +- ubi-utils/ubiattach.c | 7 +- ubi-utils/ubiformat.c | 11 ++- ubi-utils/ubimkvol.c | 7 +- ubi-utils/ubinfo.c | 11 ++- ubi-utils/ubinize.c | 13 ++- ubi-utils/ubirsvol.c | 3 +- ubi-utils/ubiutils-common.c | 158 ------------------------------------ 13 files changed, 190 insertions(+), 226 deletions(-) create mode 100644 lib/common.c delete mode 100644 ubi-utils/include/ubiutils-common.h delete mode 100644 ubi-utils/ubiutils-common.c diff --git a/include/common.h b/include/common.h index 11e1989..4f0bb9b 100644 --- a/include/common.h +++ b/include/common.h @@ -220,6 +220,10 @@ do { \ #include "xalloc.h" +long long util_get_bytes(const char *str); +void util_print_bytes(long long bytes, int bracket); +int util_srand(void); + #ifdef __cplusplus } #endif diff --git a/lib/Makemodule.am b/lib/Makemodule.am index b30a8aa..694a151 100644 --- a/lib/Makemodule.am +++ b/lib/Makemodule.am @@ -1,6 +1,7 @@ libmtd_a_SOURCES = \ lib/libmtd.c \ lib/libfec.c \ + lib/common.c \ lib/libcrc32.c \ lib/libmtd_legacy.c \ lib/libmtd_int.h diff --git a/lib/common.c b/lib/common.c new file mode 100644 index 0000000..69b03b3 --- /dev/null +++ b/lib/common.c @@ -0,0 +1,158 @@ +/* + * Copyright (C) 2007, 2008 Nokia Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See + * the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +/* + * This file contains various common stuff. + * + * Authors: Artem Bityutskiy + * Adrian Hunter + */ + +#define PROGRAM_NAME "mtd-utils" + +#include +#include +#include +#include +#include +#include +#include +#include "common.h" + +/** + * get_multiplier - convert size specifier to an integer multiplier. + * @str: the size specifier string + * + * This function parses the @str size specifier, which may be one of + * 'KiB', 'MiB', or 'GiB' into an integer multiplier. Returns positive + * size multiplier in case of success and %-1 in case of failure. + */ +static int get_multiplier(const char *str) +{ + if (!str) + return 1; + + /* Remove spaces before the specifier */ + while (*str == ' ' || *str == '\t') + str += 1; + + if (!strcmp(str, "KiB")) + return 1024; + if (!strcmp(str, "MiB")) + return 1024 * 1024; + if (!strcmp(str, "GiB")) + return 1024 * 1024 * 1024; + + return -1; +} + +/** + * util_get_bytes - convert a string containing amount of bytes into an + * integer + * @str: string to convert + * + * This function parses @str which may have one of 'KiB', 'MiB', or 'GiB' + * size specifiers. Returns positive amount of bytes in case of success and %-1 + * in case of failure. + */ +long long util_get_bytes(const char *str) +{ + char *endp; + long long bytes = strtoull(str, &endp, 0); + + if (endp == str || bytes < 0) { + fprintf(stderr, "incorrect amount of bytes: \"%s\"\n", str); + return -1; + } + + if (*endp != '\0') { + int mult = get_multiplier(endp); + + if (mult == -1) { + fprintf(stderr, "bad size specifier: \"%s\" - " + "should be 'KiB', 'MiB' or 'GiB'\n", endp); + return -1; + } + bytes *= mult; + } + + return bytes; +} + +/** + * util_print_bytes - print bytes. + * @bytes: variable to print + * @bracket: whether brackets have to be put or not + * + * This is a helper function which prints amount of bytes in a human-readable + * form, i.e., it prints the exact amount of bytes following by the approximate + * amount of Kilobytes, Megabytes, or Gigabytes, depending on how big @bytes + * is. + */ +void util_print_bytes(long long bytes, int bracket) +{ + const char *p; + + if (bracket) + p = " ("; + else + p = ", "; + + printf("%lld bytes", bytes); + + if (bytes > 1024 * 1024 * 1024) + printf("%s%.1f GiB", p, (double)bytes / (1024 * 1024 * 1024)); + else if (bytes > 1024 * 1024) + printf("%s%.1f MiB", p, (double)bytes / (1024 * 1024)); + else if (bytes > 1024 && bytes != 0) + printf("%s%.1f KiB", p, (double)bytes / 1024); + else + return; + + if (bracket) + printf(")"); +} + +/** + * util_srand - randomly seed the standard pseudo-random generator. + * + * This helper function seeds the standard libc pseudo-random generator with a + * more or less random value to make sure the 'rand()' call does not return the + * same sequence every time UBI utilities run. Returns zero in case of success + * and a %-1 in case of error. + */ +int util_srand(void) +{ + struct timeval tv; + struct timezone tz; + unsigned int seed; + + /* + * Just assume that a combination of the PID + current time is a + * reasonably random number. + */ + if (gettimeofday(&tv, &tz)) + return -1; + + seed = (unsigned int)tv.tv_sec; + seed += (unsigned int)tv.tv_usec; + seed *= getpid(); + seed %= RAND_MAX; + srand(seed); + return 0; +} diff --git a/ubi-utils/Makemodule.am b/ubi-utils/Makemodule.am index dc68f38..f1c50df 100644 --- a/ubi-utils/Makemodule.am +++ b/ubi-utils/Makemodule.am @@ -1,4 +1,4 @@ -libubi_a_SOURCES = ubi-utils/libubi.c ubi-utils/ubiutils-common.c ubi-utils/libubi_int.h +libubi_a_SOURCES = ubi-utils/libubi.c ubi-utils/libubi_int.h libubi_a_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/ubi-utils/include libubigen_a_SOURCES = ubi-utils/libubigen.c diff --git a/ubi-utils/include/ubiutils-common.h b/ubi-utils/include/ubiutils-common.h deleted file mode 100644 index 99c18f7..0000000 --- a/ubi-utils/include/ubiutils-common.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) Artem Bityutskiy, 2007, 2008 - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See - * the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -#ifndef __UBI_UTILS_COMMON_H__ -#define __UBI_UTILS_COMMON_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -long long ubiutils_get_bytes(const char *str); -void ubiutils_print_bytes(long long bytes, int bracket); -int ubiutils_srand(void); - -#ifdef __cplusplus -} -#endif - -#endif /* !__UBI_UTILS_COMMON_H__ */ diff --git a/ubi-utils/mtdinfo.c b/ubi-utils/mtdinfo.c index a86abd1..11e59c1 100644 --- a/ubi-utils/mtdinfo.c +++ b/ubi-utils/mtdinfo.c @@ -34,7 +34,6 @@ #include #include #include "common.h" -#include "ubiutils-common.h" /* The variables below are set by command line arguments */ struct args { @@ -172,7 +171,7 @@ static void print_ubi_info(const struct mtd_info *mtd_info, printf("Default UBI VID header offset: %d\n", ui.vid_hdr_offs); printf("Default UBI data offset: %d\n", ui.data_offs); printf("Default UBI LEB size: "); - ubiutils_print_bytes(ui.leb_size, 0); + util_print_bytes(ui.leb_size, 0); printf("\n"); printf("Maximum UBI volumes count: %d\n", ui.max_volumes); } @@ -306,10 +305,10 @@ static int print_dev_info(libmtd_t libmtd, const struct mtd_info *mtd_info, int printf("Name: %s\n", mtd.name); printf("Type: %s\n", mtd.type_str); printf("Eraseblock size: "); - ubiutils_print_bytes(mtd.eb_size, 0); + util_print_bytes(mtd.eb_size, 0); printf("\n"); printf("Amount of eraseblocks: %d (", mtd.eb_cnt); - ubiutils_print_bytes(mtd.size, 0); + util_print_bytes(mtd.size, 0); printf(")\n"); printf("Minimum input/output unit size: %d %s\n", mtd.min_io_size, mtd.min_io_size > 1 ? "bytes" : "byte"); diff --git a/ubi-utils/ubiattach.c b/ubi-utils/ubiattach.c index a7c62d0..09f85af 100644 --- a/ubi-utils/ubiattach.c +++ b/ubi-utils/ubiattach.c @@ -31,7 +31,6 @@ #include #include "common.h" -#include "ubiutils-common.h" #define DEFAULT_CTRL_DEV "/dev/ubi_ctrl" @@ -238,11 +237,11 @@ int main(int argc, char * const argv[]) } printf("UBI device number %d, total %d LEBs (", dev_info.dev_num, dev_info.total_lebs); - ubiutils_print_bytes(dev_info.total_bytes, 0); + util_print_bytes(dev_info.total_bytes, 0); printf("), available %d LEBs (", dev_info.avail_lebs); - ubiutils_print_bytes(dev_info.avail_bytes, 0); + util_print_bytes(dev_info.avail_bytes, 0); printf("), LEB size "); - ubiutils_print_bytes(dev_info.leb_size, 1); + util_print_bytes(dev_info.leb_size, 1); printf("\n"); libubi_close(libubi); diff --git a/ubi-utils/ubiformat.c b/ubi-utils/ubiformat.c index 21409ca..68906f2 100644 --- a/ubi-utils/ubiformat.c +++ b/ubi-utils/ubiformat.c @@ -45,7 +45,6 @@ #include #include #include "common.h" -#include "ubiutils-common.h" /* The variables below are set by command line arguments */ struct args { @@ -130,7 +129,7 @@ static const struct option long_options[] = { static int parse_opt(int argc, char * const argv[]) { - ubiutils_srand(); + util_srand(); args.image_seq = rand(); while (1) { @@ -143,7 +142,7 @@ static int parse_opt(int argc, char * const argv[]) switch (key) { case 's': - args.subpage_size = ubiutils_get_bytes(optarg); + args.subpage_size = util_get_bytes(optarg); if (args.subpage_size <= 0) return errmsg("bad sub-page size: \"%s\"", optarg); if (!is_power_of_2(args.subpage_size)) @@ -170,7 +169,7 @@ static int parse_opt(int argc, char * const argv[]) break; case 'S': - args.image_sz = ubiutils_get_bytes(optarg); + args.image_sz = util_get_bytes(optarg); if (args.image_sz <= 0) return errmsg("bad image-size: \"%s\"", optarg); break; @@ -791,9 +790,9 @@ int main(int argc, char * const argv[]) if (!args.quiet) { normsg_cont("mtd%d (%s), size ", mtd.mtd_num, mtd.type_str); - ubiutils_print_bytes(mtd.size, 1); + util_print_bytes(mtd.size, 1); printf(", %d eraseblocks of ", mtd.eb_cnt); - ubiutils_print_bytes(mtd.eb_size, 1); + util_print_bytes(mtd.eb_size, 1); printf(", min. I/O size %d bytes\n", mtd.min_io_size); } diff --git a/ubi-utils/ubimkvol.c b/ubi-utils/ubimkvol.c index 7c2a234..fdbc67f 100644 --- a/ubi-utils/ubimkvol.c +++ b/ubi-utils/ubimkvol.c @@ -33,7 +33,6 @@ #include #include "common.h" -#include "ubiutils-common.h" /* The variables below are set by command line arguments */ struct args { @@ -137,7 +136,7 @@ static int parse_opt(int argc, char * const argv[]) break; case 's': - args.bytes = ubiutils_get_bytes(optarg); + args.bytes = util_get_bytes(optarg); if (args.bytes <= 0) return errmsg("bad volume size: \"%s\"", optarg); break; @@ -278,9 +277,9 @@ int main(int argc, char * const argv[]) } printf("Volume ID %d, size %d LEBs (", vol_info.vol_id, vol_info.rsvd_lebs); - ubiutils_print_bytes(vol_info.rsvd_bytes, 0); + util_print_bytes(vol_info.rsvd_bytes, 0); printf("), LEB size "); - ubiutils_print_bytes(vol_info.leb_size, 1); + util_print_bytes(vol_info.leb_size, 1); printf(", %s, name \"%s\", alignment %d\n", req.vol_type == UBI_DYNAMIC_VOLUME ? "dynamic" : "static", vol_info.name, vol_info.alignment); diff --git a/ubi-utils/ubinfo.c b/ubi-utils/ubinfo.c index cb88f53..82d4f18 100644 --- a/ubi-utils/ubinfo.c +++ b/ubi-utils/ubinfo.c @@ -31,7 +31,6 @@ #include #include "common.h" -#include "ubiutils-common.h" /* The variables below are set by command line arguments */ struct args { @@ -211,12 +210,12 @@ static int print_vol_info(libubi_t libubi, int dev_num, int vol_id) printf("Alignment: %d\n", vol_info.alignment); printf("Size: %d LEBs (", vol_info.rsvd_lebs); - ubiutils_print_bytes(vol_info.rsvd_bytes, 0); + util_print_bytes(vol_info.rsvd_bytes, 0); printf(")\n"); if (vol_info.type == UBI_STATIC_VOLUME) { printf("Data bytes: "); - ubiutils_print_bytes(vol_info.data_bytes, 1); + util_print_bytes(vol_info.data_bytes, 1); printf("\n"); } printf("State: %s\n", vol_info.corrupted ? "corrupted" : "OK"); @@ -240,15 +239,15 @@ static int print_dev_info(libubi_t libubi, int dev_num, int all) printf("ubi%d\n", dev_info.dev_num); printf("Volumes count: %d\n", dev_info.vol_count); printf("Logical eraseblock size: "); - ubiutils_print_bytes(dev_info.leb_size, 0); + util_print_bytes(dev_info.leb_size, 0); printf("\n"); printf("Total amount of logical eraseblocks: %d (", dev_info.total_lebs); - ubiutils_print_bytes(dev_info.total_bytes, 0); + util_print_bytes(dev_info.total_bytes, 0); printf(")\n"); printf("Amount of available logical eraseblocks: %d (", dev_info.avail_lebs); - ubiutils_print_bytes(dev_info.avail_bytes, 0); + util_print_bytes(dev_info.avail_bytes, 0); printf(")\n"); printf("Maximum count of volumes %d\n", dev_info.max_vol_count); diff --git a/ubi-utils/ubinize.c b/ubi-utils/ubinize.c index b5ebadc..c85ff9b 100644 --- a/ubi-utils/ubinize.c +++ b/ubi-utils/ubinize.c @@ -37,7 +37,6 @@ #include #include #include "common.h" -#include "ubiutils-common.h" static const char optionsstr[] = "-o, --output= output file name\n" @@ -110,7 +109,7 @@ static struct args args = { static int parse_opt(int argc, char * const argv[]) { - ubiutils_srand(); + util_srand(); args.image_seq = rand(); while (1) { @@ -131,13 +130,13 @@ static int parse_opt(int argc, char * const argv[]) break; case 'p': - args.peb_size = ubiutils_get_bytes(optarg); + args.peb_size = util_get_bytes(optarg); if (args.peb_size <= 0) return errmsg("bad physical eraseblock size: \"%s\"", optarg); break; case 'm': - args.min_io_size = ubiutils_get_bytes(optarg); + args.min_io_size = util_get_bytes(optarg); if (args.min_io_size <= 0) return errmsg("bad min. I/O unit size: \"%s\"", optarg); if (!is_power_of_2(args.min_io_size)) @@ -145,7 +144,7 @@ static int parse_opt(int argc, char * const argv[]) break; case 's': - args.subpage_size = ubiutils_get_bytes(optarg); + args.subpage_size = util_get_bytes(optarg); if (args.subpage_size <= 0) return errmsg("bad sub-page size: \"%s\"", optarg); if (!is_power_of_2(args.subpage_size)) @@ -324,7 +323,7 @@ static int read_section(const struct ubigen_info *ui, const char *sname, sprintf(buf, "%s:vol_size", sname); p = iniparser_getstring(args.dict, buf, NULL); if (p) { - vi->bytes = ubiutils_get_bytes(p); + vi->bytes = util_get_bytes(p); if (vi->bytes <= 0) return errmsg("bad \"vol_size\" key value \"%s\" (section \"%s\")", p, sname); @@ -353,7 +352,7 @@ static int read_section(const struct ubigen_info *ui, const char *sname, normsg_cont("volume size was not specified in section \"%s\", assume" " minimum to fit image \"%s\"", sname, *img); - ubiutils_print_bytes(vi->bytes, 1); + util_print_bytes(vi->bytes, 1); printf("\n"); } diff --git a/ubi-utils/ubirsvol.c b/ubi-utils/ubirsvol.c index c469060..69a4ea1 100644 --- a/ubi-utils/ubirsvol.c +++ b/ubi-utils/ubirsvol.c @@ -33,7 +33,6 @@ #include #include "common.h" -#include "ubiutils-common.h" /* The variables below are set by command line arguments */ struct args { @@ -114,7 +113,7 @@ static int parse_opt(int argc, char * const argv[]) switch (key) { case 's': - args.bytes = ubiutils_get_bytes(optarg); + args.bytes = util_get_bytes(optarg); if (args.bytes <= 0) return errmsg("bad volume size: \"%s\"", optarg); break; diff --git a/ubi-utils/ubiutils-common.c b/ubi-utils/ubiutils-common.c deleted file mode 100644 index 5636785..0000000 --- a/ubi-utils/ubiutils-common.c +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright (C) 2007, 2008 Nokia Corporation - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See - * the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -/* - * This file contains various common stuff used by UBI utilities. - * - * Authors: Artem Bityutskiy - * Adrian Hunter - */ - -#define PROGRAM_NAME "ubiutils" - -#include -#include -#include -#include -#include -#include -#include -#include "common.h" - -/** - * get_multiplier - convert size specifier to an integer multiplier. - * @str: the size specifier string - * - * This function parses the @str size specifier, which may be one of - * 'KiB', 'MiB', or 'GiB' into an integer multiplier. Returns positive - * size multiplier in case of success and %-1 in case of failure. - */ -static int get_multiplier(const char *str) -{ - if (!str) - return 1; - - /* Remove spaces before the specifier */ - while (*str == ' ' || *str == '\t') - str += 1; - - if (!strcmp(str, "KiB")) - return 1024; - if (!strcmp(str, "MiB")) - return 1024 * 1024; - if (!strcmp(str, "GiB")) - return 1024 * 1024 * 1024; - - return -1; -} - -/** - * ubiutils_get_bytes - convert a string containing amount of bytes into an - * integer - * @str: string to convert - * - * This function parses @str which may have one of 'KiB', 'MiB', or 'GiB' - * size specifiers. Returns positive amount of bytes in case of success and %-1 - * in case of failure. - */ -long long ubiutils_get_bytes(const char *str) -{ - char *endp; - long long bytes = strtoull(str, &endp, 0); - - if (endp == str || bytes < 0) { - fprintf(stderr, "incorrect amount of bytes: \"%s\"\n", str); - return -1; - } - - if (*endp != '\0') { - int mult = get_multiplier(endp); - - if (mult == -1) { - fprintf(stderr, "bad size specifier: \"%s\" - " - "should be 'KiB', 'MiB' or 'GiB'\n", endp); - return -1; - } - bytes *= mult; - } - - return bytes; -} - -/** - * ubiutils_print_bytes - print bytes. - * @bytes: variable to print - * @bracket: whether brackets have to be put or not - * - * This is a helper function which prints amount of bytes in a human-readable - * form, i.e., it prints the exact amount of bytes following by the approximate - * amount of Kilobytes, Megabytes, or Gigabytes, depending on how big @bytes - * is. - */ -void ubiutils_print_bytes(long long bytes, int bracket) -{ - const char *p; - - if (bracket) - p = " ("; - else - p = ", "; - - printf("%lld bytes", bytes); - - if (bytes > 1024 * 1024 * 1024) - printf("%s%.1f GiB", p, (double)bytes / (1024 * 1024 * 1024)); - else if (bytes > 1024 * 1024) - printf("%s%.1f MiB", p, (double)bytes / (1024 * 1024)); - else if (bytes > 1024 && bytes != 0) - printf("%s%.1f KiB", p, (double)bytes / 1024); - else - return; - - if (bracket) - printf(")"); -} - -/** - * ubiutils_srand - randomly seed the standard pseudo-random generator. - * - * This helper function seeds the standard libc pseudo-random generator with a - * more or less random value to make sure the 'rand()' call does not return the - * same sequence every time UBI utilities run. Returns zero in case of success - * and a %-1 in case of error. - */ -int ubiutils_srand(void) -{ - struct timeval tv; - struct timezone tz; - unsigned int seed; - - /* - * Just assume that a combination of the PID + current time is a - * reasonably random number. - */ - if (gettimeofday(&tv, &tz)) - return -1; - - seed = (unsigned int)tv.tv_sec; - seed += (unsigned int)tv.tv_usec; - seed *= getpid(); - seed %= RAND_MAX; - srand(seed); - return 0; -} -- cgit v1.2.3