Add count_usable_erasers which returns the number of well-defined erasers for a chip

It solves one FIXME and consequentially allows to remove a later check
right now, and is used in the upcoming SFDP patch.
Adds a forward declaration of check_block_eraser.

Corresponding to flashrom svn r1358.

Signed-off-by: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
Acked-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
diff --git a/flashrom.c b/flashrom.c
index 5bf9645..12a51ad 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -465,6 +465,8 @@
  */
 static int may_register_shutdown = 0;
 
+static int check_block_eraser(const struct flashchip *flash, int k, int log);
+
 /* Register a function to be executed on programmer shutdown.
  * The advantage over atexit() is that you can supply a void pointer which will
  * be used as parameter to the registered function upon programmer shutdown.
@@ -711,6 +713,19 @@
 	return extract_param(&programmer_param, param_name, ",");
 }
 
+/* Returns the number of well-defined erasers for a chip.
+ * The log parameter controls output. */
+static unsigned int count_usable_erasers(const struct flashchip *flash, int log)
+{
+	unsigned int usable_erasefunctions = 0;
+	int k;
+	for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
+		if (!check_block_eraser(flash, k, 0))
+			usable_erasefunctions++;
+	}
+	return usable_erasefunctions;
+}
+
 /* start is an offset to the base address of the flash chip */
 int check_erased_range(struct flashchip *flash, int start, int len)
 {
@@ -1467,7 +1482,7 @@
 	return 0;
 }
 
-static int check_block_eraser(struct flashchip *flash, int k, int log)
+static int check_block_eraser(const struct flashchip *flash, int k, int log)
 {
 	struct block_eraser eraser = flash->block_erasers[k];
 
@@ -1496,18 +1511,9 @@
 	int k, ret = 0;
 	uint8_t *curcontents;
 	unsigned long size = flash->total_size * 1024;
-	int usable_erasefunctions = 0;
+	unsigned int usable_erasefunctions = count_usable_erasers(flash, 0);
 
-	for (k = 0; k < NUM_ERASEFUNCTIONS; k++)
-		if (!check_block_eraser(flash, k, 0))
-			usable_erasefunctions++;
 	msg_cinfo("Erasing and writing flash chip... ");
-	if (!usable_erasefunctions) {
-		msg_cerr("ERROR: flashrom has no erase function for this flash "
-			 "chip.\n");
-		return 1;
-	}
-
 	curcontents = (uint8_t *) malloc(size);
 	/* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
 	memcpy(curcontents, oldcontents, size);
@@ -1825,7 +1831,11 @@
 				return 1;
 			msg_cerr("Continuing anyway.\n");
 		}
-		/* FIXME: Check if at least one erase function exists. */
+		if(count_usable_erasers(flash, 0) == 0) {
+			msg_cerr("flashrom has no erase function for this "
+				 "flash chip.\n");
+			return 1;
+		}
 	}
 	if (write_it) {
 		if (flash->tested & TEST_BAD_WRITE) {