sfdp: Move hints about SFDP detection into spi_prepare_sfdp()

Now that the preparation is only run when we are actually going to use
the SFDP definition, we can do the logging there, unconditionally.

Change-Id: I9abb22178799c2d2f1098f0d578ac3231983fb19
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/487
diff --git a/flashprog.c b/flashprog.c
index 0b802a7..8746e4e 100644
--- a/flashprog.c
+++ b/flashprog.c
@@ -332,7 +332,7 @@
 }
 
 /* Returns the number of well-defined erasers for a chip. */
-static unsigned int count_usable_erasers(const struct flashctx *flash)
+unsigned int flashprog_count_usable_erasers(const struct flashctx *flash)
 {
 	unsigned int usable_erasefunctions = 0;
 	int k;
@@ -755,28 +755,6 @@
 		 * one for this programmer interface (master) and thus no other chip has
 		 * been found on this interface.
 		 */
-		if (startchip == 0 && flash->chip->id.model == SFDP_DEVICE_ID) {
-			msg_cinfo("===\n"
-				  "SFDP has autodetected a flash chip which is "
-				  "not natively supported by flashprog yet.\n");
-			if (count_usable_erasers(flash) == 0)
-				msg_cinfo("The standard operations read and "
-					  "verify should work, but to support "
-					  "erase, write and all other "
-					  "possible features");
-			else
-				msg_cinfo("All standard operations (read, "
-					  "verify, erase and write) should "
-					  "work, but to support all possible "
-					  "features");
-
-			msg_cinfo(" we need to add them manually.\n"
-				  "You can help us by mailing us the output of the following command to "
-				  "flashprog@flashprog.org:\n"
-				  "'flashprog -VV [plus the -p/--programmer parameter]'\n"
-				  "Thanks for your help!\n"
-				  "===\n");
-		}
 
 		/* First flash chip detected on this bus. */
 		if (startchip == 0)
@@ -1084,7 +1062,7 @@
 static int create_erase_layout(struct flashctx *const flashctx, struct erase_layout **e_layout)
 {
 	const struct flashchip *chip = flashctx->chip;
-	const size_t erasefn_count = count_usable_erasers(flashctx);
+	const size_t erasefn_count = flashprog_count_usable_erasers(flashctx);
 
 	if (!erasefn_count) {
 		msg_gerr("No erase functions supported\n");
@@ -1737,7 +1715,7 @@
 				return 1;
 			msg_cerr("Continuing anyway.\n");
 		}
-		if(count_usable_erasers(flash) == 0) {
+		if(flashprog_count_usable_erasers(flash) == 0) {
 			msg_cerr("flashprog has no erase function for this "
 				 "flash chip.\n");
 			return 1;
diff --git a/include/flash.h b/include/flash.h
index 31b4f0c..082448a 100644
--- a/include/flash.h
+++ b/include/flash.h
@@ -567,6 +567,7 @@
 int prepare_flash_access(struct flashctx *, bool read_it, bool write_it, bool erase_it, bool verify_it);
 void finalize_flash_access(struct flashctx *);
 int register_chip_restore(chip_restore_fn_cb_t func, struct flashctx *flash, uint8_t status);
+unsigned int flashprog_count_usable_erasers(const struct flashctx *);
 
 /* Something happened that shouldn't happen, but we can go on. */
 #define ERROR_NONFATAL 0x100
diff --git a/sfdp.c b/sfdp.c
index b8f3093..a324f9c 100644
--- a/sfdp.c
+++ b/sfdp.c
@@ -462,15 +462,35 @@
 		free(tbuf);
 	}
 
-	if (ret == 0 && selfcheck_chip(flash->chip, -1)) {
-		msg_cerr("SFDP parsing resulted in invalid chip structure.\n");
-		ret = SPI_FLASHPROG_BUG;
-	}
-
 cleanup_hdrs:
 	free(hdrs);
 	free(hbuf);
-	return ret;
+
+	if (ret)
+		return ret;
+
+	if (selfcheck_chip(flash->chip, -1)) {
+		msg_cerr("SFDP parsing resulted in invalid chip structure.\n");
+		return SPI_FLASHPROG_BUG;
+	}
+
+	msg_cinfo("===\n"
+		  "SFDP has autodetected a flash chip which is not natively supported\n"
+		  "by flashprog yet. ");
+	if (flashprog_count_usable_erasers(flash) == 0)
+		msg_cinfo("The standard operations read and verify should\n"
+			  "work, but to support erase, write and all other possible features\n");
+	else
+		msg_cinfo("All standard operations (read, verify, erase\n"
+			  "and write) should work, but to support all possible features\n");
+	msg_cinfo("we need to add them manually.\n"
+		  "You can help us by mailing us the output of the following command to\n"
+		  "flashprog@flashprog.org:\n"
+		  "'flashprog -VV [plus the -p/--programmer parameter]'\n"
+		  "Thanks for your help!\n"
+		  "===\n");
+
+	return 0;
 }
 
 struct found_id *probe_spi_sfdp(const struct bus_probe *probe,