Patchwork libmtd: unify erase block argument checking

login
register
mail settings
Submitter Mike Frysinger
Date Sept. 24, 2010, 1:51 a.m.
Message ID <1285293113-24844-1-git-send-email-vapier@gentoo.org>
Download mbox | patch
Permalink /patch/65612/
State Accepted, archived
Commit da3106bf16a7c065e2205297136c6c60b0562dc3
Headers show

Comments

Mike Frysinger - Sept. 24, 2010, 1:51 a.m.
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
---
 lib/libmtd.c |   68 +++++++++++++++++++++++++++------------------------------
 1 files changed, 32 insertions(+), 36 deletions(-)
Artem Bityutskiy - Sept. 24, 2010, 12:23 p.m.
On Thu, 2010-09-23 at 21:51 -0400, Mike Frysinger wrote:
> Signed-off-by: Mike Frysinger <vapier@gentoo.org>
> ---
>  lib/libmtd.c |   68 +++++++++++++++++++++++++++------------------------------
>  1 files changed, 32 insertions(+), 36 deletions(-)

Pushed, thanks!

Patch

diff --git a/lib/libmtd.c b/lib/libmtd.c
index 0010790..d57ae8f 100644
--- a/lib/libmtd.c
+++ b/lib/libmtd.c
@@ -791,19 +791,27 @@  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_erase(libmtd_t desc, const struct mtd_dev_info *mtd, int fd, int eb)
+static int mtd_valid_erase_block(const struct mtd_dev_info *mtd, int eb)
 {
-	int ret;
-	struct libmtd *lib = (struct libmtd *)desc;
-	struct erase_info_user64 ei64;
-	struct erase_info_user ei;
-
 	if (eb < 0 || eb >= mtd->eb_cnt) {
 		errmsg("bad eraseblock number %d, mtd%d has %d eraseblocks",
 		       eb, mtd->mtd_num, mtd->eb_cnt);
 		errno = EINVAL;
 		return -1;
 	}
+	return 0;
+}
+
+int mtd_erase(libmtd_t desc, const struct mtd_dev_info *mtd, int fd, int eb)
+{
+	int ret;
+	struct libmtd *lib = (struct libmtd *)desc;
+	struct erase_info_user64 ei64;
+	struct erase_info_user ei;
+
+	ret = mtd_valid_erase_block(mtd, eb);
+	if (ret)
+		return ret;
 
 	ei64.start = (__u64)eb * mtd->eb_size;
 	ei64.length = mtd->eb_size;
@@ -930,12 +938,9 @@  int mtd_is_bad(const struct mtd_dev_info *mtd, int fd, int eb)
 	int ret;
 	loff_t seek;
 
-	if (eb < 0 || eb >= mtd->eb_cnt) {
-		errmsg("bad eraseblock number %d, mtd%d has %d eraseblocks",
-		       eb, mtd->mtd_num, mtd->eb_cnt);
-		errno = EINVAL;
-		return -1;
-	}
+	ret = mtd_valid_erase_block(mtd, eb);
+	if (ret)
+		return ret;
 
 	if (!mtd->bb_allowed)
 		return 0;
@@ -958,12 +963,9 @@  int mtd_mark_bad(const struct mtd_dev_info *mtd, int fd, int eb)
 		return -1;
 	}
 
-	if (eb < 0 || eb >= mtd->eb_cnt) {
-		errmsg("bad eraseblock number %d, mtd%d has %d eraseblocks",
-		       eb, mtd->mtd_num, mtd->eb_cnt);
-		errno = EINVAL;
-		return -1;
-	}
+	ret = mtd_valid_erase_block(mtd, eb);
+	if (ret)
+		return ret;
 
 	seek = (loff_t)eb * mtd->eb_size;
 	ret = ioctl(fd, MEMSETBADBLOCK, &seek);
@@ -979,12 +981,10 @@  int mtd_read(const struct mtd_dev_info *mtd, int fd, int eb, int offs,
 	int ret, rd = 0;
 	off_t seek;
 
-	if (eb < 0 || eb >= mtd->eb_cnt) {
-		errmsg("bad eraseblock number %d, mtd%d has %d eraseblocks",
-		       eb, mtd->mtd_num, mtd->eb_cnt);
-		errno = EINVAL;
-		return -1;
-	}
+	ret = mtd_valid_erase_block(mtd, eb);
+	if (ret)
+		return ret;
+
 	if (offs < 0 || offs + len > mtd->eb_size) {
 		errmsg("bad offset %d or length %d, mtd%d eraseblock size is %d",
 		       offs, len, mtd->mtd_num, mtd->eb_size);
@@ -1015,12 +1015,10 @@  int mtd_write(const struct mtd_dev_info *mtd, int fd, int eb, int offs,
 	int ret;
 	off_t seek;
 
-	if (eb < 0 || eb >= mtd->eb_cnt) {
-		errmsg("bad eraseblock number %d, mtd%d has %d eraseblocks",
-		       eb, mtd->mtd_num, mtd->eb_cnt);
-		errno = EINVAL;
-		return -1;
-	}
+	ret = mtd_valid_erase_block(mtd, eb);
+	if (ret)
+		return ret;
+
 	if (offs < 0 || offs + len > mtd->eb_size) {
 		errmsg("bad offset %d or length %d, mtd%d eraseblock size is %d",
 		       offs, len, mtd->mtd_num, mtd->eb_size);
@@ -1157,12 +1155,10 @@  int mtd_write_img(const struct mtd_dev_info *mtd, int fd, int eb, int offs,
 	struct stat st;
 	char *buf;
 
-	if (eb < 0 || eb >= mtd->eb_cnt) {
-		errmsg("bad eraseblock number %d, mtd%d has %d eraseblocks",
-		       eb, mtd->mtd_num, mtd->eb_cnt);
-		errno = EINVAL;
-		return -1;
-	}
+	ret = mtd_valid_erase_block(mtd, eb);
+	if (ret)
+		return ret;
+
 	if (offs < 0 || offs >= mtd->eb_size) {
 		errmsg("bad offset %d, mtd%d eraseblock size is %d",
 		       offs, mtd->mtd_num, mtd->eb_size);