diff mbox series

[[libubootenv,1/2] Add configuration for disabling MTD lock

Message ID 20210722115839.223096-1-sbabic@denx.de
State Accepted
Headers show
Series [[libubootenv,1/2] Add configuration for disabling MTD lock | expand

Commit Message

Stefano Babic July 22, 2021, 11:58 a.m. UTC
Some flashes have issue by locking/unlocking. The additional flag in
configuration file allows to disable the LOCK/UNLOCK mechanism in MTD.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 src/uboot_env.c     | 18 ++++++++++++------
 src/uboot_private.h |  2 ++
 2 files changed, 14 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/src/uboot_env.c b/src/uboot_env.c
index b97507a..34962e6 100644
--- a/src/uboot_env.c
+++ b/src/uboot_env.c
@@ -51,6 +51,11 @@ 
 	    ((tvar) = LIST_NEXT((var), field), 1);			\
 	    (var) = (tvar))
 
+#define MTDLOCK(dev, psector)	\
+	if (!dev->disable_mtd_lock) ioctl (dev->fd, MEMLOCK, psector)
+#define MTDUNLOCK(dev, psector) \
+	if (!dev->disable_mtd_lock) ioctl (dev->fd, MEMUNLOCK, psector)
+
 /*
  * The lockfile is the same as defined in U-Boot for
  * the fw_printenv utilities
@@ -739,7 +744,7 @@  static int mtdwrite(struct uboot_flash_env *dev, void *data)
 			/*
 			 * unlock could fail, no check
 			 */
-			ioctl(dev->fd, MEMUNLOCK, &erase);
+			MTDUNLOCK(dev, &erase);
 			if (ioctl(dev->fd, MEMERASE, &erase) != 0) {
 				ret =-EIO;
 				goto devwrite_out;
@@ -752,7 +757,7 @@  static int mtdwrite(struct uboot_flash_env *dev, void *data)
 				ret =-EIO;
 				goto devwrite_out;
 			}
-			ioctl(dev->fd, MEMLOCK, &erase);
+			MTDLOCK(dev, &erase);
 			start += dev->sectorsize;
 			buf += blocksize;
 			count -= blocksize;
@@ -832,13 +837,13 @@  static int set_obsolete_flag(struct uboot_flash_env *dev)
 	}
 	erase.start = dev->offset;
 	erase.length = dev->sectorsize;
-	ioctl(dev->fd, MEMUNLOCK, &erase);
+	MTDUNLOCK(dev, &erase);
 	ret = write(dev->fd, &flag, sizeof(flag));
 	if (ret == sizeof(flag))
 		ret = 0;
 	else if (ret >= 0)
 		ret = -EIO;
-	ioctl (dev->fd, MEMLOCK, &erase);
+	MTDLOCK(dev, &erase);
 	close(dev->fd);
 
 	return ret;
@@ -1216,12 +1221,13 @@  int libuboot_read_config(struct uboot_ctx *ctx, const char *config)
 		if (line[0] == '#')
 			continue;
 
-		ret = sscanf(line, "%ms %lli %zx %zx %lx",
+		ret = sscanf(line, "%ms %lli %zx %zx %lx %d",
 				&tmp,
 				&dev->offset,
 				&dev->envsize,
 				&dev->sectorsize,
-				&dev->envsectors);
+				&dev->envsectors,
+				&dev->disable_mtd_lock);
 
 		/*
 		 * At least name offset and size should be set
diff --git a/src/uboot_private.h b/src/uboot_private.h
index 4b7a9f9..a179584 100644
--- a/src/uboot_private.h
+++ b/src/uboot_private.h
@@ -87,6 +87,8 @@  struct uboot_flash_env {
 	enum flags_type		flagstype;
 	/** type of device (mtd, ubi, file, ....) */
 	enum device_type	device_type;
+	/** Disable lock mechanism (required by some flashes */
+	int disable_mtd_lock;
 };
 
 /** Internal structure for an environment variable