* [PATCH v1] mtd: ubi: add ubi_attach_mtd_path function
@ 2017-04-05 7:11 Oleksij Rempel
0 siblings, 0 replies; only message in thread
From: Oleksij Rempel @ 2017-04-05 7:11 UTC (permalink / raw)
To: barebox; +Cc: Oleksij Rempel, Oleksij Rempel
From: Oleksij Rempel <linux@rempel-privat.de>
Add funktion to attach mtd device by path. This will
reduce some code duplications.
Signed-off-by: Oleksij Rempel <linux@rempel-privat.de>
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
commands/ubi.c | 23 ++---------------------
drivers/mtd/ubi/build.c | 34 ++++++++++++++++++++++++++++++++++
include/mtd/ubi-user.h | 1 +
3 files changed, 37 insertions(+), 21 deletions(-)
diff --git a/commands/ubi.c b/commands/ubi.c
index 5e2758400..795ac3a53 100644
--- a/commands/ubi.c
+++ b/commands/ubi.c
@@ -174,8 +174,7 @@ BAREBOX_CMD_END
static int do_ubiattach(int argc, char *argv[])
{
int opt;
- struct mtd_info_user user;
- int fd, ret;
+ int ret;
int vid_hdr_offset = 0;
int devnum = UBI_DEV_NUM_AUTO;
@@ -195,25 +194,7 @@ static int do_ubiattach(int argc, char *argv[])
if (optind == argc)
return COMMAND_ERROR_USAGE;
- fd = open(argv[optind], O_RDWR);
- if (fd < 0) {
- perror("open");
- return 1;
- }
-
- ret = ioctl(fd, MEMGETINFO, &user);
- if (ret) {
- printf("MEMGETINFO failed: %s\n", strerror(-ret));
- goto err;
- }
-
- ret = ubi_attach_mtd_dev(user.mtd, devnum, vid_hdr_offset, 20);
- if (ret < 0)
- printf("failed to attach: %s\n", strerror(-ret));
- else
- ret = 0;
-err:
- close(fd);
+ ret = ubi_attach_mtd_path(argv[optind]);
return ret ? 1 : 0;
}
diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c
index 2ea66ed06..de8aae729 100644
--- a/drivers/mtd/ubi/build.c
+++ b/drivers/mtd/ubi/build.c
@@ -25,6 +25,8 @@
* later using the "UBI control device".
*/
+#include <fs.h>
+#include <ioctl.h>
#include <linux/err.h>
#include <linux/stringify.h>
#include <linux/stat.h>
@@ -684,6 +686,38 @@ out_free:
}
/**
+ * ubi_attach_mtd_path - attach an MTD device by path.
+ * @path: path to MTD device.
+ */
+int ubi_attach_mtd_path(const char *path)
+{
+ struct mtd_info_user user;
+ int fd, ret;
+
+ fd = open(path, O_RDWR);
+ if (fd < 0) {
+ perror("open");
+ return fd;
+ }
+
+ ret = ioctl(fd, MEMGETINFO, &user);
+ if (ret) {
+ pr_err("MEMGETINFO failed: %s\n", strerror(-ret));
+ goto err;
+ }
+
+ ret = ubi_attach_mtd_dev(user.mtd, UBI_DEV_NUM_AUTO, 0, 20);
+ if (ret < 0)
+ pr_err("failed to attach: %s\n", strerror(-ret));
+ else
+ ret = 0;
+err:
+ close(fd);
+
+ return ret;
+}
+
+/**
* ubi_detach_mtd_dev - detach an MTD device.
* @ubi_num: UBI device number to detach from
* @anyway: detach MTD even if device reference count is not zero
diff --git a/include/mtd/ubi-user.h b/include/mtd/ubi-user.h
index 9425533d1..1a1dbb04a 100644
--- a/include/mtd/ubi-user.h
+++ b/include/mtd/ubi-user.h
@@ -408,6 +408,7 @@ struct ubi_set_vol_prop_req {
int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
int vid_hdr_offset, int max_beb_per1024);
+int ubi_attach_mtd_path(const char *path);
int ubi_detach(int ubi_num);
int ubi_num_get_by_mtd(struct mtd_info *mtd);
--
2.11.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2017-04-05 7:12 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-05 7:11 [PATCH v1] mtd: ubi: add ubi_attach_mtd_path function Oleksij Rempel
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox