diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2016-07-11 07:58:34 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2016-07-11 07:58:34 +0200 |
commit | 50543054eb23e9b93b5be4a272d130f04bf8bc4d (patch) | |
tree | c7f2dcc723ebcf287063c1699e5d87d54733a631 /commands | |
parent | e77acea709fb64b64ab884f5e946738d326df970 (diff) | |
parent | 2c310bd8a9e43e7023fbf7f0cebb2faaed9b03e0 (diff) | |
download | barebox-50543054eb23e9b93b5be4a272d130f04bf8bc4d.tar.gz barebox-50543054eb23e9b93b5be4a272d130f04bf8bc4d.tar.xz |
Merge branch 'for-next/ubi'
Diffstat (limited to 'commands')
-rw-r--r-- | commands/ubi.c | 119 |
1 files changed, 112 insertions, 7 deletions
diff --git a/commands/ubi.c b/commands/ubi.c index 844d75dcb8..65d2d256a8 100644 --- a/commands/ubi.c +++ b/commands/ubi.c @@ -7,30 +7,134 @@ #include <getopt.h> #include <linux/mtd/mtd.h> #include <linux/kernel.h> +#include <linux/stat.h> #include <linux/mtd/mtd-abi.h> #include <mtd/ubi-user.h> #include <mtd/ubi-media.h> +static int do_ubiupdatevol(int argc, char *argv[]) +{ + int fd_img, fd_vol, ret = 0; + uint64_t size = 0; + struct stat st; + unsigned int count; + void *buf; + + if (argc - optind < 2) + return COMMAND_ERROR_USAGE; + + if (stat(argv[optind + 1], &st)) { + perror("stat image"); + return 1; + } + + size = st.st_size; + + if (size == FILESIZE_MAX) { + printf("%s has unknown filesize, this is not supported\n", + argv[optind + 1]); + return 1; + } + + fd_img = open(argv[optind + 1], O_RDONLY); + if (fd_img < 0) { + perror("open image"); + return 1; + } + + fd_vol = open(argv[optind], O_WRONLY); + if (fd_vol < 0) { + perror("open volume"); + ret = 1; + goto error_img; + } + + ret = ioctl(fd_vol, UBI_IOCVOLUP, &size); + if (ret) { + printf("failed to start update: %s\n", strerror(-ret)); + goto error; + } + + buf = xmalloc(RW_BUF_SIZE); + + while (size) { + + count = read(fd_img, buf, RW_BUF_SIZE); + if (count < 0) { + perror("read"); + ret = 1; + break; + } + + ret = write(fd_vol, buf, count); + if (ret < 0) { + perror("write"); + break; + } + + size -= count; + } + + free(buf); + +error: + close(fd_vol); +error_img: + close(fd_img); + return ret; +} + + +BAREBOX_CMD_HELP_START(ubiupdatevol) +BAREBOX_CMD_HELP_TEXT("Update UBI volume with an image.") +BAREBOX_CMD_HELP_END + +BAREBOX_CMD_START(ubiupdatevol) + .cmd = do_ubiupdatevol, + BAREBOX_CMD_DESC("Update an UBI volume") + BAREBOX_CMD_OPTS("UBIVOL IMAGE") + BAREBOX_CMD_GROUP(CMD_GRP_PART) + BAREBOX_CMD_HELP(cmd_ubiupdatevol_help) +BAREBOX_CMD_END + + static int do_ubimkvol(int argc, char *argv[]) { + int opt; struct ubi_mkvol_req req; int fd, ret; uint64_t size; - if (argc != 4) + req.vol_type = UBI_DYNAMIC_VOLUME; + + while ((opt = getopt(argc, argv, "t:")) > 0) { + switch (opt) { + case 't': + if (!strcmp(optarg, "dynamic")) + req.vol_type = UBI_DYNAMIC_VOLUME; + else if (!strcmp(optarg, "static")) + req.vol_type = UBI_STATIC_VOLUME; + else + return COMMAND_ERROR_USAGE; + break; + default: + return COMMAND_ERROR_USAGE; + } + } + + if (argc - optind != 3) return COMMAND_ERROR_USAGE; - size = strtoull_suffix(argv[3], NULL, 0); - req.name_len = min_t(int, strlen(argv[2]), UBI_VOL_NAME_MAX); - strncpy(req.name, argv[2], req.name_len); + size = strtoull_suffix(argv[optind+2], NULL, 0); + req.name_len = min_t(int, strlen(argv[optind+1]), UBI_VOL_NAME_MAX); + strncpy(req.name, argv[optind+1], req.name_len); req.name[req.name_len] = 0; - req.vol_type = UBI_DYNAMIC_VOLUME; req.bytes = size; req.vol_id = UBI_VOL_NUM_AUTO; req.alignment = 1; - fd = open(argv[1], O_WRONLY); + fd = open(argv[optind], O_WRONLY); if (fd < 0) { perror("open"); return 1; @@ -48,12 +152,13 @@ static int do_ubimkvol(int argc, char *argv[]) BAREBOX_CMD_HELP_START(ubimkvol) BAREBOX_CMD_HELP_TEXT("Create an UBI volume on UBIDEV with NAME and SIZE.") BAREBOX_CMD_HELP_TEXT("If SIZE is 0 all available space is used for the volume.") +BAREBOX_CMD_HELP_OPT("-t <static|dynamic>", "volume type, default is dynamic") BAREBOX_CMD_HELP_END BAREBOX_CMD_START(ubimkvol) .cmd = do_ubimkvol, BAREBOX_CMD_DESC("create an UBI volume") - BAREBOX_CMD_OPTS("UBIDEV NAME SIZE") + BAREBOX_CMD_OPTS("[-t] UBIDEV NAME SIZE") BAREBOX_CMD_GROUP(CMD_GRP_PART) BAREBOX_CMD_HELP(cmd_ubimkvol_help) BAREBOX_CMD_END |