summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArtem Bityutskiy <Artem.Bityutskiy@nokia.com>2007-12-23 22:56:49 +0200
committerArtem Bityutskiy <Artem.Bityutskiy@nokia.com>2007-12-23 22:59:19 +0200
commit945f33f51ecc20b08ad2b74e9c881b118e9b5f7b (patch)
treea56bac350ac096f9cd6108f4b2033382446f9113
parenta683490ad913de1403f49fe10a901b3bc8883f00 (diff)
ubi-utils: few mor fixes and cleanups
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
-rw-r--r--ubi-utils/src/common.h3
-rw-r--r--ubi-utils/src/libubi.c1
-rw-r--r--ubi-utils/src/ubimkvol.c134
-rw-r--r--ubi-utils/src/ubinfo.c47
-rw-r--r--ubi-utils/src/ubirmvol.c76
-rw-r--r--ubi-utils/src/ubiupdate.c43
6 files changed, 142 insertions, 162 deletions
diff --git a/ubi-utils/src/common.h b/ubi-utils/src/common.h
index 0e33dd1..06ae623 100644
--- a/ubi-utils/src/common.h
+++ b/ubi-utils/src/common.h
@@ -23,9 +23,6 @@
extern "C" {
#endif
-/* Maximum device node name length */
-#define MAX_NODE_LEN 255
-
/* Error messages */
#define errmsg(fmt, ...) do { \
fprintf(stderr, PROGRAM_NAME " error: " fmt "\n", ##__VA_ARGS__); \
diff --git a/ubi-utils/src/libubi.c b/ubi-utils/src/libubi.c
index 9aa7847..32d988e 100644
--- a/ubi-utils/src/libubi.c
+++ b/ubi-utils/src/libubi.c
@@ -691,7 +691,6 @@ int ubi_node_type(libubi_t desc, const char *node)
return -1;
if (!S_ISCHR(st.st_mode)) {
- errmsg("\"%s\" is not a character device", node);
errno = EINVAL;
return -1;
}
diff --git a/ubi-utils/src/ubimkvol.c b/ubi-utils/src/ubimkvol.c
index b8bc03a..38c737d 100644
--- a/ubi-utils/src/ubimkvol.c
+++ b/ubi-utils/src/ubimkvol.c
@@ -45,7 +45,7 @@ struct args {
int alignment;
const char *name;
int nlen;
- char node[MAX_NODE_LEN + 2];
+ const char *node;
int maxavs;
};
@@ -57,6 +57,7 @@ static struct args myargs = {
.vol_id = UBI_VOL_NUM_AUTO,
.name = NULL,
.nlen = 0,
+ .node = NULL,
.maxavs = 0,
};
@@ -99,7 +100,38 @@ static const struct option long_options[] = {
{ NULL, 0, NULL, 0},
};
-static int parse_opt(int argc, char * const argv[], struct args *args)
+static int param_sanity_check(void)
+{
+ int len;
+
+ if (myargs.bytes == -1 && !myargs.maxavs && myargs.lebs == -1) {
+ errmsg("volume size was not specified (use -h for help)");
+ return -1;
+ }
+
+ if ((myargs.bytes != -1 && (myargs.maxavs || myargs.lebs != -1)) ||
+ (myargs.lebs != -1 && (myargs.maxavs || myargs.bytes != -1)) ||
+ (myargs.maxavs && (myargs.bytes != -1 || myargs.lebs != -1))) {
+ errmsg("size specified with more then one option");
+ return -1;
+ }
+
+ if (myargs.name == NULL) {
+ errmsg("volume name was not specified (use -h for help)");
+ return -1;
+ }
+
+ len = strlen(myargs.name);
+ if (len > UBI_MAX_VOLUME_NAME) {
+ errmsg("too long name (%d symbols), max is %d",
+ len, UBI_MAX_VOLUME_NAME);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int parse_opt(int argc, char * const argv[])
{
while (1) {
int key;
@@ -112,9 +144,9 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
switch (key) {
case 't':
if (!strcmp(optarg, "dynamic"))
- args->vol_type = UBI_DYNAMIC_VOLUME;
+ myargs.vol_type = UBI_DYNAMIC_VOLUME;
else if (!strcmp(optarg, "static"))
- args->vol_type = UBI_STATIC_VOLUME;
+ myargs.vol_type = UBI_STATIC_VOLUME;
else {
errmsg("bad volume type: \"%s\"", optarg);
return -1;
@@ -122,8 +154,8 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
break;
case 's':
- args->bytes = strtoull(optarg, &endp, 0);
- if (endp == optarg || args->bytes <= 0) {
+ myargs.bytes = strtoull(optarg, &endp, 0);
+ if (endp == optarg || myargs.bytes <= 0) {
errmsg("bad volume size: \"%s\"", optarg);
return -1;
}
@@ -135,37 +167,37 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
"should be 'KiB', 'MiB' or 'GiB'", endp);
return -1;
}
- args->bytes *= mult;
+ myargs.bytes *= mult;
}
break;
case 'S':
- args->lebs = strtoull(optarg, &endp, 0);
- if (endp == optarg || args->lebs <= 0 || *endp != '\0') {
+ myargs.lebs = strtoull(optarg, &endp, 0);
+ if (endp == optarg || myargs.lebs <= 0 || *endp != '\0') {
errmsg("bad volume size: \"%s\"", optarg);
return -1;
}
break;
case 'a':
- args->alignment = strtoul(optarg, &endp, 0);
- if (*endp != '\0' || endp == optarg || args->alignment <= 0) {
+ myargs.alignment = strtoul(optarg, &endp, 0);
+ if (*endp != '\0' || endp == optarg || myargs.alignment <= 0) {
errmsg("bad volume alignment: \"%s\"", optarg);
return -1;
}
break;
case 'n':
- args->vol_id = strtoul(optarg, &endp, 0);
- if (*endp != '\0' || endp == optarg || args->vol_id < 0) {
+ myargs.vol_id = strtoul(optarg, &endp, 0);
+ if (*endp != '\0' || endp == optarg || myargs.vol_id < 0) {
errmsg("bad volume ID: " "\"%s\"", optarg);
return -1;
}
break;
case 'N':
- args->name = optarg;
- args->nlen = strlen(args->name);
+ myargs.name = optarg;
+ myargs.nlen = strlen(myargs.name);
break;
case 'h':
@@ -179,7 +211,7 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
exit(0);
case 'm':
- args->maxavs = 1;
+ myargs.maxavs = 1;
break;
case ':':
@@ -192,42 +224,18 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
}
}
- return 0;
-}
-
-static int param_sanity_check(struct args *args)
-{
- int len;
-
- if (strlen(args->node) > MAX_NODE_LEN) {
- errmsg("too long device node name: \"%s\" (%d characters), max. is %d",
- args->node, strlen(args->node), MAX_NODE_LEN);
- return -1;
- }
-
- if (args->bytes == -1 && !args->maxavs && args->lebs == -1) {
- errmsg("volume size was not specified (use -h for help)");
+ if (optind == argc) {
+ errmsg("UBI device name was not specified (use -h for help)");
return -1;
- }
-
- if ((args->bytes != -1 && (args->maxavs || args->lebs != -1)) ||
- (args->lebs != -1 && (args->maxavs || args->bytes != -1)) ||
- (args->maxavs && (args->bytes != -1 || args->lebs != -1))) {
- errmsg("size specified with more then one option");
+ } else if (optind != argc - 1) {
+ errmsg("more then one UBI devices specified (use -h for help)");
return -1;
}
- if (args->name == NULL) {
- errmsg("volume name was not specified (use -h for help)");
- return -1;
- }
+ myargs.node = argv[optind];
- len = strlen(args->name);
- if (len > UBI_MAX_VOLUME_NAME) {
- errmsg("too long name (%d symbols), max is %d",
- len, UBI_MAX_VOLUME_NAME);
+ if (param_sanity_check())
return -1;
- }
return 0;
}
@@ -240,29 +248,7 @@ int main(int argc, char * const argv[])
struct ubi_vol_info vol_info;
struct ubi_mkvol_request req;
- if (argc < 2) {
- errmsg("UBI device name was not specified (use -h for help)");
- return -1;
- }
-
- if (argc < 3) {
- errmsg("too few arguments (use -h for help)");
- return -1;
- }
-
- if (argv[1][0] == '-') {
- errmsg("UBI device was not specified (use -h for help)");
- return -1;
- }
-
- if (argv[2][0] != '-') {
- errmsg("incorrect arguments, use -h for help");
- return -1;
- }
-
- strncpy(myargs.node, argv[1], MAX_NODE_LEN + 1);
-
- err = parse_opt(argc, argv, &myargs);
+ err = parse_opt(argc, argv);
if (err)
return err;
@@ -273,9 +259,15 @@ int main(int argc, char * const argv[])
return -1;
}
- err = param_sanity_check(&myargs);
- if (err)
+ err = ubi_node_type(libubi, myargs.node);
+ if (err == 2) {
+ errmsg("\"%s\" is an UBI volume node, not an UBI device node",
+ myargs.node);
+ goto out_libubi;
+ } else if (err < 0) {
+ errmsg("\"%s\" is not an UBI device node", myargs.node);
goto out_libubi;
+ }
err = ubi_get_dev_info(libubi, myargs.node, &dev_info);
if (err) {
diff --git a/ubi-utils/src/ubinfo.c b/ubi-utils/src/ubinfo.c
index 280c770..c907335 100644
--- a/ubi-utils/src/ubinfo.c
+++ b/ubi-utils/src/ubinfo.c
@@ -39,15 +39,14 @@ struct args {
int devn;
int vol_id;
int all;
- char node[MAX_NODE_LEN + 2];
- int node_given;
+ const char *node;
};
static struct args myargs = {
.vol_id = -1,
.devn = -1,
.all = 0,
- .node_given = 0,
+ .node = NULL,
};
static const char *doc = "Version " PROGRAM_VERSION "\n"
@@ -83,7 +82,7 @@ static const struct option long_options[] = {
{ NULL, 0, NULL, 0},
};
-static int parse_opt(int argc, char * const argv[], struct args *args)
+static int parse_opt(int argc, char * const argv[])
{
while (1) {
int key;
@@ -95,20 +94,20 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
switch (key) {
case 'a':
- args->all = 1;
+ myargs.all = 1;
break;
case 'n':
- args->vol_id = strtoul(optarg, &endp, 0);
- if (*endp != '\0' || endp == optarg || args->vol_id < 0) {
+ myargs.vol_id = strtoul(optarg, &endp, 0);
+ if (*endp != '\0' || endp == optarg || myargs.vol_id < 0) {
errmsg("bad volume ID: " "\"%s\"", optarg);
return -1;
}
break;
case 'd':
- args->devn = strtoul(optarg, &endp, 0);
- if (*endp != '\0' || endp == optarg || args->devn < 0) {
+ myargs.devn = strtoul(optarg, &endp, 0);
+ if (*endp != '\0' || endp == optarg || myargs.devn < 0) {
errmsg("bad UBI device number: \"%s\"", optarg);
return -1;
}
@@ -135,6 +134,13 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
}
}
+ if (optind == argc - 1) {
+ myargs.node = argv[optind];
+ } else if (optind < argc) {
+ errmsg("more then one UBI devices specified (use -h for help)");
+ return -1;
+ }
+
return 0;
}
@@ -142,17 +148,10 @@ static int translate_dev(libubi_t libubi, const char *node)
{
int err;
- if (strlen(node) > MAX_NODE_LEN) {
- errmsg("too long device node name: \"%s\" (%d characters), max. is %d",
- node, strlen(node), MAX_NODE_LEN);
- return -1;
- }
-
err = ubi_node_type(libubi, node);
if (err == -1) {
if (errno) {
errmsg("unrecognized device node \"%s\"", node);
- perror("ubi_node_type");
return -1;
}
errmsg("\"%s\" does not correspond to any UBI device or volume",
@@ -399,21 +398,11 @@ int main(int argc, char * const argv[])
int err;
libubi_t libubi;
- if (argc > 1 && argv[1][0] != '-') {
- strncpy(myargs.node, argv[1], MAX_NODE_LEN + 1);
- myargs.node_given = 1;
- }
-
- if (argc > 2 && argv[2][0] != '-') {
- errmsg("incorrect arguments, use -h for help");
- return -1;
- }
-
- err = parse_opt(argc, argv, &myargs);
+ err = parse_opt(argc, argv);
if (err)
return -1;
- if (argc > 1 && !myargs.node_given && myargs.devn != -1) {
+ if (!myargs.node && myargs.devn != -1) {
errmsg("specify either device number or node file (use -h for help)");
return -1;
}
@@ -425,7 +414,7 @@ int main(int argc, char * const argv[])
return -1;
}
- if (myargs.node_given) {
+ if (myargs.node) {
/*
* A character device was specified, translate this into UBI
* device number and volume ID.
diff --git a/ubi-utils/src/ubirmvol.c b/ubi-utils/src/ubirmvol.c
index 10ce617..5f35525 100644
--- a/ubi-utils/src/ubirmvol.c
+++ b/ubi-utils/src/ubirmvol.c
@@ -39,11 +39,12 @@
/* The variables below is set by command line arguments */
struct args {
int vol_id;
- char node[MAX_NODE_LEN + 2];
+ const char *node;
};
static struct args myargs = {
.vol_id = -1,
+ .node = NULL,
};
static const char *doc = "Version: " PROGRAM_VERSION "\n"
@@ -66,7 +67,17 @@ static const struct option long_options[] = {
{ NULL, 0, NULL, 0},
};
-static int parse_opt(int argc, char * const argv[], struct args *args)
+static int param_sanity_check(void)
+{
+ if (myargs.vol_id == -1) {
+ errmsg("volume ID is was not specified");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int parse_opt(int argc, char * const argv[])
{
while (1) {
int key;
@@ -79,8 +90,8 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
switch (key) {
case 'n':
- args->vol_id = strtoul(optarg, &endp, 0);
- if (*endp != '\0' || endp == optarg || args->vol_id < 0) {
+ myargs.vol_id = strtoul(optarg, &endp, 0);
+ if (*endp != '\0' || endp == optarg || myargs.vol_id < 0) {
errmsg("bad volume ID: " "\"%s\"", optarg);
return -1;
}
@@ -98,7 +109,7 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
case ':':
errmsg("parameter is missing");
- goto out;
+ return -1;
default:
fprintf(stderr, "Use -h for help\n");
@@ -106,23 +117,18 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
}
}
- return 0;
- out:
- return -1;
-}
-
-static int param_sanity_check(struct args *args)
-{
- if (strlen(args->node) > MAX_NODE_LEN) {
- errmsg("too long device node name: \"%s\" (%d characters), max. is %d",
- args->node, strlen(args->node), MAX_NODE_LEN);
+ if (optind == argc) {
+ errmsg("UBI device name was not specified (use -h for help)");
+ return -1;
+ } else if (optind != argc - 1) {
+ errmsg("more then one UBI devices specified (use -h for help)");
return -1;
}
- if (args->vol_id == -1) {
- errmsg("volume ID is was not specified");
+ myargs.node = argv[optind];
+
+ if (param_sanity_check())
return -1;
- }
return 0;
}
@@ -132,29 +138,7 @@ int main(int argc, char * const argv[])
int err;
libubi_t libubi;
- strncpy(myargs.node, argv[1], MAX_NODE_LEN + 1);
-
- if (argc < 2) {
- errmsg("UBI device name was not specified (use -h for help)");
- return -1;
- }
-
- if (argc < 3) {
- errmsg("too few arguments (use -h for help)");
- return -1;
- }
-
- if (argv[1][0] == '-') {
- errmsg("UBI device was not specified (use -h for help)");
- return -1;
- }
-
- if (argv[2][0] != '-') {
- errmsg("incorrect arguments, use -h for help");
- return -1;
- }
-
- err = parse_opt(argc, argv, &myargs);
+ err = parse_opt(argc, argv);
if (err)
return -1;
@@ -165,9 +149,15 @@ int main(int argc, char * const argv[])
return -1;
}
- err = param_sanity_check(&myargs);
- if (err)
+ err = ubi_node_type(libubi, myargs.node);
+ if (err == 2) {
+ errmsg("\"%s\" is an UBI volume node, not an UBI device node",
+ myargs.node);
goto out_libubi;
+ } else if (err < 0) {
+ errmsg("\"%s\" is not an UBI device node", myargs.node);
+ goto out_libubi;
+ }
err = ubi_rmvol(libubi, myargs.node, myargs.vol_id);
if (err) {
diff --git a/ubi-utils/src/ubiupdate.c b/ubi-utils/src/ubiupdate.c
index 19caf5a..1b9188e 100644
--- a/ubi-utils/src/ubiupdate.c
+++ b/ubi-utils/src/ubiupdate.c
@@ -49,6 +49,7 @@ struct args {
static struct args myargs = {
.truncate = 0,
+ .node = NULL,
.img = NULL,
};
@@ -74,7 +75,7 @@ struct option long_options[] = {
{ NULL, 0, NULL, 0}
};
-static int parse_opt(int argc, char * const argv[], struct args *args)
+static int parse_opt(int argc, char * const argv[])
{
while (1) {
int key;
@@ -85,7 +86,7 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
switch (key) {
case 't':
- args->truncate = 1;
+ myargs.truncate = 1;
break;
case 'h':
@@ -108,6 +109,18 @@ static int parse_opt(int argc, char * const argv[], struct args *args)
}
}
+ if (optind == argc) {
+ errmsg("UBI device name was not specified (use -h for help)");
+ return -1;
+ } else if (optind != argc - 2) {
+ errmsg("specify UBI device name and image file name as first 2 "
+ "parameters (use -h for help)");
+ return -1;
+ }
+
+ myargs.node = argv[optind];
+ myargs.img = argv[optind + 1];
+
return 0;
}
@@ -256,23 +269,13 @@ int main(int argc, char * const argv[])
libubi_t libubi;
struct ubi_vol_info vol_info;
- if (argc < 2 || argv[1][0] == '-') {
- errmsg("UBI device name was not specified (use -h for help)");
- return -1;
- }
-
- if (argc < 3) {
- errmsg("too few arguments (use -h for help)");
+ err = parse_opt(argc, argv);
+ if (err)
return -1;
- }
-
- myargs.img = argv[argc - 1];
- myargs.node = argv[1];
-
- parse_opt(argc, argv, &myargs);
if (!myargs.img && !myargs.truncate) {
errmsg("incorrect arguments, use -h for help");
+ return -1;
}
libubi = libubi_open();
@@ -281,6 +284,16 @@ int main(int argc, char * const argv[])
goto out_libubi;
}
+ err = ubi_node_type(libubi, myargs.node);
+ if (err == 1) {
+ errmsg("\"%s\" is an UBI device node, not an UBI volume node",
+ myargs.node);
+ goto out_libubi;
+ } else if (err < 0) {
+ errmsg("\"%s\" is not an UBI volume node", myargs.node);
+ goto out_libubi;
+ }
+
err = ubi_get_vol_info(libubi, myargs.node, &vol_info);
if (err) {
errmsg("cannot get information about UBI volume \"%s\"",