mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Giorgio Dal Molin <iw3gtf@arcor.de>
To: barebox@lists.infradead.org
Cc: Giorgio Dal Molin <giorgio.dal.molin@mobotix.com>,
	Giorgio Dal Molin <iw3gtf@arcor.de>
Subject: [PATCH 2/2] commands: ubi: added the new command 'ubirename' to rename ubi volumes.
Date: Wed, 21 Sep 2016 10:04:43 +0200	[thread overview]
Message-ID: <20160921080443.21522-3-iw3gtf@arcor.de> (raw)
In-Reply-To: <20160921080443.21522-1-iw3gtf@arcor.de>

From: Giorgio Dal Molin <giorgio.dal.molin@mobotix.com>

The syntax was taken from the corresponding command of the 'mts-utils'
userland package:

# ubirename UBIDEV OLD_NAME NEW_NAME [OLD_NAME NEW_NAME ...]

Signed-off-by: Giorgio Dal Molin <iw3gtf@arcor.de>
---
 commands/ubi.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 87 insertions(+)

diff --git a/commands/ubi.c b/commands/ubi.c
index 26b521f..3479340 100644
--- a/commands/ubi.c
+++ b/commands/ubi.c
@@ -6,6 +6,8 @@
 #include <errno.h>
 #include <getopt.h>
 #include <linux/mtd/mtd.h>
+#include <linux/mtd/ubi.h>
+#include <libgen.h>
 #include <linux/kernel.h>
 #include <linux/stat.h>
 #include <linux/mtd/mtd-abi.h>
@@ -306,3 +308,88 @@ BAREBOX_CMD_START(ubirmvol)
 	BAREBOX_CMD_GROUP(CMD_GRP_PART)
 	BAREBOX_CMD_HELP(cmd_ubirmvol_help)
 BAREBOX_CMD_END
+
+
+static int get_vol_id(const char *vol_name)
+{
+	struct ubi_volume_desc *desc;
+	struct cdev *vol_cdev;
+	struct ubi_volume_info vi;
+
+	vol_cdev = cdev_by_name(vol_name);
+	if(!vol_cdev) {
+		perror("cdev_by_name");
+		return -1;
+	}
+	desc = ubi_open_volume_cdev(vol_cdev, UBI_READONLY);
+	if(IS_ERR(desc)) {
+		perror("ubi_open_volume_cdev");
+		return -1;
+	}
+	ubi_get_volume_info(desc, &vi);
+	ubi_close_volume(desc);
+
+	return vi.vol_id;
+};
+
+static int do_ubirename(int argc, char *argv[])
+{
+	struct ubi_rnvol_req req;
+	struct cdev *ubi_cd;
+	int i, j, fd, ret;
+
+	if ((argc < 4) || (argc % 2))
+		return COMMAND_ERROR_USAGE;
+
+	req.count = (argc / 2) - 1;
+	if (req.count > UBI_MAX_RNVOL) {
+		printf("too many volume renames. (max: %u)\n", UBI_MAX_RNVOL);
+		return COMMAND_ERROR_USAGE;
+	}
+
+	ubi_cd = cdev_by_name(basename(argv[1]));
+	if(!ubi_cd || !ubi_cd->name || (strlen(ubi_cd->name)>127)) {
+		printf("arg 1 (%s) is not an ubi device.\n", argv[1]);
+		return COMMAND_ERROR_USAGE;
+	}
+
+	for(i=2, j=0; i<argc; ++j, i+=2) {
+		char vol_name[128 + UBI_MAX_VOLUME_NAME];
+
+		snprintf(vol_name, sizeof(vol_name), "%s.%s", ubi_cd->name, argv[i]);
+		req.ents[j].vol_id = get_vol_id(vol_name);
+		if(req.ents[j].vol_id < 0) {
+			printf("'%s' is not a volume name.\n", vol_name);
+			return COMMAND_ERROR_USAGE;
+		}
+		strncpy(req.ents[j].name, argv[i+1], UBI_MAX_VOLUME_NAME);
+		req.ents[j].name_len = strlen(req.ents[j].name);
+	}
+
+	fd = open(argv[1], O_WRONLY);
+	if (fd < 0) {
+		perror("open");
+		return 1;
+	}
+
+	ret = ioctl(fd, UBI_IOCRNVOL, &req);
+	if (ret)
+		printf("failed to rename: %s\n", strerror(-ret));
+
+	close(fd);
+
+	return ret ? 1 : 0;
+
+}
+
+BAREBOX_CMD_HELP_START(ubirename)
+BAREBOX_CMD_HELP_TEXT("Rename UBI volume(s) from UBIDEV")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(ubirename)
+	.cmd		= do_ubirename,
+	BAREBOX_CMD_DESC("rename UBI volume(s)")
+	BAREBOX_CMD_OPTS("UBIDEV OLD_NAME NEW_NAME [OLD_NAME NEW_NAME ...]")
+	BAREBOX_CMD_GROUP(CMD_GRP_PART)
+	BAREBOX_CMD_HELP(cmd_ubirename_help)
+BAREBOX_CMD_END
-- 
2.10.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2016-09-21  8:06 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-21  8:04 [PATCH 0/2] added support for renaming UBI volumes Giorgio Dal Molin
2016-09-21  8:04 ` [PATCH 1/2] mtd: UBI: add support (ioctl) for renaming ubi volumes Giorgio Dal Molin
2016-09-21  8:04 ` Giorgio Dal Molin [this message]
2016-09-22  8:04   ` [PATCH 2/2] commands: ubi: added the new command 'ubirename' to rename " Sascha Hauer
2016-09-23  9:07     ` [PATCH 0/2] mtd: ubi: implement the new command 'ubirename' Giorgio Dal Molin
2016-09-23  9:07     ` [PATCH 1/2] mtd: ubi: add API call to rename volumes Giorgio Dal Molin
2016-09-23  9:07     ` [PATCH 2/2] mtd: ubi: commands: added the new command 'ubirename' Giorgio Dal Molin
2016-09-26  6:19       ` Sascha Hauer
2016-09-26 10:52         ` [PATCH 0/2 (try 2)] mtd: ubi: implement " Giorgio Dal Molin
2016-09-27  6:21           ` Sascha Hauer
2016-09-26 10:52         ` [PATCH 1/2] mtd: ubi: add API call to rename volumes Giorgio Dal Molin
2016-09-26 10:52         ` [PATCH 2/2] mtd: ubi: commands: added the new command 'ubirename' Giorgio Dal Molin
2016-09-23 10:11   ` Aw: Re: [PATCH 2/2] commands: ubi: added the new command 'ubirename' to rename ubi volumes iw3gtf
2016-09-27  6:23     ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160921080443.21522-3-iw3gtf@arcor.de \
    --to=iw3gtf@arcor.de \
    --cc=barebox@lists.infradead.org \
    --cc=giorgio.dal.molin@mobotix.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox