From 7bd1747ad5a5c2a53e465b565d879ee47fa16415 Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Fri, 18 Jan 2008 11:45:36 +0200 Subject: ubi-utils: tweak ubigen Make ubigen consistent with other UBI utilities Signed-off-by: Artem Bityutskiy --- ubi-utils/src/ubigen.c | 516 +++++++++++++++++++++++-------------------------- 1 file changed, 238 insertions(+), 278 deletions(-) (limited to 'ubi-utils/src') diff --git a/ubi-utils/src/ubigen.c b/ubi-utils/src/ubigen.c index 9fcafab..3b1fc0e 100644 --- a/ubi-utils/src/ubigen.c +++ b/ubi-utils/src/ubigen.c @@ -14,312 +14,283 @@ * 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. + */ + +/* + * An utility to generate UBI images. * - * Author: Oliver Lohmann - * - * Tool to add UBI headers to binary images. - * - * 1.0 Initial version - * 1.1 Different CRC32 start value - * 1.2 Removed argp because we want to use uClibc. - * 1.3 Minor cleanups + * Authors: Oliver Lohmann + * Artem Bityutskiy */ #include -#include #include +#include #include #include #include #include #include "ubigen.h" -#include "config.h" - -#define PROGRAM_VERSION "1.3" +#include "common.h" + +#define PROGRAM_VERSION "1.4" +#define PROGRAM_NAME "ubigen" + +struct args { + FILE *fp_in; + FILE *fp_out; + int peb_size; + int id; + int min_io_size; + int type; + int sub_page_size; + int alignment; + int vid_hdr_offs; + int ec; + int ubi_ver; +}; -typedef enum action_t { - ACT_NORMAL = 0x00000001, - ACT_BROKEN_UPDATE = 0x00000002, -} action_t; +struct args args = { + .fp_in = NULL, + .fp_out = NULL, + .peb_size = -1, + .id = -1, + .min_io_size = -1, + .type = UBI_VID_DYNAMIC, + .sub_page_size = -1, + .alignment = 1, + .vid_hdr_offs = 0, + .ec = 0, + .ubi_ver = 0, +}; -static char doc[] = "\nVersion: " PROGRAM_VERSION "\n" - "ubigen - a tool for adding UBI information to a binary input file.\n"; +static const char *doc = PROGRAM_NAME " version " PROGRAM_VERSION +" - a tool for adding UBI headers to a binary image.\n" +"Note, the images generated by this program are not ready to be used\n" +"because they do not contain the volume table. If not sure about one\n" +"of the parameters, do not specify and let the utility to use default\n" +"values."; static const char *optionsstr = -" Common settings:\n" -" -c, --copyright Print copyright information.\n" -" -d, --debug\n" -" -v, --verbose Print more progress information.\n" -"\n" -" UBI Settings:\n" -" -A, --alignment= Set the alignment size to (default 1).\n" -" Values can be specified as bytes, 'ki' or 'Mi'.\n" -" -B, --blocksize= Set the eraseblock size to (default 128\n" -" KiB).\n" -" Values can be specified as bytes, 'ki' or 'Mi'.\n" -" -E, --erasecount= Set the erase count to (default 0)\n" -" -I, --id= The UBI volume id.\n" -" -O, --offset= Offset from start of an erase block to the UBI\n" -" volume header.\n" -" -T, --type= The UBI volume type:\n" -" 1 = dynamic, 2 = static\n" -" -X, --setver= Set UBI version number to (default 1)\n" -"\n" -" Input/Output:\n" -" -i, --infile= Read input from file.\n" -" -o, --outfile= Write output to file (default is stdout).\n" -"\n" -" Special options:\n" -" -U, --broken-update= Create an ubi image which simulates a broken\n" -" update.\n" -" specifies the logical eraseblock number to\n" -" update.\n" -"\n" -" -?, --help Give this help list\n" -" --usage Give a short usage message\n" -" -V, --version Print program version\n"; +"-i, --infile= the input file\n" +"-o, --outfile= the output file (default is stdout)\n" +"-b, --peb-size= size of the physical eraseblock of the flash this\n" +" UBI image is created for in bytes, kilobytes (KiB),\n" +" or megabytes (MiB) (mandatory parameter)\n" +"-I, --vol-id= volume ID (mandatory parameter)\n" +"-m, --min-io-size= minimum input/output unit size of the flash in bytes\n" +" kilobytes (KiB), or megabytes (MiB) (mandatory\n" +" parameter); e.g. this is NAND page size in case of\n" +" NAND flash\n" +"-t, --type= volume type: dynamic or static (default is dynamic)\n" +"-s, --sub-page-size= minimum input/output unit used for UBI headers, e.g.\n" +" sub-page size in case of NAND flash (equivalent to\n" +" the minimum input/output unit size by default)\n" +"-a, --alignment= volume alignment in bytes, kilobytes (KiB), or\n" +" megabytes (MiB) (default is 1)\n" +"-O, --vid-hdr-offset= offset if the VID header from start of the physical\n" +" eraseblock (default is the second minimum I/O unit\n" +" or sub-page, if it was specified)\n" +"-e, --erase-counter= the erase counter value to put to EC headers\n" +" (default is 0)\n" +"-x, --ubi-ver= UBI version number to put to EC headers\n" +" (default is 1)\n" +"-h, --help print help message\n" +"-V, --version print program version"; static const char *usage = -"Usage: ubigen [-cdv?V] [-A ] [-B ] [-E ] [-I ]\n" -" [-O ] [-T ] [-X ] [-i ] [-o ]\n" -" [-U ] [--copyright] [--debug] [--verbose] [--alignment=]\n" -" [--blocksize=] [--erasecount=] [--id=]\n" -" [--offset=] [--type=] [--setver=]\n" -" [--infile=] [--outfile=]\n" -" [--broken-update=] [--help] [--usage] [--version]\n"; +"Usage: " PROGRAM_NAME " -i -o -b \n" +" -I -m [-s ]\n" +" [-a ] [-O ]\n" +" [-e ] [-x ] [-h] [-V]"; struct option long_options[] = { - { .name = "copyright", .has_arg = 0, .flag = NULL, .val = 'c' }, - { .name = "debug", .has_arg = 0, .flag = NULL, .val = 'd' }, - { .name = "verbose", .has_arg = 0, .flag = NULL, .val = 'v' }, - { .name = "alignment", .has_arg = 1, .flag = NULL, .val = 'A' }, - { .name = "blocksize", .has_arg = 1, .flag = NULL, .val = 'B' }, - { .name = "erasecount", .has_arg = 1, .flag = NULL, .val = 'E' }, - { .name = "id", .has_arg = 1, .flag = NULL, .val = 'I' }, - { .name = "offset", .has_arg = 1, .flag = NULL, .val = 'O' }, - { .name = "type", .has_arg = 1, .flag = NULL, .val = 'T' }, - { .name = "setver", .has_arg = 1, .flag = NULL, .val = 'X' }, - { .name = "infile", .has_arg = 1, .flag = NULL, .val = 'i' }, - { .name = "outfile", .has_arg = 1, .flag = NULL, .val = 'o' }, - { .name = "broken-update", .has_arg = 1, .flag = NULL, .val = 'U' }, - { .name = "help", .has_arg = 0, .flag = NULL, .val = '?' }, - { .name = "usage", .has_arg = 0, .flag = NULL, .val = 0 }, - { .name = "version", .has_arg = 0, .flag = NULL, .val = 'V' }, + { .name = "infile", .has_arg = 1, .flag = NULL, .val = 'i' }, + { .name = "outfile", .has_arg = 1, .flag = NULL, .val = 'o' }, + { .name = "peb-size", .has_arg = 1, .flag = NULL, .val = 'b' }, + { .name = "vol-id", .has_arg = 1, .flag = NULL, .val = 'I' }, + { .name = "min-io-size", .has_arg = 1, .flag = NULL, .val = 'm' }, + { .name = "type", .has_arg = 1, .flag = NULL, .val = 't' }, + { .name = "sub-page-size", .has_arg = 1, .flag = NULL, .val = 's' }, + { .name = "alignment", .has_arg = 1, .flag = NULL, .val = 'a' }, + { .name = "vid-hdr-offset", .has_arg = 1, .flag = NULL, .val = 'O' }, + { .name = "erase-counter", .has_arg = 1, .flag = NULL, .val = 'e' }, + { .name = "ubi-ver", .has_arg = 1, .flag = NULL, .val = 'x' }, + { .name = "help", .has_arg = 0, .flag = NULL, .val = 'h' }, + { .name = "version", .has_arg = 0, .flag = NULL, .val = 'V' }, { NULL, 0, NULL, 0} }; -static const char copyright [] __attribute__((unused)) = - "Copyright IBM Corp 2006"; - -#define CHECK_ENDP(option, endp) do { \ - if (*endp) { \ - fprintf(stderr, \ - "Parse error option \'%s\'. " \ - "No correct numeric value.\n" \ - , option); \ - exit(EXIT_FAILURE); \ - } \ -} while(0) - -typedef struct myargs { - /* common settings */ - action_t action; - int verbose; - - int32_t id; - uint8_t type; - uint32_t eb_size; - uint64_t ec; - uint8_t version; - uint32_t hdr_offset; - uint32_t update_block; - uint32_t alignment; - - FILE* fp_in; - FILE* fp_out; - - /* special stuff needed to get additional arguments */ - char *arg1; - char **options; /* [STRING...] */ -} myargs; - - -static int ustrtoul(const char *cp, char **endp, unsigned int base) +static int parse_opt(int argc, char * const argv[]) { - unsigned long result = strtoul(cp, endp, base); - - switch (**endp) { - case 'G': - result *= 1024; - case 'M': - result *= 1024; - case 'k': - case 'K': - result *= 1024; - /* "Ki", "ki", "Mi" or "Gi" are to be used. */ - if ((*endp)[1] == 'i') - (*endp) += 2; - } - return result; -} - -static int -parse_opt(int argc, char **argv, myargs *args) -{ - int err = 0; - char* endp; - while (1) { int key; + char *endp; - key = getopt_long(argc, argv, "cdvA:B:E:I:O:T:X:i:o:U:?V", - long_options, NULL); + key = getopt_long(argc, argv, "i:o:b:I:m:t:s:a:O:e:x:hV", + long_options, NULL); if (key == -1) break; switch (key) { - case 'c': - fprintf(stderr, "%s\n", copyright); - exit(0); - break; - case 'o': /* output */ - args->fp_out = fopen(optarg, "wb"); - if ((args->fp_out) == NULL) { - fprintf(stderr, "Cannot open file %s " - "for output\n", optarg); - exit(1); + case 'o': + args.fp_out = fopen(optarg, "wb"); + if (!args.fp_out) { + errmsg("cannot open file \"%s\"", optarg); + return -1; + } + break; + + case 'i': + args.fp_in = fopen(optarg, "rb"); + if (!args.fp_in) { + errmsg("cannot open file \"%s\"", optarg); + return -1; + } + break; + + case 'b': + args.peb_size = strtoull(optarg, &endp, 0); + if (endp == optarg || args.peb_size <= 0) { + errmsg("bad physical eraseblock size: \"%s\"", optarg); + return -1; + } + if (*endp != '\0') { + int mult = ubiutils_get_multiplier(endp); + + if (mult == -1) { + errmsg("bad size specifier: \"%s\" - " + "should be 'KiB', 'MiB' or 'GiB'", endp); + return -1; } - break; - case 'i': /* input */ - args->fp_in = fopen(optarg, "rb"); - if ((args->fp_in) == NULL) { - fprintf(stderr, "Cannot open file %s " - "for input\n", optarg); - exit(1); + args.peb_size *= mult; + } + + case 'm': + args.min_io_size = strtoull(optarg, &endp, 0); + if (endp == optarg || args.min_io_size <= 0) { + errmsg("bad min. I/O unit size: \"%s\"", optarg); + return -1; + } + if (*endp != '\0') { + int mult = ubiutils_get_multiplier(endp); + + if (mult == -1) { + errmsg("bad size specifier: \"%s\" - " + "should be 'KiB', 'MiB' or 'GiB'", endp); + return -1; } - break; - case 'v': /* verbose */ - args->verbose = 1; - break; - - case 'B': /* eb_size */ - args->eb_size = - (uint32_t)ustrtoul(optarg, &endp, 0); - CHECK_ENDP("B", endp); - break; - case 'E': /* erasecount */ - args->ec = (uint64_t)strtoul(optarg, &endp, 0); - CHECK_ENDP("E", endp); - break; - case 'I': /* id */ - args->id = (uint16_t)strtoul(optarg, &endp, 0); - CHECK_ENDP("I", endp); - break; - case 'T': /* type */ - args->type = - (uint16_t)strtoul(optarg, &endp, 0); - CHECK_ENDP("T", endp); - break; - case 'X': /* versionnr */ - args->version = - (uint8_t)strtoul(optarg, &endp, 0); - CHECK_ENDP("X", endp); - break; - case 'O': /* offset for volume hdr */ - args->hdr_offset = - (uint32_t) strtoul(optarg, &endp, 0); - CHECK_ENDP("O", endp); - break; - - case 'U': /* broken update */ - args->action = ACT_BROKEN_UPDATE; - args->update_block = - (uint32_t) strtoul(optarg, &endp, 0); - CHECK_ENDP("U", endp); - break; - - case '?': /* help */ - fprintf(stderr, "Usage: ubigen [OPTION...]\n"); - fprintf(stderr, "%s", doc); - fprintf(stderr, "%s", optionsstr); - fprintf(stderr, "\nReport bugs to %s\n", - PACKAGE_BUGREPORT); - exit(0); - break; - - case 'V': - fprintf(stderr, "%s\n", PROGRAM_VERSION); - exit(0); - break; - - default: - fprintf(stderr, "%s", usage); - exit(-1); - } - } + args.min_io_size *= mult; + } + + case 'e': + args.ec = strtoul(optarg, &endp, 0); + if (endp == optarg || args.ec < 0) { + errmsg("bad erase counter value: \"%s\"", optarg); + return -1; + } + break; + + case 'I': + args.id = strtoul(optarg, &endp, 0); + if (endp == optarg || args.id < 0) { + errmsg("bad volume ID: \"%s\"", optarg); + return -1; + } + break; - if (optind < argc) { - if (!args->fp_in) { - args->fp_in = fopen(argv[optind++], "rb"); - if ((args->fp_in) == NULL) { - fprintf(stderr, "Cannot open file %s for " - "input\n", argv[optind]); - exit(1); + case 't': + if (!strcmp(optarg, "dynamic")) + args.type = UBI_VID_DYNAMIC; + else if (!strcmp(optarg, "static")) + args.type = UBI_VID_STATIC; + else { + errmsg("bad volume type: \"%s\"", optarg); + return -1; } + break; + + case 'x': + args.ubi_ver = strtoul(optarg, &endp, 0); + if (endp == optarg || args.ubi_ver < 0) { + errmsg("bad UBI version: \"%s\"", optarg); + return -1; + } + break; + + case 'O': + args.vid_hdr_offs = strtoul(optarg, &endp, 0); + if (endp == optarg || args.vid_hdr_offs < 0) { + errmsg("bad VID header offset: \"%s\"", optarg); + return -1; + } + break; + + case 'h': + fprintf(stderr, "%s\n\n", doc); + fprintf(stderr, "%s\n\n", usage); + fprintf(stderr, "%s\n", optionsstr); + exit(EXIT_SUCCESS); + + case 'V': + fprintf(stderr, "%s\n", PROGRAM_VERSION); + exit(EXIT_SUCCESS); + + case ':': + errmsg("parameter is missing"); + return -1; + + default: + fprintf(stderr, "Use -h for help\n"); + return -1; } } - if (args->id < 0) { - err = 1; - fprintf(stderr, - "Please specify an UBI Volume ID.\n"); + + if (!args.fp_in) { + errmsg("input file was not specified (use -h for help)"); + return -1; + } + + if (!args.fp_out) + args.fp_out = stdout; + + if (args.id < 0) { + errmsg("wolume ID was not specified (use -h for help)"); + return -1; } - if (args->type == 0) { - err = 1; - fprintf(stderr, - "Please specify an UBI Volume type.\n"); + + if (args.peb_size < 0) { + errmsg("physical eraseblock size was not specified " + "(use -h for help)"); + return -1; } - if (err) { - fprintf(stderr, "%s", usage); - exit(1); + + if (args.min_io_size < 0) { + errmsg("min. I/O unit size was not specified " + "(use -h for help)"); + return -1; } + if (args.sub_page_size < 0) + args.sub_page_size = args.min_io_size; + return 0; } -int -main(int argc, char **argv) +int main(int argc, char * const argv[]) { - int rc = 0; + int err; ubi_info_t u; struct stat file_info; off_t input_len = 0; /* only used in static volumes */ - myargs args = { - .action = ACT_NORMAL, - .verbose = 0, - - .id = -1, - .type = 0, - .eb_size = 0, - .update_block = 0, - .ec = 0, - .version = 0, - .hdr_offset = (DEFAULT_PAGESIZE) - (UBI_VID_HDR_SIZE), - .alignment = 1, + ubigen_init(); - .fp_in = NULL, - .fp_out = stdout, - /* arguments */ - .arg1 = NULL, - .options = NULL, - }; - - ubigen_init(); /* Init CRC32 table in ubigen */ - - /* parse arguments */ - parse_opt(argc, argv, &args); + err = parse_opt(argc, argv); + if (err) + return -1; if (fstat(fileno(args.fp_in), &file_info) != 0) { fprintf(stderr, "Cannot fetch file size " @@ -327,33 +298,22 @@ main(int argc, char **argv) } input_len = file_info.st_size; - rc = ubigen_create(&u, (uint32_t)args.id, args.type, - args.eb_size, args.ec, args.alignment, - args.version, args.hdr_offset, 0 ,input_len, + err = ubigen_create(&u, (uint32_t)args.id, args.type, + args.peb_size, args.ec, args.alignment, + args.ubi_ver, args.vid_hdr_offs, 0 ,input_len, args.fp_in, args.fp_out); - if (rc != 0) { - fprintf(stderr, "Cannot create UBI info handler rc: %d\n", rc); - exit(EXIT_FAILURE); + if (err) { + fprintf(stderr, "Cannot create UBI info handler err: %d\n", err); + return -1; } - if (!args.fp_in || !args.fp_out) { - fprintf(stderr, "Input/Output error.\n"); - exit(EXIT_FAILURE); - - } - - if (args.action & ACT_NORMAL) { - rc = ubigen_write_complete(u); - } - else if (args.action & ACT_BROKEN_UPDATE) { - rc = ubigen_write_broken_update(u, args.update_block); - } - if (rc != 0) { + err = ubigen_write_complete(u); + if (err != 0) { fprintf(stderr, "Error converting input data.\n"); - exit(EXIT_FAILURE); + return -1; } - rc = ubigen_destroy(&u); - return rc; + err = ubigen_destroy(&u); + return err; } -- cgit v1.2.3