>From f1a7323ebbc92c2124649160f008db23fd8e6d74 Mon Sep 17 00:00:00 2001
From: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
Date: Fri, 9 May 2014 18:24:11 +0200
Subject: [PATCH] Add two new states to enum test_state and use it for
 flashchips.

The new enum test_state looks like this:
enum test_state {
	OK = 0,
	NT = 1,	/* Not tested */
	BAD,	/* Known to not work */
	DEP,	/* Support depends on configuration (e.g. Intel flash descriptor) */
	NA,	/* Not applicable (e.g. write support on ROM chips) */
};

The second new state 'NA' is introduced, among other things, to indicate
the erase and write states of real ROMs correctly. This is also implemented
by this patch and required to exchange the previous bit mask in struct
flashchip with a new struct containing an enum test_state for each operation.
The -L output is changed accordingly to print '-' in the case of an N/A state
and the wiki output uses a new template producing a greyed out cell.
Previous users of enum test_state are not affected by this change (yet).

Signed-off-by: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
---
 flash.h      | 59 +++++++++++++++++++++++++++--------------------------------
 flashchips.c |  4 ++--
 flashrom.c   | 40 +++++++++++++++++++++++-----------------
 ichspi.c     |  5 ++++-
 print.c      | 24 ++++++++++++++++--------
 print_wiki.c | 40 ++++++++++++++++++++++++++++++----------
 6 files changed, 102 insertions(+), 70 deletions(-)

diff --git a/flash.h b/flash.h
index 59f7cd4..b72bfde 100644
--- a/flash.h
+++ b/flash.h
@@ -120,6 +120,26 @@ enum write_granularity {
 #define FEATURE_OTP		(1 << 8)
 #define FEATURE_QPI		(1 << 9)
 
+enum test_state {
+	OK = 0,
+	NT = 1,	/* Not tested */
+	BAD,	/* Known to not work */
+	DEP,	/* Support depends on configuration (e.g. Intel flash descriptor) */
+	NA,	/* Not applicable (e.g. write support on ROM chips) */
+};
+
+#define TEST_UNTESTED	{ .probe = NT, .read = NT, .erase = NT, .write = NT }
+
+#define TEST_OK_PROBE	{ .probe = OK, .read = NT, .erase = NT, .write = NT }
+#define TEST_OK_PR	{ .probe = OK, .read = OK, .erase = NT, .write = NT }
+#define TEST_OK_PRE	{ .probe = OK, .read = OK, .erase = OK, .write = NT }
+#define TEST_OK_PREW	{ .probe = OK, .read = OK, .erase = OK, .write = OK }
+
+#define TEST_BAD_PROBE	{ .probe = BAD, .read = NT, .erase = NT, .write = NT }
+#define TEST_BAD_PR	{ .probe = BAD, .read = BAD, .erase = NT, .write = NT }
+#define TEST_BAD_PRE	{ .probe = BAD, .read = BAD, .erase = BAD, .write = NT }
+#define TEST_BAD_PREW	{ .probe = BAD, .read = BAD, .erase = BAD, .write = BAD }
+
 struct flashctx;
 typedef int (erasefunc_t)(struct flashctx *flash, unsigned int addr, unsigned int blocklen);
 
@@ -143,11 +163,13 @@ struct flashchip {
 	unsigned int page_size;
 	int feature_bits;
 
-	/*
-	 * Indicate if flashrom has been tested with this flash chip and if
-	 * everything worked correctly.
-	 */
-	uint32_t tested;
+	/* Indicate how well flashrom supports different operations of this flash chip. */
+	struct tested {
+		enum test_state probe;
+		enum test_state read;
+		enum test_state erase;
+		enum test_state write;
+	} tested;
 
 	int (*probe) (struct flashctx *flash);
 
@@ -192,27 +214,6 @@ struct flashctx {
 	struct registered_programmer *pgm;
 };
 
-#define TEST_UNTESTED	0
-
-#define TEST_OK_PROBE	(1 << 0)
-#define TEST_OK_READ	(1 << 1)
-#define TEST_OK_ERASE	(1 << 2)
-#define TEST_OK_WRITE	(1 << 3)
-#define TEST_OK_PR	(TEST_OK_PROBE | TEST_OK_READ)
-#define TEST_OK_PRE	(TEST_OK_PROBE | TEST_OK_READ | TEST_OK_ERASE)
-#define TEST_OK_PRW	(TEST_OK_PROBE | TEST_OK_READ | TEST_OK_WRITE)
-#define TEST_OK_PREW	(TEST_OK_PROBE | TEST_OK_READ | TEST_OK_ERASE | TEST_OK_WRITE)
-#define TEST_OK_MASK	0x0f
-
-#define TEST_BAD_PROBE	(1 << 4)
-#define TEST_BAD_READ	(1 << 5)
-#define TEST_BAD_ERASE	(1 << 6)
-#define TEST_BAD_WRITE	(1 << 7)
-#define TEST_BAD_EW	(TEST_BAD_ERASE | TEST_BAD_WRITE)
-#define TEST_BAD_REW	(TEST_BAD_READ | TEST_BAD_ERASE | TEST_BAD_WRITE)
-#define TEST_BAD_PREW	(TEST_BAD_PROBE | TEST_BAD_READ | TEST_BAD_ERASE | TEST_BAD_WRITE)
-#define TEST_BAD_MASK	0xf0
-
 /* Timing used in probe routines. ZERO is -2 to differentiate between an unset
  * field and zero delay.
  * 
@@ -265,12 +266,6 @@ int doit(struct flashctx *flash, int force, const char *filename, int read_it, i
 int read_buf_from_file(unsigned char *buf, unsigned long size, const char *filename);
 int write_buf_to_file(unsigned char *buf, unsigned long size, const char *filename);
 
-enum test_state {
-	OK = 0,
-	NT = 1,	/* Not tested */
-	BAD
-};
-
 /* Something happened that shouldn't happen, but we can go on. */
 #define ERROR_NONFATAL 0x100
 
diff --git a/flashchips.c b/flashchips.c
index 8059374..99322b3 100644
--- a/flashchips.c
+++ b/flashchips.c
@@ -2189,7 +2189,7 @@ const struct flashchip flashchips[] = {
 		.model_id	= ATMEL_AT26F004,
 		.total_size	= 512,
 		.page_size	= 256,
-		.tested		= TEST_BAD_WRITE,
+		.tested		= {.probe = NT, .read = NT, .erase = NT, .write = BAD },
 		.feature_bits	= FEATURE_WRSR_WREN,
 		.probe		= probe_spi_rdid,
 		.probe_timing	= TIMING_ZERO,
@@ -5915,7 +5915,7 @@ const struct flashchip flashchips[] = {
 		.model_id	= MACRONIX_MX23L3254,
 		.total_size	= 4096,
 		.page_size	= 256,
-		.tested		= TEST_OK_PR | TEST_BAD_EW,
+		.tested		= {.probe = OK, .read = OK, .erase = NA, .write = NA},
 		.probe		= probe_spi_rdid,
 		.probe_timing	= TIMING_ZERO,
 		.write		= NULL, /* MX23L3254 is a mask ROM, so it is read-only */
diff --git a/flashrom.c b/flashrom.c
index c20461a..4956ded 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -1794,32 +1794,38 @@ void check_chip_supported(const struct flashchip *chip)
 			 "clone the contents of this chip (see man page for "
 			 "details).\n");
 	}
-	if (TEST_OK_MASK != (chip->tested & TEST_OK_MASK)) {
+	if ((chip->tested.probe == BAD) || (chip->tested.probe == NT) ||
+	    (chip->tested.read == BAD)  || (chip->tested.read == NT) ||
+	    (chip->tested.erase == BAD) || (chip->tested.erase == NT) ||
+	    (chip->tested.write == BAD) || (chip->tested.write == NT)){
 		msg_cinfo("===\n");
-		if (chip->tested & TEST_BAD_MASK) {
+		if ((chip->tested.probe == BAD) ||
+		    (chip->tested.read == BAD) ||
+		    (chip->tested.erase == BAD) ||
+		    (chip->tested.write == BAD)) {
 			msg_cinfo("This flash part has status NOT WORKING for operations:");
-			if (chip->tested & TEST_BAD_PROBE)
+			if (chip->tested.probe == BAD)
 				msg_cinfo(" PROBE");
-			if (chip->tested & TEST_BAD_READ)
+			if (chip->tested.read == BAD)
 				msg_cinfo(" READ");
-			if (chip->tested & TEST_BAD_ERASE)
+			if (chip->tested.erase == BAD)
 				msg_cinfo(" ERASE");
-			if (chip->tested & TEST_BAD_WRITE)
+			if (chip->tested.write == BAD)
 				msg_cinfo(" WRITE");
 			msg_cinfo("\n");
 		}
-		if ((!(chip->tested & TEST_BAD_PROBE) && !(chip->tested & TEST_OK_PROBE)) ||
-		    (!(chip->tested & TEST_BAD_READ) && !(chip->tested & TEST_OK_READ)) ||
-		    (!(chip->tested & TEST_BAD_ERASE) && !(chip->tested & TEST_OK_ERASE)) ||
-		    (!(chip->tested & TEST_BAD_WRITE) && !(chip->tested & TEST_OK_WRITE))) {
+		if ((chip->tested.probe == NT) ||
+		    (chip->tested.read == NT) ||
+		    (chip->tested.erase == NT) ||
+		    (chip->tested.write == NT)) {
 			msg_cinfo("This flash part has status UNTESTED for operations:");
-			if (!(chip->tested & TEST_BAD_PROBE) && !(chip->tested & TEST_OK_PROBE))
+			if (chip->tested.probe == NT)
 				msg_cinfo(" PROBE");
-			if (!(chip->tested & TEST_BAD_READ) && !(chip->tested & TEST_OK_READ))
+			if (chip->tested.read == NT)
 				msg_cinfo(" READ");
-			if (!(chip->tested & TEST_BAD_ERASE) && !(chip->tested & TEST_OK_ERASE))
+			if (chip->tested.erase == NT)
 				msg_cinfo(" ERASE");
-			if (!(chip->tested & TEST_BAD_WRITE) && !(chip->tested & TEST_OK_WRITE))
+			if (chip->tested.write == NT)
 				msg_cinfo(" WRITE");
 			msg_cinfo("\n");
 		}
@@ -1862,7 +1868,7 @@ int chip_safety_check(const struct flashctx *flash, int force, int read_it, int
 
 	if (read_it || erase_it || write_it || verify_it) {
 		/* Everything needs read. */
-		if (chip->tested & TEST_BAD_READ) {
+		if (chip->tested.read == BAD) {
 			msg_cerr("Read is not working on this chip. ");
 			if (!force)
 				return 1;
@@ -1876,7 +1882,7 @@ int chip_safety_check(const struct flashctx *flash, int force, int read_it, int
 	}
 	if (erase_it || write_it) {
 		/* Write needs erase. */
-		if (chip->tested & TEST_BAD_ERASE) {
+		if (chip->tested.erase == BAD) {
 			msg_cerr("Erase is not working on this chip. ");
 			if (!force)
 				return 1;
@@ -1889,7 +1895,7 @@ int chip_safety_check(const struct flashctx *flash, int force, int read_it, int
 		}
 	}
 	if (write_it) {
-		if (chip->tested & TEST_BAD_WRITE) {
+		if (chip->tested.write == BAD) {
 			msg_cerr("Write is not working on this chip. ");
 			if (!force)
 				return 1;
diff --git a/ichspi.c b/ichspi.c
index 6c394db..c12ad0d 100644
--- a/ichspi.c
+++ b/ichspi.c
@@ -1236,7 +1236,10 @@ static int ich_hwseq_probe(struct flashctx *flash)
 		msg_cdbg("In that range are %d erase blocks with %d B each.\n",
 			 size_high / erase_size_high, erase_size_high);
 	}
-	flash->chip->tested = TEST_OK_PREW;
+	flash->chip->tested.probe = OK;
+	flash->chip->tested.read = OK;
+	flash->chip->tested.erase = OK;
+	flash->chip->tested.write = OK;
 	return 1;
 }
 
diff --git a/print.c b/print.c
index 759908d..853ac98 100644
--- a/print.c
+++ b/print.c
@@ -249,38 +249,46 @@ static int print_supported_chips(void)
 		for (i = curdevlen; i < maxchiplen; i++)
 			msg_ginfo(" ");
 
-		if ((chip->tested & TEST_OK_PROBE))
+		if (chip->tested.probe == OK)
 			msg_ginfo("P");
+		else if (chip->tested.probe == NA)
+			msg_ginfo("-");
 		else
 			msg_ginfo(" ");
-		if ((chip->tested & TEST_OK_READ))
+		if (chip->tested.read == OK)
 			msg_ginfo("R");
+		else if (chip->tested.read == NA)
+			msg_ginfo("-");
 		else
 			msg_ginfo(" ");
-		if ((chip->tested & TEST_OK_ERASE))
+		if (chip->tested.erase == OK)
 			msg_ginfo("E");
+		else if (chip->tested.erase == NA)
+			msg_ginfo("-");
 		else
 			msg_ginfo(" ");
-		if ((chip->tested & TEST_OK_WRITE))
+		if (chip->tested.write == OK)
 			msg_ginfo("W");
+		else if (chip->tested.write == NA)
+			msg_ginfo("-");
 		else
 			msg_ginfo(" ");
 		for (i = 0; i < border; i++)
 			msg_ginfo(" ");
 
-		if ((chip->tested & TEST_BAD_PROBE))
+		if (chip->tested.probe == BAD)
 			msg_ginfo("P");
 		else
 			msg_ginfo(" ");
-		if ((chip->tested & TEST_BAD_READ))
+		if (chip->tested.read == BAD)
 			msg_ginfo("R");
 		else
 			msg_ginfo(" ");
-		if ((chip->tested & TEST_BAD_ERASE))
+		if (chip->tested.erase == BAD)
 			msg_ginfo("E");
 		else
 			msg_ginfo(" ");
-		if ((chip->tested & TEST_BAD_WRITE))
+		if (chip->tested.write == BAD)
 			msg_ginfo("W");
 		else
 			msg_ginfo(" ");
diff --git a/print_wiki.c b/print_wiki.c
index 5dcc4b6..2dc8156 100644
--- a/print_wiki.c
+++ b/print_wiki.c
@@ -249,7 +249,6 @@ static void print_supported_boards_wiki(void)
 static void print_supported_chips_wiki(int cols)
 {
 	unsigned int lines_per_col;
-	uint32_t t;
 	char *s;
 	char vmax[6];
 	char vmin[6];
@@ -287,7 +286,35 @@ static void print_supported_chips_wiki(int cols)
 			c = !c;
 
 		old = f;
-		t = f->tested;
+		const char *probe, *read, *write, *erase;
+		switch (f->tested.probe) {
+			case OK: probe = "OK"; break;
+			case BAD: probe = "No"; break;
+			case NA: probe = "NA"; break;
+			case DEP: probe = "Dep"; break;
+			default: probe = "?3"; break;
+		}
+		switch (f->tested.read) {
+			case OK: read = "OK"; break;
+			case BAD: read = "No"; break;
+			case NA: read = "NA"; break;
+			case DEP: read = "Dep"; break;
+			default: read = "?3"; break;
+		}
+		switch (f->tested.erase) {
+			case OK: erase = "OK"; break;
+			case BAD: erase = "No"; break;
+			case NA: erase = "NA"; break;
+			case DEP: erase = "Dep"; break;
+			default: erase = "?3"; break;
+		}
+		switch (f->tested.write) {
+			case OK: write = "OK"; break;
+			case BAD: write = "No"; break;
+			case NA: write = "NA"; break;
+			case DEP: write = "Dep"; break;
+			default: write = "?3"; break;
+		}
 		s = flashbuses_to_text(f->bustype);
 		sprintf(vmin, "%0.03f", f->voltage.min / (double)1000);
 		sprintf(vmax, "%0.03f", f->voltage.max / (double)1000);
@@ -298,14 +325,7 @@ static void print_supported_chips_wiki(int cols)
 		       "|| %s || %s \n",
 		       (c == 1) ? "eeeeee" : "dddddd", f->vendor, f->name,
 		       f->total_size, s,
-		       (t & TEST_OK_PROBE) ? "OK" :
-		       (t & TEST_BAD_PROBE) ? "No" : "?3",
-		       (t & TEST_OK_READ) ? "OK" :
-		       (t & TEST_BAD_READ) ? "No" : "?3",
-		       (t & TEST_OK_ERASE) ? "OK" :
-		       (t & TEST_BAD_ERASE) ? "No" : "?3",
-		       (t & TEST_OK_WRITE) ? "OK" :
-		       (t & TEST_BAD_WRITE) ? "No" : "?3",
+		       probe, read, erase, write,
 		       f->voltage.min ? vmin : "N/A",
 		       f->voltage.min ? vmax : "N/A");
 		free(s);
-- 
Kind regards, Stefan Tauner

