[U-Boot] [PATCH 4/6] Unify active vs. redundant environment variable naming

Guennadi Liakhovetski lg at denx.de
Wed Aug 27 17:52:49 CEST 2008


Use consistent naming for active and redundant environment variables, remove
redundant erase struct initialisation by using separate structs for the
active and redundant environments.

Signed-off-by: Guennadi Liakhovetski <lg at denx.de>
---
 tools/env/fw_env.c |  129 +++++++++++++++++++++++++--------------------------
 1 files changed, 63 insertions(+), 66 deletions(-)

diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
index 2c82970..556aa85 100644
--- a/tools/env/fw_env.c
+++ b/tools/env/fw_env.c
@@ -52,14 +52,14 @@ typedef struct envdev_s {
 } envdev_t;
 
 static envdev_t envdevices[2];
-static int curdev;
+static int dev_current;
 
 #define DEVNAME(i)    envdevices[(i)].devname
 #define DEVOFFSET(i)  envdevices[(i)].devoff
 #define ENVSIZE(i)    envdevices[(i)].env_size
 #define DEVESIZE(i)   envdevices[(i)].erase_size
 
-#define CFG_ENV_SIZE ENVSIZE(curdev)
+#define CFG_ENV_SIZE ENVSIZE(dev_current)
 
 #define ENV_SIZE      getenvsize()
 
@@ -415,45 +415,47 @@ int fw_setenv (int argc, char *argv[])
 
 static int flash_write (void)
 {
-	int fd, fdr, rc, otherdev, resid;
-	erase_info_t erase;
+	int fd_current, fd_target, rc, dev_target, resid;
+	erase_info_t erase_current = {}, erase_target;
 	char *data = NULL;
 
-	if ((fd = open (DEVNAME (curdev), O_RDWR)) < 0) {
+	/* dev_current: fd_current, erase_current */
+	if ((fd_current = open (DEVNAME (dev_current), O_RDWR)) < 0) {
 		fprintf (stderr,
 			 "Can't open %s: %s\n",
-			 DEVNAME (curdev), strerror (errno));
+			 DEVNAME (dev_current), strerror (errno));
 		return -1;
 	}
 
 	if (HaveRedundEnv) {
 		/* switch to next partition for writing */
-		otherdev = !curdev;
-		if ((fdr = open (DEVNAME (otherdev), O_RDWR)) < 0) {
+		dev_target = !dev_current;
+		/* dev_target: fd_target, erase_target */
+		if ((fd_target = open (DEVNAME (dev_target), O_RDWR)) < 0) {
 			fprintf (stderr,
 				 "Can't open %s: %s\n",
-				 DEVNAME (otherdev),
+				 DEVNAME (dev_target),
 				 strerror (errno));
 			return -1;
 		}
 	} else {
-		otherdev = curdev;
-		fdr = fd;
+		dev_target = dev_current;
+		fd_target = fd_current;
 	}
 	printf ("Unlocking flash...\n");
-	erase.length = DEVESIZE (otherdev);
-	erase.start = DEVOFFSET (otherdev);
-	ioctl (fdr, MEMUNLOCK, &erase);
+	erase_target.length = DEVESIZE (dev_target);
+	erase_target.start = DEVOFFSET (dev_target);
+	ioctl (fd_target, MEMUNLOCK, &erase_target);
 
 	if (HaveRedundEnv) {
-		erase.length = DEVESIZE (curdev);
-		erase.start = DEVOFFSET (curdev);
-		ioctl (fd, MEMUNLOCK, &erase);
+		erase_current.length = DEVESIZE (dev_current);
+		erase_current.start = DEVOFFSET (dev_current);
+		ioctl (fd_current, MEMUNLOCK, &erase_current);
 		ENV_FLAGS(environment) = active_flag;
 	}
 
 	printf ("Done\n");
-	resid = DEVESIZE (otherdev) - CFG_ENV_SIZE;
+	resid = DEVESIZE (dev_target) - CFG_ENV_SIZE;
 	if (resid) {
 		if ((data = malloc (resid)) == NULL) {
 			fprintf (stderr,
@@ -462,17 +464,17 @@ static int flash_write (void)
 				 strerror (errno));
 			return -1;
 		}
-		if (lseek (fdr, DEVOFFSET (otherdev) + CFG_ENV_SIZE, SEEK_SET)
-		    == -1) {
+		if (lseek (fd_target, DEVOFFSET (dev_target) + CFG_ENV_SIZE,
+			   SEEK_SET) == -1) {
 			fprintf (stderr, "seek error on %s: %s\n",
-				 DEVNAME (otherdev),
+				 DEVNAME (dev_target),
 				 strerror (errno));
 			return -1;
 		}
-		if ((rc = read (fdr, data, resid)) != resid) {
+		if ((rc = read (fd_target, data, resid)) != resid) {
 			fprintf (stderr,
 				 "read error on %s: %s\n",
-				 DEVNAME (otherdev),
+				 DEVNAME (dev_target),
 				 strerror (errno));
 			return -1;
 		}
@@ -480,80 +482,75 @@ static int flash_write (void)
 
 	printf ("Erasing old environment...\n");
 
-	erase.length = DEVESIZE (otherdev);
-	erase.start = DEVOFFSET (otherdev);
-	if (ioctl (fdr, MEMERASE, &erase) != 0) {
+	if (ioctl (fd_target, MEMERASE, &erase_target) != 0) {
 		fprintf (stderr, "MTD erase error on %s: %s\n",
-			 DEVNAME (otherdev),
+			 DEVNAME (dev_target),
 			 strerror (errno));
 		return -1;
 	}
 
 	printf ("Done\n");
 
-	printf ("Writing environment to %s...\n", DEVNAME (otherdev));
-	if (lseek (fdr, DEVOFFSET (otherdev), SEEK_SET) == -1) {
+	printf ("Writing environment to %s...\n", DEVNAME (dev_target));
+	if (lseek (fd_target, DEVOFFSET (dev_target), SEEK_SET) == -1) {
 		fprintf (stderr,
 			 "seek error on %s: %s\n",
-			 DEVNAME (otherdev), strerror (errno));
+			 DEVNAME (dev_target), strerror (errno));
 		return -1;
 	}
 
-	if (write (fdr, environment.image, CFG_ENV_SIZE) != CFG_ENV_SIZE) {
+	if (write (fd_target, environment.image, CFG_ENV_SIZE) !=
+	    CFG_ENV_SIZE) {
 		fprintf (stderr,
 			 "Write error on %s: %s\n",
-			 DEVNAME (otherdev), strerror (errno));
+			 DEVNAME (dev_target), strerror (errno));
 		return -1;
 	}
 
 	if (resid) {
-		if (write (fdr, data, resid) != resid) {
+		if (write (fd_target, data, resid) != resid) {
 			fprintf (stderr,
 				 "write error on %s: %s\n",
-				 DEVNAME (curdev), strerror (errno));
+				 DEVNAME (dev_current), strerror (errno));
 			return -1;
 		}
 		free (data);
 	}
 	if (HaveRedundEnv) {
 		/* change flag on current active env partition */
-		if (lseek (fd, DEVOFFSET (curdev) + sizeof (ulong), SEEK_SET)
-		    == -1) {
+		if (lseek (fd_current, DEVOFFSET (dev_current) + sizeof (ulong),
+			   SEEK_SET) == -1) {
 			fprintf (stderr, "seek error on %s: %s\n",
-				 DEVNAME (curdev), strerror (errno));
+				 DEVNAME (dev_current), strerror (errno));
 			return -1;
 		}
-		if (write (fd, &obsolete_flag, sizeof (obsolete_flag)) !=
-		    sizeof (obsolete_flag)) {
+		if (write (fd_current, &obsolete_flag,
+			   sizeof (obsolete_flag)) != sizeof (obsolete_flag)) {
 			fprintf (stderr,
 				 "Write error on %s: %s\n",
-				 DEVNAME (curdev), strerror (errno));
+				 DEVNAME (dev_current), strerror (errno));
 			return -1;
 		}
 	}
 	printf ("Done\n");
 	printf ("Locking ...\n");
-	erase.length = DEVESIZE (otherdev);
-	erase.start = DEVOFFSET (otherdev);
-	ioctl (fdr, MEMLOCK, &erase);
+	ioctl (fd_target, MEMLOCK, &erase_target);
 	if (HaveRedundEnv) {
-		erase.length = DEVESIZE (curdev);
-		erase.start = DEVOFFSET (curdev);
-		ioctl (fd, MEMLOCK, &erase);
-		if (close (fdr)) {
+		ioctl (fd_current, MEMLOCK, &erase_current);
+		if (close (fd_target)) {
 			fprintf (stderr,
 				 "I/O error on %s: %s\n",
-				 DEVNAME (otherdev),
+				 DEVNAME (dev_target),
 				 strerror (errno));
 			return -1;
 		}
 	}
 	printf ("Done\n");
 
-	if (close (fd)) {
+	if (close (fd_current)) {
 		fprintf (stderr,
 			 "I/O error on %s: %s\n",
-			 DEVNAME (curdev), strerror (errno));
+			 DEVNAME (dev_current), strerror (errno));
 		return -1;
 	}
 
@@ -565,30 +562,30 @@ static int flash_read (void)
 {
 	int fd;
 
-	if ((fd = open (DEVNAME (curdev), O_RDONLY)) < 0) {
+	if ((fd = open (DEVNAME (dev_current), O_RDONLY)) < 0) {
 		fprintf (stderr,
 			 "Can't open %s: %s\n",
-			 DEVNAME (curdev), strerror (errno));
+			 DEVNAME (dev_current), strerror (errno));
 		return -1;
 	}
 
-	if (lseek (fd, DEVOFFSET (curdev), SEEK_SET) == -1) {
+	if (lseek (fd, DEVOFFSET (dev_current), SEEK_SET) == -1) {
 		fprintf (stderr,
 			 "seek error on %s: %s\n",
-			 DEVNAME (curdev), strerror (errno));
+			 DEVNAME (dev_current), strerror (errno));
 		return -1;
 	}
 	if (read (fd, environment.image, CFG_ENV_SIZE) != CFG_ENV_SIZE) {
 		fprintf (stderr,
 			 "Read error on %s: %s\n",
-			 DEVNAME (curdev), strerror (errno));
+			 DEVNAME (dev_current), strerror (errno));
 		return -1;
 	}
 
 	if (close (fd)) {
 		fprintf (stderr,
 			 "I/O error on %s: %s\n",
-			 DEVNAME (curdev), strerror (errno));
+			 DEVNAME (dev_current), strerror (errno));
 		return -1;
 	}
 
@@ -638,7 +635,7 @@ static int env_init (void)
 	environment.image = (union env_image *)addr1;
 	environment.data = HaveRedundEnv ? environment.image->redund.data :
 		environment.image->single.data;
-	curdev = 0;
+	dev_current = 0;
 	if (flash_read ()) {
 		return errno;
 	}
@@ -654,7 +651,7 @@ static int env_init (void)
 	} else {
 		flag1 = ENV_FLAGS(environment);
 
-		curdev = 1;
+		dev_current = 1;
 		if ((addr2 = calloc (1, CFG_ENV_SIZE)) == NULL) {
 			fprintf (stderr,
 				"Not enough memory for environment (%ld bytes)\n",
@@ -676,47 +673,47 @@ static int env_init (void)
 			environment.image = (union env_image *)addr1;
 			ENV_FLAGS(environment) = flag1;
 			environment.image->redund.crc = crc1;
-			curdev = 0;
+			dev_current = 0;
 			free (addr2);
 		} else if (!crc1_ok && crc2_ok) {
 			environment.image = (union env_image *)addr2;
 			ENV_FLAGS(environment) = flag2;
 			environment.image->redund.crc = crc2;
-			curdev = 1;
+			dev_current = 1;
 			free (addr1);
 		} else if (!crc1_ok && !crc2_ok) {
 			fprintf (stderr,
 				"Warning: Bad CRC, using default environment\n");
 			memcpy(environment.image->redund.data,
 			       default_environment, sizeof default_environment);
-			curdev = 0;
+			dev_current = 0;
 			free (addr1);
 		/* From here: both CRCs correct */
 		} else if (flag1 == active_flag && flag2 == obsolete_flag) {
 			environment.image = (union env_image *)addr1;
 			ENV_FLAGS(environment) = flag1;
 			environment.image->redund.crc = crc1;
-			curdev = 0;
+			dev_current = 0;
 			free (addr2);
 		} else if (flag1 == obsolete_flag && flag2 == active_flag) {
 			environment.image = (union env_image *)addr2;
 			ENV_FLAGS(environment) = flag2;
 			environment.image->redund.crc = crc2;
-			curdev = 1;
+			dev_current = 1;
 			free (addr1);
 		/* From here: invalid flag configuration */
 		} else if (flag1 == flag2) {
 			environment.image = (union env_image *)addr1;
 			ENV_FLAGS(environment) = flag1;
 			environment.image->redund.crc = crc1;
-			curdev = 0;
+			dev_current = 0;
 			free (addr2);
 		} else if (flag1 == obsolete_flag || flag1 == active_flag) {
 			/* flag1 valid, update flag2 */
 			environment.image = (union env_image *)addr1;
 			ENV_FLAGS(environment) = flag1;
 			environment.image->redund.crc = crc1;
-			curdev = 0;
+			dev_current = 0;
 			free (addr2);
 		} else {
 			if (flag2 != obsolete_flag && flag2 != active_flag)
@@ -725,7 +722,7 @@ static int env_init (void)
 			environment.image = (union env_image *)addr2;
 			ENV_FLAGS(environment) = flag2;
 			environment.image->redund.crc = crc2;
-			curdev = 1;
+			dev_current = 1;
 			free (addr1);
 		}
 		environment.data = environment.image->redund.data;
-- 
1.5.4



More information about the U-Boot mailing list