Do not read the flash chip twice in verification mode

Kyösti Mälkki noticed that we unnecessarily read the flash chip twice when
called with --verify. The first one is the mandatory read before everything
(to be able to detect the seriousness of errors), but the second one is not
necessary because we can just use the former for the comparison.

This introduces a small output change: previously we printed ERASE or
VERIFY depending on the callee. This special case has been dropped
because it is unnecessary to print it (and wrong for the verification
function to need to know why it is verifying exactly).
If an erase fails we mention that fact explicitly already, similar for verify.

Corresponding to flashrom svn r1619.

Signed-off-by: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
Acked-by: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
diff --git a/flash.h b/flash.h
index 0bb6c2b..526087c 100644
--- a/flash.h
+++ b/flash.h
@@ -231,7 +231,7 @@
 int max(int a, int b);
 void tolower_string(char *str);
 char *extract_param(char **haystack, const char *needle, const char *delim);
-int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len, const char *message);
+int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len);
 int need_erase(uint8_t *have, uint8_t *want, unsigned int len, enum write_granularity gran);
 char *strcat_realloc(char *dest, const char *src);
 void print_version(void);
diff --git a/flashrom.c b/flashrom.c
index 7d1ffa3..2299b06 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -544,6 +544,26 @@
 	return usable_erasefunctions;
 }
 
+int compare_range(uint8_t *wantbuf, uint8_t *havebuf, unsigned int start, unsigned int len)
+{
+	int ret = 0, failcount = 0;
+	unsigned int i;
+	for (i = 0; i < len; i++) {
+		if (wantbuf[i] != havebuf[i]) {
+			/* Only print the first failure. */
+			if (!failcount++)
+				msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
+					 start + i, wantbuf[i], havebuf[i]);
+		}
+	}
+	if (failcount) {
+		msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
+			 start, start + len - 1, failcount);
+		ret = -1;
+	}
+	return ret;
+}
+
 /* start is an offset to the base address of the flash chip */
 int check_erased_range(struct flashctx *flash, unsigned int start,
 		       unsigned int len)
@@ -556,7 +576,7 @@
 		exit(1);
 	}
 	memset(cmpbuf, 0xff, len);
-	ret = verify_range(flash, cmpbuf, start, len, "ERASE");
+	ret = verify_range(flash, cmpbuf, start, len);
 	free(cmpbuf);
 	return ret;
 }
@@ -566,15 +586,12 @@
  *		flash content at location start
  * @start	offset to the base address of the flash chip
  * @len		length of the verified area
- * @message	string to print in the "FAILED" message
  * @return	0 for success, -1 for failure
  */
-int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start,
-		 unsigned int len, const char *message)
+int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len)
 {
-	unsigned int i;
 	uint8_t *readbuf = malloc(len);
-	int ret = 0, failcount = 0;
+	int ret = 0;
 
 	if (!len)
 		goto out_free;
@@ -595,8 +612,6 @@
 		ret = -1;
 		goto out_free;
 	}
-	if (!message)
-		message = "VERIFY";
 
 	ret = flash->chip->read(flash, readbuf, start, len);
 	if (ret) {
@@ -605,22 +620,7 @@
 		return ret;
 	}
 
-	for (i = 0; i < len; i++) {
-		if (cmpbuf[i] != readbuf[i]) {
-			/* Only print the first failure. */
-			if (!failcount++)
-				msg_cerr("%s FAILED at 0x%08x! "
-					 "Expected=0x%02x, Read=0x%02x,",
-					 message, start + i, cmpbuf[i],
-					 readbuf[i]);
-		}
-	}
-	if (failcount) {
-		msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
-			 start, start + len - 1, failcount);
-		ret = -1;
-	}
-
+	ret = compare_range(cmpbuf, readbuf, start, len);
 out_free:
 	free(readbuf);
 	return ret;
@@ -1054,21 +1054,6 @@
 	return chip - flashchips;
 }
 
-int verify_flash(struct flashctx *flash, uint8_t *buf)
-{
-	int ret;
-	unsigned int total_size = flash->chip->total_size * 1024;
-
-	msg_cinfo("Verifying flash... ");
-
-	ret = verify_range(flash, buf, 0, total_size, NULL);
-
-	if (!ret)
-		msg_cinfo("VERIFIED.          \n");
-
-	return ret;
-}
-
 int read_buf_from_file(unsigned char *buf, unsigned long size,
 		       const char *filename)
 {
@@ -1424,10 +1409,8 @@
 {
 	msg_gerr("Your flash chip is in an unknown state.\n"
 		"Get help on IRC at chat.freenode.net (channel #flashrom) or\n"
-		"mail flashrom@flashrom.org with the subject "
-		"\"FAILED: <your board name>\"!\n"
-		"-------------------------------------------------------------"
-		  "------------------\n"
+		"mail flashrom@flashrom.org with the subject \"FAILED: <your board name>\"!\n"
+		"-------------------------------------------------------------------------------\n"
 		"DO NOT REBOOT OR POWEROFF!\n");
 }
 
@@ -1860,15 +1843,22 @@
 	}
 
 	if (verify_it) {
-		/* Work around chips which need some time to calm down. */
-		if (write_it)
+		msg_cinfo("Verifying flash... ");
+
+		if (write_it) {
+			/* Work around chips which need some time to calm down. */
 			programmer_delay(1000*1000);
-		ret = verify_flash(flash, newcontents);
-		/* If we tried to write, and verification now fails, we
-		 * might have an emergency situation.
-		 */
-		if (ret && write_it)
-			emergency_help_message();
+			ret = verify_range(flash, newcontents, 0, size);
+			/* If we tried to write, and verification now fails, we
+			 * might have an emergency situation.
+			 */
+			if (ret)
+				emergency_help_message();
+		} else {
+			ret = compare_range(newcontents, oldcontents, 0, size);
+		}
+		if (!ret)
+			msg_cinfo("VERIFIED.\n");
 	}
 
 out:
diff --git a/jedec.c b/jedec.c
index d8532c6..7bc9133 100644
--- a/jedec.c
+++ b/jedec.c
@@ -410,7 +410,7 @@
 
 	dst = d;
 	src = s;
-	failed = verify_range(flash, src, start, page_size, NULL);
+	failed = verify_range(flash, src, start, page_size);
 
 	if (failed && tried++ < MAX_REFLASH_TRIES) {
 		msg_cerr("retrying.\n");