Extract chip selfcheck into new selfcheck_chip() function

We're going to call it from the SFDP parser, too.

Change-Id: Ib526d005b84717d4be69cc2dff46cf628af4c9cd
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/392
diff --git a/flashprog.c b/flashprog.c
index 07025d9..b44e440 100644
--- a/flashprog.c
+++ b/flashprog.c
@@ -1488,6 +1488,48 @@
 	}
 }
 
+int selfcheck_chip(const struct flashchip *const chip, const int idx)
+{
+	int ret = 0;
+	const char *const name = chip->name != NULL ? chip->name : "unnamed";
+
+	if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
+		ret = 1;
+		msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
+			 "Please report a bug at flashprog@flashprog.org\n", idx, name);
+	}
+	if (chip->feature_bits &
+	    (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7 |
+	     FEATURE_ANY_DUAL | FEATURE_ANY_QUAD)
+	    && !chip->prepare_access) {
+		msg_gerr("ERROR: Flash chip #%d (%s) misses chip\n"
+			 "preparation function for 4BA and multi-i/o modes.\n"
+			 "Please report a bug at flashprog@flashprog.org\n", idx, name);
+		ret = 1;
+	}
+	uint8_t zero_cycles[sizeof(chip->dummy_cycles)] = { 0 };
+	if ((chip->feature_bits & (FEATURE_QPI_35_F5 | FEATURE_QPI_38_FF)) &&
+	    !memcmp(&chip->dummy_cycles, zero_cycles, sizeof(zero_cycles))) {
+		msg_gerr("ERROR: Flash chip #%d (%s) misses QPI dummy-cycle\n"
+			 "settings. Please report a bug at flashprog@flashprog.org\n",
+			 idx, name);
+		ret = 1;
+	}
+	if (chip->reg_bits.bp[0].reg != INVALID_REG &&
+	    (!chip->wp_write_cfg || !chip->wp_read_cfg ||
+	     !chip->wp_get_ranges || !chip->decode_range)) {
+		msg_gerr("ERROR: Flash chip #%d (%s) advertises block-protection\n"
+			 "bits, but misses one or more write-protection functions.\n"
+			 "Please report a bug at flashprog@flashprog.org\n", idx, name);
+		ret = 1;
+	}
+	if (selfcheck_eraseblocks(chip)) {
+		ret = 1;
+	}
+
+	return ret;
+}
+
 int selfcheck(void)
 {
 	unsigned int i;
@@ -1541,41 +1583,8 @@
 		ret = 1;
 	} else {
 		for (i = 0; i < flashchips_size - 1; i++) {
-			const struct flashchip *chip = &flashchips[i];
-			const char *const name = chip->name != NULL ? chip->name : "unnamed";
-			if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
+			if (selfcheck_chip(&flashchips[i], i))
 				ret = 1;
-				msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
-					 "Please report a bug at flashprog@flashprog.org\n", i, name);
-			}
-			if (chip->feature_bits &
-			    (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7 |
-			     FEATURE_ANY_DUAL | FEATURE_ANY_QUAD)
-			    && !chip->prepare_access) {
-				msg_gerr("ERROR: Flash chip #%d (%s) misses chip\n"
-					 "preparation function for 4BA and multi-i/o modes.\n"
-					 "Please report a bug at flashprog@flashprog.org\n", i, name);
-				ret = 1;
-			}
-			uint8_t zero_cycles[sizeof(chip->dummy_cycles)] = { 0 };
-			if ((chip->feature_bits & (FEATURE_QPI_35_F5 | FEATURE_QPI_38_FF)) &&
-			    !memcmp(&chip->dummy_cycles, zero_cycles, sizeof(zero_cycles))) {
-				msg_gerr("ERROR: Flash chip #%d (%s) misses QPI dummy-cycle\n"
-					 "settings. Please report a bug at flashprog@flashprog.org\n",
-					 i, name);
-				ret = 1;
-			}
-			if (chip->reg_bits.bp[0].reg != INVALID_REG &&
-			    (!chip->wp_write_cfg || !chip->wp_read_cfg ||
-			     !chip->wp_get_ranges || !chip->decode_range)) {
-				msg_gerr("ERROR: Flash chip #%d (%s) advertises block-protection\n"
-					 "bits, but misses one or more write-protection functions.\n"
-					 "Please report a bug at flashprog@flashprog.org\n", i, name);
-				ret = 1;
-			}
-			if (selfcheck_eraseblocks(chip)) {
-				ret = 1;
-			}
 		}
 	}
 
diff --git a/include/flash.h b/include/flash.h
index 67853cb..4304bb8 100644
--- a/include/flash.h
+++ b/include/flash.h
@@ -543,6 +543,7 @@
 int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len);
 void emergency_help_message(void);
 void list_programmers_linebreak(int startcol, int cols, int paren);
+int selfcheck_chip(const struct flashchip *, int idx);
 int selfcheck(void);
 int read_buf_from_file(unsigned char *buf, unsigned long size, const char *filename);
 int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename);