diff mbox

[4/6] libmtd: add helper funcs for getting fds, regioninfo, and locked info

Message ID 1307427548-29306-4-git-send-email-vapier@gentoo.org
State Superseded, archived
Headers show

Commit Message

Mike Frysinger June 7, 2011, 6:19 a.m. UTC
This extends the libmtd with the helper functions:
	mtd_open_dev1: open a MTD device by its number
	mtd_regioninfo: interface to MEMGETREGIONINFO
	mtd_islocked: interface to MEMISLOCKED

Users of these functions will follow shortly ...

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
---
 include/libmtd.h |   37 +++++++++++++++++++++++++++++++++++++
 lib/libmtd.c     |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 88 insertions(+), 0 deletions(-)

Comments

Artem Bityutskiy June 7, 2011, 6:50 a.m. UTC | #1
On Tue, 2011-06-07 at 02:19 -0400, Mike Frysinger wrote: 
> +int mtd_open_dev1(int mtd_num)
> +{
> +	char devpath[128];
> +	int fd;
> +
> +	sprintf(devpath, "/dev/mtd%i", mtd_num);
> +	fd = open(devpath, O_RDONLY | O_CLOEXEC);
> +	if (fd < 0)
> +		return sys_errmsg("cannot open \"%s\"", devpath);
> +

Could we avoid hard-coding device node names?

We indeed have -m option in mtdutils which opens a device by its number.
But I think it was a mistake. Generally, MTD devices can have any name,
it is up to udev configuration. So I'd rather deprecate and would
require a list of device node names.
Artem Bityutskiy June 7, 2011, 6:56 a.m. UTC | #2
On Tue, 2011-06-07 at 09:50 +0300, Artem Bityutskiy wrote:
> We indeed have -m option in mtdutils which opens a device by its number.
> But I think it was a mistake. Generally, MTD devices can have any name,
> it is up to udev configuration. So I'd rather deprecate and would
> require a list of device node names.

Sorry, I was not clear. I wanted to say that supports -m and does
hard-code the mtd device name. But this was a mistake because it gives a
bad example, and adding more hard-coded names is bad.

So I think this option can be deprecated and removed, and we should only
accept full device node names.
Mike Frysinger June 7, 2011, 7:04 a.m. UTC | #3
On Tue, Jun 7, 2011 at 02:56, Artem Bityutskiy wrote:
> On Tue, 2011-06-07 at 09:50 +0300, Artem Bityutskiy wrote:
>> We indeed have -m option in mtdutils which opens a device by its number.
>> But I think it was a mistake. Generally, MTD devices can have any name,
>> it is up to udev configuration. So I'd rather deprecate and would
>> require a list of device node names.
>
> Sorry, I was not clear. I wanted to say that supports -m and does
> hard-code the mtd device name. But this was a mistake because it gives a
> bad example, and adding more hard-coded names is bad.
>
> So I think this option can be deprecated and removed, and we should only
> accept full device node names.

i only added this because mtdinfo has the -m option.  i have no
problem with punting that and going back to requiring people to
specify the path to the device nodes.  i'm off now though, so i'll get
to it tomorrow.
-mike
diff mbox

Patch

diff --git a/include/libmtd.h b/include/libmtd.h
index e30c8a6..ba1348a 100644
--- a/include/libmtd.h
+++ b/include/libmtd.h
@@ -35,6 +35,9 @@  extern "C" {
 /* MTD library descriptor */
 typedef void * libmtd_t;
 
+/* Forward decls */
+struct region_info_user;
+
 /**
  * @mtd_dev_cnt: count of MTD devices in system
  * @lowest_mtd_num: lowest MTD device number in system
@@ -138,6 +141,15 @@  int mtd_get_dev_info(libmtd_t desc, const char *node, struct mtd_dev_info *mtd);
 int mtd_get_dev_info1(libmtd_t desc, int mtd_num, struct mtd_dev_info *mtd);
 
 /**
+ * mtd_open_dev1 - get a fd to an MTD device.
+ * @desc: MTD library descriptor
+ * @mtd_num: MTD device number to fetch information about
+ *
+ * Based on a MTD device number, attempt to open the corresponding node.
+ */
+int mtd_open_dev1(int mtd_num);
+
+/**
  * mtd_lock - lock eraseblocks.
  * @desc: MTD library descriptor
  * @mtd: MTD device description object
@@ -174,6 +186,31 @@  int mtd_unlock(const struct mtd_dev_info *mtd, int fd, int eb);
 int mtd_erase(libmtd_t desc, const struct mtd_dev_info *mtd, int fd, int eb);
 
 /**
+ * mtd_regioninfo - get information about an erase region.
+ * @fd: MTD device node file descriptor
+ * @regidx: index of region to look up
+ * @reginfo: the region information is returned here
+ *
+ * This function gets information about an erase region defined by the
+ * @regidx index and saves this information in the @reginfo object.
+ * Returns %0 in case of success and %-1 in case of failure. If the
+ * @regidx is not valid or unavailable, errno is set to @ENODEV.
+ */
+int mtd_regioninfo(int fd, int regidx, struct region_info_user *reginfo);
+
+/**
+ * mtd_islocked - see if the specified eraseblock is locked.
+ * @mtd: MTD device description object
+ * @fd: MTD device node file descriptor
+ * @eb: eraseblock to check
+ *
+ * This function checks to see if eraseblock @eb of MTD device described
+ * by @fd is locked. Returns %0 if it is unlocked, %1 if it is locked, and
+ * %-1 in case of failure.
+ */
+int mtd_islocked(const struct mtd_dev_info *mtd, int fd, int eb);
+
+/**
  * mtd_torture - torture an eraseblock.
  * @desc: MTD library descriptor
  * @mtd: MTD device description object
diff --git a/lib/libmtd.c b/lib/libmtd.c
index a651808..3be28c4 100644
--- a/lib/libmtd.c
+++ b/lib/libmtd.c
@@ -787,6 +787,25 @@  int mtd_get_dev_info(libmtd_t desc, const char *node, struct mtd_dev_info *mtd)
 	return mtd_get_dev_info1(desc, mtd_num, mtd);
 }
 
+int mtd_open_dev1(int mtd_num)
+{
+	char devpath[128];
+	int fd;
+
+	sprintf(devpath, "/dev/mtd%i", mtd_num);
+	fd = open(devpath, O_RDONLY | O_CLOEXEC);
+	if (fd < 0)
+		return sys_errmsg("cannot open \"%s\"", devpath);
+
+	/*
+	 * XXX: We could do a stat on the fd and make sure the dev
+	 *      major/minor matches what is expected ... but no real
+	 *      way for us to recover from a mismatch.
+	 */
+
+	return fd;
+}
+
 static inline int mtd_ioctl_error(const struct mtd_dev_info *mtd, int eb,
 				  const char *sreq)
 {
@@ -883,6 +902,38 @@  int mtd_erase(libmtd_t desc, const struct mtd_dev_info *mtd, int fd, int eb)
 	return 0;
 }
 
+int mtd_regioninfo(int fd, int regidx, struct region_info_user *reginfo)
+{
+	int ret;
+
+	if (regidx < 0) {
+		errno = ENODEV;
+		return -1;
+	}
+
+	ret = ioctl(fd, MEMGETREGIONINFO, reginfo);
+	if (ret < 0)
+		return sys_errmsg("%s ioctl failed for erase region %d",
+			"MEMGETREGIONINFO", regidx);
+
+	return 0;
+}
+
+int mtd_islocked(const struct mtd_dev_info *mtd, int fd, int eb)
+{
+	int ret;
+	erase_info_t ei;
+
+	ei.start = eb * mtd->eb_size;
+	ei.length = mtd->eb_size;
+
+	ret = ioctl(fd, MEMISLOCKED, &ei);
+	if (ret < 0)
+		return mtd_ioctl_error(mtd, eb, "MEMISLOCKED");
+
+	return ret;
+}
+
 /* Patterns to write to a physical eraseblock when torturing it */
 static uint8_t patterns[] = {0xa5, 0x5a, 0x0};