aboutsummaryrefslogtreecommitdiff
path: root/miscutils
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2013-06-30 02:38:18 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2013-06-30 02:38:18 +0200
commit65bdf0b737955de55b4b38a08d51b410dfaf42eb (patch)
treeb1b28c87d273033f3cb858e2a47d477143e59592 /miscutils
parentf86404f7737e548fd1e4d319701ed4529026df80 (diff)
downloadbusybox-65bdf0b737955de55b4b38a08d51b410dfaf42eb.tar.gz
ubi_tools: extend supported volume size to 64 bits; shrink code
function old new delta get_num_from_file - 90 +90 packed_usage 29395 29377 -18 ubi_tools_main 1349 1141 -208 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 0/2 up/down: 90/-226) Total: -136 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'miscutils')
-rw-r--r--miscutils/ubi_tools.c303
1 files changed, 154 insertions, 149 deletions
diff --git a/miscutils/ubi_tools.c b/miscutils/ubi_tools.c
index 6f20f300c..79fb2044d 100644
--- a/miscutils/ubi_tools.c
+++ b/miscutils/ubi_tools.c
@@ -66,14 +66,6 @@
#endif
#include <mtd/ubi-user.h>
-#define OPTION_M (1 << 0)
-#define OPTION_D (1 << 1)
-#define OPTION_n (1 << 2)
-#define OPTION_N (1 << 3)
-#define OPTION_s (1 << 4)
-#define OPTION_a (1 << 5)
-#define OPTION_t (1 << 6)
-
#define do_attach (ENABLE_UBIATTACH && applet_name[3] == 'a')
#define do_detach (ENABLE_UBIDETACH && applet_name[3] == 'd')
#define do_mkvol (ENABLE_UBIMKVOL && applet_name[3] == 'm')
@@ -81,230 +73,243 @@
#define do_rsvol (ENABLE_UBIRSVOL && applet_name[4] == 's')
#define do_update (ENABLE_UBIUPDATEVOL && applet_name[3] == 'u')
-//usage:#define ubiattach_trivial_usage
-//usage: "-m MTD_NUM [-d UBI_NUM] UBI_CTRL_DEV"
-//usage:#define ubiattach_full_usage "\n\n"
-//usage: "Attach MTD device to UBI\n"
-//usage: "\n -m MTD_NUM MTD device number to attach"
-//usage: "\n -d UBI_NUM UBI device number to assign"
-//usage:
-//usage:#define ubidetach_trivial_usage
-//usage: "-d UBI_NUM UBI_CTRL_DEV"
-//usage:#define ubidetach_full_usage "\n\n"
-//usage: "Detach MTD device from UBI\n"
-//usage: "\n -d UBI_NUM UBI device number"
-//usage:
-//usage:#define ubimkvol_trivial_usage
-//usage: "UBI_DEVICE -N NAME [-s SIZE | -m]"
-//usage:#define ubimkvol_full_usage "\n\n"
-//usage: "Create UBI volume\n"
-//usage: "\n -a ALIGNMENT Volume alignment (default 1)"
-//usage: "\n -m Set volume size to maximum available"
-//usage: "\n -n VOLID Volume ID, if not specified, it"
-//usage: "\n will be assigned automatically"
-//usage: "\n -N NAME Volume name"
-//usage: "\n -s SIZE Size in bytes"
-//usage: "\n -t TYPE Volume type (static|dynamic)"
-//usage:
-//usage:#define ubirmvol_trivial_usage
-//usage: "UBI_DEVICE -n VOLID"
-//usage:#define ubirmvol_full_usage "\n\n"
-//usage: "Remove UBI volume\n"
-//usage: "\n -n VOLID Volume ID"
-//usage:
-//usage:#define ubirsvol_trivial_usage
-//usage: "UBI_DEVICE -n VOLID -s SIZE"
-//usage:#define ubirsvol_full_usage "\n\n"
-//usage: "Resize UBI volume\n"
-//usage: "\n -n VOLID Volume ID to resize"
-//usage: "\n -s SIZE Size in bytes"
-//usage:
-//usage:#define ubiupdatevol_trivial_usage
-//usage: "UBI_DEVICE [IMG_FILE]"
-//usage:#define ubiupdatevol_full_usage "\n\n"
-//usage: "Update UBI volume\n"
-//usage: "\n -t Truncate UBI volume"
-//usage: "\n -s SIZE Bytes in input (if reading stdin)"
+static unsigned get_num_from_file(const char *path, unsigned max, const char *errmsg)
+{
+ char buf[sizeof(long long)*3];
+ unsigned long long num;
+
+ if (open_read_close(path, buf, sizeof(buf)) < 0)
+ bb_perror_msg_and_die(errmsg, path);
+ /* It can be \n terminated, xatoull won't work well */
+ if (sscanf(buf, "%llu", &num) != 1 || num > max)
+ bb_error_msg_and_die(errmsg, path);
+ return num;
+}
+/* To prevent malloc(1G) accidents */
+#define MAX_SANE_ERASEBLOCK (16*1024*1024)
int ubi_tools_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int ubi_tools_main(int argc UNUSED_PARAM, char **argv)
{
unsigned opts;
char *ubi_ctrl;
- //struct stat st;
int fd;
int mtd_num;
int dev_num = UBI_DEV_NUM_AUTO;
int vol_id = UBI_VOL_NUM_AUTO;
- char *vol_name = NULL;
- int size_bytes;
+ char *vol_name;
+ unsigned long long size_bytes = size_bytes; /* for compiler */
+ char *size_bytes_str;
int alignment = 1;
- char *type = NULL;
+ char *type;
+ union {
+ struct ubi_attach_req attach_req;
+ struct ubi_mkvol_req mkvol_req;
+ struct ubi_rsvol_req rsvol_req;
+ } req_structs;
+#define attach_req req_structs.attach_req
+#define mkvol_req req_structs.mkvol_req
+#define rsvol_req req_structs.rsvol_req
+ char path[sizeof("/sys/class/ubi/ubi%d_%d/usable_eb_size")
+ + 2 * sizeof(int)*3 + /*just in case:*/ 16];
+#define path_sys_class_ubi_ubi (path + sizeof("/sys/class/ubi/ubi")-1)
+
+ strcpy(path_sys_class_ubi_ubi, "/sys/class/ubi/ubi");
+ memset(&req_structs, 0, sizeof(req_structs));
if (do_mkvol) {
- opt_complementary = "-1:d+:n+:s+:a+";
- opts = getopt32(argv, "md:n:N:s:a:t::",
+ opt_complementary = "-1:d+:n+:a+";
+ opts = getopt32(argv, "md:n:N:s:a:t:",
&dev_num, &vol_id,
- &vol_name, &size_bytes, &alignment, &type
+ &vol_name, &size_bytes_str, &alignment, &type
);
} else {
- opt_complementary = "-1:m+:d+:n+:s+:a+";
- opts = getopt32(argv, "m:d:n:N:s:a:t::",
+ opt_complementary = "-1:m+:d+:n+:a+";
+ opts = getopt32(argv, "m:d:n:N:s:a:t:",
&mtd_num, &dev_num, &vol_id,
- &vol_name, &size_bytes, &alignment, &type
+ &vol_name, &size_bytes_str, &alignment, &type
);
}
- ubi_ctrl = argv[optind];
+#define OPTION_m (1 << 0)
+#define OPTION_d (1 << 1)
+#define OPTION_n (1 << 2)
+#define OPTION_N (1 << 3)
+#define OPTION_s (1 << 4)
+#define OPTION_a (1 << 5)
+#define OPTION_t (1 << 6)
+
+ if (opts & OPTION_s)
+ size_bytes = xatoull(size_bytes_str);
+ argv += optind;
+ ubi_ctrl = *argv;
fd = xopen(ubi_ctrl, O_RDWR);
//xfstat(fd, &st, ubi_ctrl);
//if (!S_ISCHR(st.st_mode))
// bb_error_msg_and_die("%s: not a char device", ubi_ctrl);
+//usage:#define ubiattach_trivial_usage
+//usage: "-m MTD_NUM [-d UBI_NUM] UBI_CTRL_DEV"
+//usage:#define ubiattach_full_usage "\n\n"
+//usage: "Attach MTD device to UBI\n"
+//usage: "\n -m MTD_NUM MTD device number to attach"
+//usage: "\n -d UBI_NUM UBI device number to assign"
if (do_attach) {
- struct ubi_attach_req req;
- if (!(opts & OPTION_M))
+ if (!(opts & OPTION_m))
bb_error_msg_and_die("%s device not specified", "MTD");
- memset(&req, 0, sizeof(req));
- req.mtd_num = mtd_num;
- req.ubi_num = dev_num;
+ attach_req.mtd_num = mtd_num;
+ attach_req.ubi_num = dev_num;
- xioctl(fd, UBI_IOCATT, &req);
+ xioctl(fd, UBI_IOCATT, &attach_req);
} else
+
+//usage:#define ubidetach_trivial_usage
+//usage: "-d UBI_NUM UBI_CTRL_DEV"
+//usage:#define ubidetach_full_usage "\n\n"
+//usage: "Detach MTD device from UBI\n"
+//usage: "\n -d UBI_NUM UBI device number"
if (do_detach) {
- if (!(opts & OPTION_D))
+ if (!(opts & OPTION_d))
bb_error_msg_and_die("%s device not specified", "UBI");
+ /* FIXME? kernel expects int32_t* here: */
xioctl(fd, UBI_IOCDET, &dev_num);
} else
+
+//usage:#define ubimkvol_trivial_usage
+//usage: "UBI_DEVICE -N NAME [-s SIZE | -m]"
+//usage:#define ubimkvol_full_usage "\n\n"
+//usage: "Create UBI volume\n"
+//usage: "\n -a ALIGNMENT Volume alignment (default 1)"
+//usage: "\n -m Set volume size to maximum available"
+//usage: "\n -n VOLID Volume ID. If not specified,"
+//usage: "\n assigned automatically"
+//usage: "\n -N NAME Volume name"
+//usage: "\n -s SIZE Size in bytes"
+//usage: "\n -t TYPE Volume type (static|dynamic)"
if (do_mkvol) {
- struct ubi_mkvol_req req;
- int vol_name_len;
- if (opts & OPTION_M) {
+ if (opts & OPTION_m) {
unsigned leb_avail;
unsigned leb_size;
unsigned num;
- char path[sizeof("/sys/class/ubi/ubi%d/avail_eraseblocks") + sizeof(int)*3];
char *p;
- char buf[20];
- if (strncmp(ubi_ctrl, "/dev/ubi", 8) != 0)
- bb_error_msg_and_die("%s device node not in correct format", "UBI");
- num = xstrtou(ubi_ctrl+8, 10);
- p = path + sprintf(path, "/sys/class/ubi/ubi%d/", num);
+ if (sscanf(ubi_ctrl, "/dev/ubi%u", &num) != 1)
+ bb_error_msg_and_die("wrong format of UBI device name");
+
+ p = path_sys_class_ubi_ubi + sprintf(path_sys_class_ubi_ubi, "%u/", num);
strcpy(p, "avail_eraseblocks");
- if ((num = open_read_close(path, buf, sizeof(buf))) <= 1)
- bb_error_msg_and_die("%s could not get LEB available", "UBI");
- buf[num-1] = '\0'; // trim trailing newline
- leb_avail = xstrtou(buf, 10);
+ leb_avail = get_num_from_file(path, UINT_MAX, "Can't get available eraseblocks from '%s'");
strcpy(p, "eraseblock_size");
- if ((num = open_read_close(path, buf, sizeof(buf))) <= 0)
- bb_error_msg_and_die("%s could not get LEB size", "UBI");
- buf[num-1] = '\0'; // trim trailing newline
- leb_size = xstrtou(buf, 10);
+ leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK, "Can't get eraseblock size from '%s'");
- size_bytes = leb_avail * leb_size;
-
- if (size_bytes <= 0)
- bb_error_msg_and_die("%s invalid maximum size calculated", "UBI");
+ size_bytes = leb_avail * (unsigned long long)leb_size;
+ //if (size_bytes <= 0)
+ // bb_error_msg_and_die("%s invalid maximum size calculated", "UBI");
} else
if (!(opts & OPTION_s))
- bb_error_msg_and_die("%s size not specified", "UBI");
- if (!(opts & OPTION_N))
- bb_error_msg_and_die("%s name not specified", "UBI");
- vol_name_len = strlen(vol_name);
- if (vol_name_len > UBI_MAX_VOLUME_NAME)
- bb_error_msg_and_die("%s volume name too long", "UBI");
-
- memset(&req, 0, sizeof(req));
- req.vol_id = vol_id;
- if ((opts & OPTION_t) && type) {
- if (type[0] == 's')
- req.vol_type = UBI_STATIC_VOLUME;
- else
- req.vol_type = UBI_DYNAMIC_VOLUME;
- } else {
- req.vol_type = UBI_DYNAMIC_VOLUME;
- }
- req.alignment = alignment;
- req.bytes = size_bytes;
- strncpy(req.name, vol_name, UBI_MAX_VOLUME_NAME);
- req.name_len = vol_name_len;
+ bb_error_msg_and_die("size not specified");
- xioctl(fd, UBI_IOCMKVOL, &req);
+ if (!(opts & OPTION_N))
+ bb_error_msg_and_die("name not specified");
+
+ mkvol_req.vol_id = vol_id;
+ mkvol_req.vol_type = UBI_DYNAMIC_VOLUME;
+ if ((opts & OPTION_t) && type[0] == 's')
+ mkvol_req.vol_type = UBI_STATIC_VOLUME;
+ mkvol_req.alignment = alignment;
+ mkvol_req.bytes = size_bytes; /* signed int64_t */
+ strncpy(mkvol_req.name, vol_name, UBI_MAX_VOLUME_NAME);
+ mkvol_req.name_len = strlen(vol_name);
+ if (mkvol_req.name_len > UBI_MAX_VOLUME_NAME)
+ bb_error_msg_and_die("volume name too long: '%s'", vol_name);
+
+ xioctl(fd, UBI_IOCMKVOL, &mkvol_req);
} else
+
+//usage:#define ubirmvol_trivial_usage
+//usage: "UBI_DEVICE -n VOLID"
+//usage:#define ubirmvol_full_usage "\n\n"
+//usage: "Remove UBI volume\n"
+//usage: "\n -n VOLID Volume ID"
if (do_rmvol) {
if (!(opts & OPTION_n))
- bb_error_msg_and_die("%s volume id not specified", "UBI");
+ bb_error_msg_and_die("volume id not specified");
+ /* FIXME? kernel expects int32_t* here: */
xioctl(fd, UBI_IOCRMVOL, &vol_id);
} else
+
+//usage:#define ubirsvol_trivial_usage
+//usage: "UBI_DEVICE -n VOLID -s SIZE"
+//usage:#define ubirsvol_full_usage "\n\n"
+//usage: "Resize UBI volume\n"
+//usage: "\n -n VOLID Volume ID"
+//usage: "\n -s SIZE Size in bytes"
if (do_rsvol) {
- struct ubi_rsvol_req req;
if (!(opts & OPTION_s))
- bb_error_msg_and_die("%s size not specified", "UBI");
+ bb_error_msg_and_die("size not specified");
if (!(opts & OPTION_n))
- bb_error_msg_and_die("%s volume id not specified", "UBI");
+ bb_error_msg_and_die("volume id not specified");
- memset(&req, 0, sizeof(req));
- req.bytes = size_bytes;
- req.vol_id = vol_id;
+ rsvol_req.bytes = size_bytes; /* signed int64_t */
+ rsvol_req.vol_id = vol_id;
- xioctl(fd, UBI_IOCRSVOL, &req);
+ xioctl(fd, UBI_IOCRSVOL, &rsvol_req);
} else
+
+//usage:#define ubiupdatevol_trivial_usage
+//usage: "UBI_DEVICE [-t | [-s SIZE] IMG_FILE]"
+//usage:#define ubiupdatevol_full_usage "\n\n"
+//usage: "Update UBI volume\n"
+//usage: "\n -t Truncate to zero size"
+//usage: "\n -s SIZE Size in bytes to resize to"
if (do_update) {
- long long bytes;
+ int64_t bytes64;
if (opts & OPTION_t) {
- // truncate the volume by starting an update for size 0
- bytes = 0;
- xioctl(fd, UBI_IOCVOLUP, &bytes);
+ /* truncate the volume by starting an update for size 0 */
+ bytes64 = 0;
+ /* this ioctl expects int64_t* parameter */
+ xioctl(fd, UBI_IOCVOLUP, &bytes64);
}
else {
struct stat st;
- char buf[sizeof("/sys/class/ubi/ubi%d_%d/usable_eb_size") + 2 * sizeof(int)*3];
- int input_fd;
unsigned ubinum, volnum;
unsigned leb_size;
ssize_t len;
char *input_data;
- // Make assumption that device not is in normal format.
- // Removes need for scanning sysfs tree as full libubi does
+ /* Assume that device is in normal format. */
+ /* Removes need for scanning sysfs tree as full libubi does. */
if (sscanf(ubi_ctrl, "/dev/ubi%u_%u", &ubinum, &volnum) != 2)
- bb_error_msg_and_die("%s volume node not in correct format", "UBI");
-
- sprintf(buf, "/sys/class/ubi/ubi%u_%u/usable_eb_size", ubinum, volnum);
- if (open_read_close(buf, buf, sizeof(buf)) <= 0)
- bb_error_msg_and_die("%s could not get LEB size", "UBI");
- if (sscanf(buf, "%u", &leb_size) != 1)
- bb_error_msg_and_die("%s could not get LEB size", "UBI");
-
- if (opts & OPTION_s) {
- input_fd = 0;
- } else {
- if (!argv[optind+1])
+ bb_error_msg_and_die("wrong format of UBI device name");
+
+ sprintf(path_sys_class_ubi_ubi, "%u_%u/usable_eb_size", ubinum, volnum);
+ leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK, "Can't get usable eraseblock size from '%s'");
+
+ if (!(opts & OPTION_s)) {
+ if (!*argv)
bb_show_usage();
- xstat(argv[optind+1], &st);
+ xstat(*argv, &st);
size_bytes = st.st_size;
- input_fd = xopen(argv[optind+1], O_RDONLY);
+ xmove_fd(xopen(*argv, O_RDONLY), STDIN_FILENO);
}
- bytes = size_bytes;
- xioctl(fd, UBI_IOCVOLUP, &bytes);
+ bytes64 = size_bytes;
+ /* this ioctl expects signed int64_t* parameter */
+ xioctl(fd, UBI_IOCVOLUP, &bytes64);
input_data = xmalloc(leb_size);
- while ((len = full_read(input_fd, input_data, leb_size)) > 0) {
+ while ((len = full_read(STDIN_FILENO, input_data, leb_size)) > 0) {
xwrite(fd, input_data, len);
}
if (len < 0)
- bb_error_msg_and_die("%s volume update failed", "UBI");
- if (ENABLE_FEATURE_CLEAN_UP)
- close(input_fd);
+ bb_perror_msg_and_die("UBI volume update failed");
}
}