2011-06-27 15:58:21 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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
|
2015-02-09 00:26:29 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-06-27 15:58:21 +08:00
|
|
|
*/
|
|
|
|
|
2011-06-27 14:39:51 +08:00
|
|
|
#include <confuse.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
2011-06-27 15:38:33 +08:00
|
|
|
#include <getopt.h>
|
2011-06-27 14:39:51 +08:00
|
|
|
|
|
|
|
#include "genimage.h"
|
|
|
|
|
|
|
|
static LIST_HEAD(optlist);
|
|
|
|
|
|
|
|
struct config {
|
|
|
|
const char *name;
|
|
|
|
cfg_opt_t opt;
|
|
|
|
const char *env;
|
|
|
|
char cmdlineopt;
|
|
|
|
struct list_head list;
|
|
|
|
char *value;
|
|
|
|
char *def;
|
|
|
|
};
|
|
|
|
|
2011-06-27 15:38:33 +08:00
|
|
|
/*
|
|
|
|
* get the value of an option
|
|
|
|
*/
|
2011-06-27 14:39:51 +08:00
|
|
|
const char *get_opt(const char *name)
|
|
|
|
{
|
|
|
|
struct config *c;
|
|
|
|
|
|
|
|
list_for_each_entry(c, &optlist, list) {
|
|
|
|
if (!strcmp(c->name, name))
|
|
|
|
return c->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-06-27 15:38:33 +08:00
|
|
|
/*
|
|
|
|
* set option 'name' to 'value'
|
|
|
|
*/
|
2011-10-10 22:38:11 +08:00
|
|
|
static int set_opt(const char *name, const char *value)
|
2011-06-27 15:38:33 +08:00
|
|
|
{
|
|
|
|
struct config *c;
|
|
|
|
|
|
|
|
list_for_each_entry(c, &optlist, list) {
|
|
|
|
if (!strcmp(c->name, name)) {
|
|
|
|
free(c->value);
|
|
|
|
c->value = strdup(value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new option
|
|
|
|
*
|
|
|
|
* name: name of the option
|
|
|
|
* env: environment variable this option corresponds to
|
|
|
|
* opt: confuse option
|
|
|
|
* def: default value
|
|
|
|
*/
|
2011-10-10 22:38:11 +08:00
|
|
|
static int add_opt(const char *name, const char *env, cfg_opt_t *opt, char *def)
|
2011-06-27 14:39:51 +08:00
|
|
|
{
|
|
|
|
struct config *c = xzalloc(sizeof(*c));
|
|
|
|
|
2015-10-12 00:39:38 +08:00
|
|
|
c->name = name;
|
2011-06-27 14:39:51 +08:00
|
|
|
c->env = env;
|
|
|
|
c->def = def;
|
|
|
|
memcpy(&c->opt, opt, sizeof(cfg_opt_t));
|
|
|
|
|
|
|
|
list_add_tail(&c->list, &optlist);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-27 15:38:33 +08:00
|
|
|
/*
|
|
|
|
* convert all options from the options list to a cfg_opt_t
|
|
|
|
* array suitable for confuse
|
|
|
|
*/
|
|
|
|
cfg_opt_t *get_confuse_opts(void)
|
2011-06-27 14:39:51 +08:00
|
|
|
{
|
|
|
|
struct config *c;
|
|
|
|
int num_opts = 0;;
|
|
|
|
cfg_opt_t *opts;
|
|
|
|
int i = 0;
|
|
|
|
cfg_opt_t cfg_end[] = {
|
|
|
|
CFG_END()
|
|
|
|
};
|
|
|
|
|
2011-06-27 15:38:33 +08:00
|
|
|
list_for_each_entry(c, &optlist, list) {
|
|
|
|
if (c->opt.name)
|
|
|
|
num_opts++;
|
|
|
|
}
|
2011-06-27 14:39:51 +08:00
|
|
|
|
|
|
|
opts = xzalloc(sizeof(cfg_opt_t) * (num_opts + 1));
|
|
|
|
|
|
|
|
list_for_each_entry(c, &optlist, list) {
|
2011-06-27 15:38:33 +08:00
|
|
|
if (c->opt.name) {
|
|
|
|
memcpy(&opts[i], &c->opt, sizeof(cfg_opt_t));
|
|
|
|
i++;
|
|
|
|
}
|
2011-06-27 14:39:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&opts[i], cfg_end, sizeof(cfg_opt_t));
|
|
|
|
|
|
|
|
return opts;
|
|
|
|
}
|
|
|
|
|
2011-06-27 15:47:48 +08:00
|
|
|
/*
|
|
|
|
* Get an integer type option from confuse, but with an optional
|
|
|
|
* 'k', 'M', 'G' suffix
|
|
|
|
*/
|
|
|
|
unsigned long long cfg_getint_suffix(cfg_t *sec, const char *name)
|
|
|
|
{
|
|
|
|
const char *str = cfg_getstr(sec, name);
|
|
|
|
unsigned long long val = 0;
|
|
|
|
|
|
|
|
if (str)
|
|
|
|
val = strtoul_suffix(str, NULL, 0);
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2011-06-27 15:38:33 +08:00
|
|
|
/*
|
|
|
|
* Initialize all options in the following order:
|
|
|
|
*
|
|
|
|
* 1) default value
|
|
|
|
* 2) from environment
|
|
|
|
* 3) from config file
|
|
|
|
* 4) from command line switch
|
|
|
|
*
|
|
|
|
* may be called multiple times with cfg == NULL when
|
|
|
|
* config file is not yet available.
|
|
|
|
*/
|
2011-06-27 14:39:51 +08:00
|
|
|
int set_config_opts(int argc, char *argv[], cfg_t *cfg)
|
|
|
|
{
|
|
|
|
struct config *c;
|
2011-06-27 15:38:33 +08:00
|
|
|
cfg_t *cfgsec = NULL;
|
|
|
|
int num_opts = 0, n, i;
|
|
|
|
static struct option *long_options = 0;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (cfg)
|
|
|
|
cfgsec = cfg_getsec(cfg, "config");
|
2011-06-27 14:39:51 +08:00
|
|
|
|
|
|
|
list_for_each_entry(c, &optlist, list) {
|
2011-06-27 15:38:33 +08:00
|
|
|
char *str;
|
|
|
|
|
|
|
|
num_opts++;
|
|
|
|
|
|
|
|
/* set from option default value */
|
|
|
|
if (c->def)
|
|
|
|
set_opt(c->name, c->def);
|
|
|
|
|
|
|
|
/* set from environment */
|
|
|
|
str = getenv(c->env);
|
2011-06-27 14:39:51 +08:00
|
|
|
if (str)
|
2011-06-27 15:38:33 +08:00
|
|
|
set_opt(c->name, str);
|
|
|
|
|
|
|
|
/* set from config file (if already available) */
|
|
|
|
if (cfgsec && c->opt.name) {
|
|
|
|
str = cfg_getstr(cfgsec, c->opt.name);
|
|
|
|
if (str)
|
|
|
|
set_opt(c->name, str);
|
|
|
|
}
|
2011-06-27 14:39:51 +08:00
|
|
|
}
|
|
|
|
|
2011-06-27 15:38:33 +08:00
|
|
|
/* and last but not least from command line switches */
|
|
|
|
|
|
|
|
long_options = xzalloc(sizeof(struct option) * (num_opts + 1));
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
2011-06-27 14:39:51 +08:00
|
|
|
list_for_each_entry(c, &optlist, list) {
|
2011-06-27 15:38:33 +08:00
|
|
|
struct option *o = &long_options[i];
|
|
|
|
o->name = c->name;
|
|
|
|
o->has_arg = 1;
|
|
|
|
i++;
|
2011-06-27 14:39:51 +08:00
|
|
|
}
|
|
|
|
|
2011-06-30 15:56:46 +08:00
|
|
|
optind = 1;
|
2011-06-27 15:38:33 +08:00
|
|
|
while (1) {
|
|
|
|
int option_index = 0;
|
|
|
|
|
|
|
|
n = getopt_long(argc, argv, "",
|
|
|
|
long_options, &option_index);
|
|
|
|
if (n == -1)
|
|
|
|
break;
|
|
|
|
switch (n) {
|
|
|
|
case 0:
|
|
|
|
ret = set_opt(long_options[option_index].name, optarg);
|
|
|
|
if (ret)
|
|
|
|
goto err_out;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto err_out;
|
2011-06-27 14:39:51 +08:00
|
|
|
}
|
|
|
|
}
|
2011-06-27 15:38:33 +08:00
|
|
|
err_out:
|
|
|
|
free(long_options);
|
2011-06-27 14:39:51 +08:00
|
|
|
|
2011-06-27 15:38:33 +08:00
|
|
|
return ret;
|
2011-06-27 14:39:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *imagepath(void)
|
|
|
|
{
|
|
|
|
return get_opt("outputpath");
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *inputpath(void)
|
|
|
|
{
|
|
|
|
return get_opt("inputpath");
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *rootpath(void)
|
|
|
|
{
|
|
|
|
return get_opt("rootpath");
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *tmppath(void)
|
|
|
|
{
|
|
|
|
return get_opt("tmppath");
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct config opts[] = {
|
|
|
|
{
|
2014-04-24 04:18:35 +08:00
|
|
|
.name = "loglevel",
|
|
|
|
.opt = CFG_STR("loglevel", "1", CFGF_NONE),
|
|
|
|
.env = "GENIMAGE_LOGLEVEL",
|
|
|
|
}, {
|
2011-06-27 14:39:51 +08:00
|
|
|
.name = "rootpath",
|
|
|
|
.opt = CFG_STR("rootpath", NULL, CFGF_NONE),
|
2011-06-27 15:38:33 +08:00
|
|
|
.env = "GENIMAGE_ROOTPATH",
|
2011-06-27 14:39:51 +08:00
|
|
|
}, {
|
|
|
|
.name = "tmppath",
|
|
|
|
.opt = CFG_STR("tmppath", NULL, CFGF_NONE),
|
2011-06-27 15:38:33 +08:00
|
|
|
.env = "GENIMAGE_TMPPATH",
|
2011-06-27 14:39:51 +08:00
|
|
|
}, {
|
|
|
|
.name = "inputpath",
|
|
|
|
.opt = CFG_STR("inputpath", NULL, CFGF_NONE),
|
2011-06-27 15:38:33 +08:00
|
|
|
.env = "GENIMAGE_INPUTPATH",
|
2011-06-27 14:39:51 +08:00
|
|
|
}, {
|
|
|
|
.name = "outputpath",
|
|
|
|
.opt = CFG_STR("outputpath", NULL, CFGF_NONE),
|
2011-06-27 15:38:33 +08:00
|
|
|
.env = "GENIMAGE_OUTPUTPATH",
|
2011-06-27 14:39:51 +08:00
|
|
|
}, {
|
2012-09-30 21:18:07 +08:00
|
|
|
.name = "cpio",
|
|
|
|
.opt = CFG_STR("cpio", NULL, CFGF_NONE),
|
|
|
|
.env = "GENIMAGE_CPIO",
|
|
|
|
.def = "cpio",
|
2011-06-27 14:39:51 +08:00
|
|
|
}, {
|
2012-09-30 21:18:07 +08:00
|
|
|
.name = "dd",
|
|
|
|
.opt = CFG_STR("dd", NULL, CFGF_NONE),
|
|
|
|
.env = "GENIMAGE_DD",
|
|
|
|
.def = "dd",
|
2011-06-27 14:39:51 +08:00
|
|
|
}, {
|
2012-09-30 21:18:07 +08:00
|
|
|
.name = "e2fsck",
|
|
|
|
.opt = CFG_STR("e2fsck", NULL, CFGF_NONE),
|
|
|
|
.env = "GENIMAGE_E2FSCK",
|
|
|
|
.def = "e2fsck",
|
2011-06-27 14:39:51 +08:00
|
|
|
}, {
|
|
|
|
.name = "genext2fs",
|
|
|
|
.opt = CFG_STR("genext2fs", NULL, CFGF_NONE),
|
2011-06-27 15:38:33 +08:00
|
|
|
.env = "GENIMAGE_GENEXT2FS",
|
2011-06-27 14:39:51 +08:00
|
|
|
.def = "genext2fs",
|
2012-11-25 06:02:31 +08:00
|
|
|
}, {
|
|
|
|
.name = "genisoimage",
|
|
|
|
.opt = CFG_STR("genisoimage", NULL, CFGF_NONE),
|
|
|
|
.env = "GENIMAGE_GENISOIMAGE",
|
|
|
|
.def = "genisoimage",
|
2011-10-11 17:05:01 +08:00
|
|
|
}, {
|
2012-09-30 21:18:07 +08:00
|
|
|
.name = "mcopy",
|
|
|
|
.opt = CFG_STR("mcopy", NULL, CFGF_NONE),
|
|
|
|
.env = "GENIMAGE_MCOPY",
|
|
|
|
.def = "mcopy",
|
2014-08-12 17:56:56 +08:00
|
|
|
}, {
|
|
|
|
.name = "mmd",
|
|
|
|
.opt = CFG_STR("mmd", NULL, CFGF_NONE),
|
|
|
|
.env = "GENIMAGE_MMD",
|
|
|
|
.def = "mmd",
|
2016-09-06 17:54:49 +08:00
|
|
|
}, {
|
|
|
|
.name = "mkcramfs",
|
|
|
|
.opt = CFG_STR("mkcramfs", NULL, CFGF_NONE),
|
|
|
|
.env = "GENIMAGE_MKCRAMFS",
|
|
|
|
.def = "mkcramfs",
|
2011-10-11 17:05:01 +08:00
|
|
|
}, {
|
2012-09-30 21:18:07 +08:00
|
|
|
.name = "mkdosfs",
|
|
|
|
.opt = CFG_STR("mkdosfs", NULL, CFGF_NONE),
|
|
|
|
.env = "GENIMAGE_MKDOSFS",
|
|
|
|
.def = "mkdosfs",
|
|
|
|
}, {
|
|
|
|
.name = "mkfsjffs2",
|
|
|
|
.opt = CFG_STR("mkfsjffs2", NULL, CFGF_NONE),
|
|
|
|
.env = "GENIMAGE_MKFJFFS2",
|
|
|
|
.def = "mkfs.jffs2",
|
|
|
|
}, {
|
|
|
|
.name = "mkfsubifs",
|
|
|
|
.opt = CFG_STR("mkfsubifs", NULL, CFGF_NONE),
|
|
|
|
.env = "GENIMAGE_MKFSUBIFS",
|
|
|
|
.def = "mkfs.ubifs",
|
2014-02-19 18:16:54 +08:00
|
|
|
}, {
|
|
|
|
.name = "mksquashfs",
|
|
|
|
.opt = CFG_STR("mksquashfs", NULL, CFGF_NONE),
|
|
|
|
.env = "GENIMAGE_MKSQUASHFS",
|
|
|
|
.def = "mksquashfs",
|
2016-08-05 17:51:05 +08:00
|
|
|
}, {
|
|
|
|
.name = "rauc",
|
|
|
|
.opt = CFG_STR("rauc", NULL, CFGF_NONE),
|
|
|
|
.env = "GENIMAGE_RAUC",
|
|
|
|
.def = "rauc",
|
2011-06-27 14:39:51 +08:00
|
|
|
}, {
|
|
|
|
.name = "tar",
|
|
|
|
.opt = CFG_STR("tar", NULL, CFGF_NONE),
|
2011-06-27 15:38:33 +08:00
|
|
|
.env = "GENIMAGE_TAR",
|
2011-06-27 14:39:51 +08:00
|
|
|
.def = "tar",
|
2012-06-22 14:27:52 +08:00
|
|
|
}, {
|
2012-09-30 21:18:07 +08:00
|
|
|
.name = "tune2fs",
|
|
|
|
.opt = CFG_STR("tune2fs", NULL, CFGF_NONE),
|
|
|
|
.env = "GENIMAGE_TUNE2FS",
|
|
|
|
.def = "tune2fs",
|
2012-09-21 15:20:54 +08:00
|
|
|
}, {
|
2012-09-30 21:18:07 +08:00
|
|
|
.name = "ubinize",
|
|
|
|
.opt = CFG_STR("ubinize", NULL, CFGF_NONE),
|
|
|
|
.env = "GENIMAGE_UBINIZE",
|
|
|
|
.def = "ubinize",
|
2011-06-27 15:38:33 +08:00
|
|
|
}, {
|
|
|
|
.name = "config",
|
|
|
|
.env = "GENIMAGE_CONFIG",
|
|
|
|
.def = "genimage.cfg",
|
2011-06-27 14:39:51 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2011-06-27 15:38:33 +08:00
|
|
|
/*
|
|
|
|
* early setup: add all options from the array above to the
|
|
|
|
* list of options
|
|
|
|
*/
|
2011-06-27 14:39:51 +08:00
|
|
|
int init_config(void)
|
|
|
|
{
|
2011-06-30 01:58:10 +08:00
|
|
|
unsigned int i;
|
|
|
|
int ret;
|
2011-06-27 14:39:51 +08:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(opts); i++) {
|
2011-06-27 15:38:33 +08:00
|
|
|
ret = add_opt(opts[i].name, opts[i].env, &opts[i].opt, opts[i].def);
|
2011-06-27 14:39:51 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|