opaque: Move probing into PREPARE_POST_PROBE hook

To get rid of the flash context in probing, we have to split the
detection of chip parameters out into a preparation step. Below
the opaque-master API, things could actually stay as they are,
we rename `.probe` to `.prepare` though for completeness.

Change-Id: Ic8418e6c5bdc49f5133cdb21efed0bf6dc2f820e
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/449
diff --git a/amd_rom3read.c b/amd_rom3read.c
index 9708ea9..a8c6648 100644
--- a/amd_rom3read.c
+++ b/amd_rom3read.c
@@ -86,7 +86,7 @@
 	return 0;
 }
 
-static int rom3read_probe(struct flashctx *const flash)
+static int rom3read_prepare(struct flashctx *const flash)
 {
 	const struct spi100 *const spi100 = flash->mst.opaque->data;
 	const void *const rom3 = spi100->memory;
@@ -151,7 +151,7 @@
 static const struct opaque_master rom3read_master = {
 	.max_data_read	= MAX_DATA_UNSPECIFIED,
 	.max_data_write	= MAX_DATA_UNSPECIFIED,
-	.probe		= rom3read_probe,
+	.prepare	= rom3read_prepare,
 	.read		= rom3read_read,
 	.write		= rom3read_write,
 	.erase		= rom3read_erase,
diff --git a/flashchips.c b/flashchips.c
index dbcb11f..6a99456 100644
--- a/flashchips.c
+++ b/flashchips.c
@@ -27371,6 +27371,7 @@
 		},
 		.write		= write_opaque,
 		.read		= read_opaque,
+		.prepare_access	= prepare_opaque,
 	},
 
 	{
diff --git a/ichspi.c b/ichspi.c
index 36e1dce..65e79cf 100644
--- a/ichspi.c
+++ b/ichspi.c
@@ -1248,7 +1248,7 @@
 	return 0;
 }
 
-static int ich_hwseq_probe(struct flashctx *flash)
+static int ich_hwseq_prepare(struct flashctx *flash)
 {
 	uint32_t total_size, boundary;
 	uint32_t erase_size_low, size_low, erase_size_high, size_high;
@@ -1687,7 +1687,7 @@
 static const struct opaque_master opaque_master_ich_hwseq = {
 	.max_data_read	= 64,
 	.max_data_write	= 64,
-	.probe		= ich_hwseq_probe,
+	.prepare	= ich_hwseq_prepare,
 	.read		= ich_hwseq_read,
 	.write		= ich_hwseq_write,
 	.erase		= ich_hwseq_block_erase,
diff --git a/include/chipdrivers/opaque.h b/include/chipdrivers/opaque.h
index a621f66..d172861 100644
--- a/include/chipdrivers/opaque.h
+++ b/include/chipdrivers/opaque.h
@@ -20,6 +20,9 @@
 
 struct flashprog_flashctx;
 
+enum preparation_steps;
+int prepare_opaque(struct flashprog_flashctx *, enum preparation_steps);
+
 int probe_opaque(struct flashprog_flashctx *);
 int read_opaque(struct flashprog_flashctx *, uint8_t *buf, unsigned int start, unsigned int len);
 int write_opaque(struct flashprog_flashctx *, const uint8_t *buf, unsigned int start, unsigned int len);
diff --git a/include/programmer.h b/include/programmer.h
index 589765b..7da687d 100644
--- a/include/programmer.h
+++ b/include/programmer.h
@@ -431,7 +431,7 @@
 	int max_data_read;
 	int max_data_write;
 	/* Specific functions for this master */
-	int (*probe) (struct flashctx *flash);
+	int (*prepare) (struct flashctx *flash);
 	int (*read) (struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
 	int (*write) (struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len);
 	int (*erase) (struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen);
diff --git a/linux_mtd.c b/linux_mtd.c
index 3725920..d4c0600 100644
--- a/linux_mtd.c
+++ b/linux_mtd.c
@@ -173,7 +173,7 @@
 	return 0;
 }
 
-static int linux_mtd_probe(struct flashctx *flash)
+static int linux_mtd_prepare(struct flashctx *flash)
 {
 	struct linux_mtd_data *data = flash->mst.opaque->data;
 
@@ -307,7 +307,7 @@
 	/* max_data_{read,write} don't have any effect for this programmer */
 	.max_data_read	= MAX_DATA_UNSPECIFIED,
 	.max_data_write	= MAX_DATA_UNSPECIFIED,
-	.probe		= linux_mtd_probe,
+	.prepare	= linux_mtd_prepare,
 	.read		= linux_mtd_read,
 	.write		= linux_mtd_write,
 	.erase		= linux_mtd_erase,
diff --git a/nicintel_eeprom.c b/nicintel_eeprom.c
index 2839e7b..207782c 100644
--- a/nicintel_eeprom.c
+++ b/nicintel_eeprom.c
@@ -103,7 +103,7 @@
 	return (device_id & 0xfff0) == 0x1530;
 }
 
-static int nicintel_ee_probe_i210(struct flashctx *flash)
+static int nicintel_ee_prepare_i210(struct flashctx *flash)
 {
 	/* Emulated eeprom has a fixed size of 4 KB */
 	flash->chip->total_size = 4;
@@ -116,7 +116,7 @@
 	return 1;
 }
 
-static int nicintel_ee_probe_82580(struct flashctx *flash)
+static int nicintel_ee_prepare_82580(struct flashctx *flash)
 {
 	if (nicintel_pci->device_id == UNPROG_DEVICE)
 		flash->chip->total_size = 16; /* Fall back to minimum supported size. */
@@ -406,7 +406,7 @@
 static int nicintel_ee_shutdown_82580(void *eecp);
 
 static const struct opaque_master opaque_master_nicintel_ee_82580 = {
-	.probe		= nicintel_ee_probe_82580,
+	.prepare	= nicintel_ee_prepare_82580,
 	.read		= nicintel_ee_read,
 	.write		= nicintel_ee_write_82580,
 	.erase		= nicintel_ee_erase_82580,
@@ -416,7 +416,7 @@
 static int nicintel_ee_shutdown_i210(void *arg);
 
 static const struct opaque_master opaque_master_nicintel_ee_i210 = {
-	.probe		= nicintel_ee_probe_i210,
+	.prepare	= nicintel_ee_prepare_i210,
 	.read		= nicintel_ee_read,
 	.write		= nicintel_ee_write_i210,
 	.erase		= nicintel_ee_erase_i210,
diff --git a/opaque.c b/opaque.c
index 33ba766..a005d91 100644
--- a/opaque.c
+++ b/opaque.c
@@ -28,7 +28,14 @@
 
 int probe_opaque(struct flashctx *flash)
 {
-	return flash->mst.opaque->probe(flash);
+	return 1;
+}
+
+int prepare_opaque(struct flashctx *flash, enum preparation_steps step)
+{
+	if (step != PREPARE_POST_PROBE)
+		return 0;
+	return flash->mst.opaque->prepare(flash) ? 0 : -1;
 }
 
 int read_opaque(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
@@ -57,7 +64,7 @@
 		}
 	}
 
-	if (!mst->probe || !mst->read || !mst->write || !mst->erase) {
+	if (!mst->prepare || !mst->read || !mst->write || !mst->erase) {
 		msg_perr("%s called with incomplete master definition.\n"
 			 "Please report a bug at flashprog@flashprog.org\n",
 			 __func__);