>From 84ff9c9182df2c35a641aea6f433af4bca8b0efe Mon Sep 17 00:00:00 2001
From: Tadas Slotkus <devtadas@gmail.com>
Date: Sat, 13 Aug 2011 17:53:31 +0300
Subject: [carFl mem 06/21] Cleanup doit: remove mallocs, unsupported stuff

Support only read/write flash.

Signed-off-by: Tadas Slotkus <devtadas@gmail.com>
---
 cli_classic.c |    2 +-
 flash.h       |    2 +-
 flashrom.c    |   96 +++++---------------------------------------------------
 3 files changed, 11 insertions(+), 89 deletions(-)

diff --git a/cli_classic.c b/cli_classic.c
index 22711b0..be95032 100644
--- a/cli_classic.c
+++ b/cli_classic.c
@@ -437,7 +437,7 @@ int cli_classic(int argc, char *argv[])
 	 * Give the chip time to settle.
 	 */
 	programmer_delay(100000);
-	return doit(fill_flash, force, filename, read_it, write_it, erase_it, verify_it);
+	return doit(fill_flash, force, filename, read_it, write_it);
 
 out_shutdown:
 	programmer_shutdown();
diff --git a/flash.h b/flash.h
index 7f51004..09dc65d 100644
--- a/flash.h
+++ b/flash.h
@@ -224,7 +224,7 @@ void print_version(void);
 void print_banner(void);
 void list_programmers_linebreak(int startcol, int cols, int paren);
 int selfcheck(void);
-int doit(struct flashchip *flash, int force, const char *filename, int read_it, int write_it, int erase_it, int verify_it);
+int doit(struct flashchip *flash, int force, const char *filename, int read_it, int write_it);
 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);
 
diff --git a/flashrom.c b/flashrom.c
index 6d01a6e..36b5e51 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -1828,9 +1828,9 @@ int main(int argc, char *argv[])
 /* FIXME: This function signature needs to be improved once doit() has a better
  * function signature.
  */
-int chip_safety_check(struct flashchip *flash, int force, int read_it, int write_it, int erase_it, int verify_it)
+int chip_safety_check(struct flashchip *flash, int force, int read_it, int write_it)
 {
-	if (!programmer_may_write && (write_it || erase_it)) {
+	if (!programmer_may_write && (write_it)) {
 		msg_perr("Write/erase is not working yet on your programmer in "
 			 "its current configuration.\n");
 		/* --force is the wrong approach, but it's the best we can do
@@ -1841,7 +1841,7 @@ int chip_safety_check(struct flashchip *flash, int force, int read_it, int write
 		msg_cerr("Continuing anyway.\n");
 	}
 
-	if (read_it || erase_it || write_it || verify_it) {
+	if (read_it || write_it) {
 		/* Everything needs read. */
 		if (flash->tested & TEST_BAD_READ) {
 			msg_cerr("Read is not working on this chip. ");
@@ -1855,7 +1855,7 @@ int chip_safety_check(struct flashchip *flash, int force, int read_it, int write
 			return 1;
 		}
 	}
-	if (erase_it || write_it) {
+	if (write_it) {
 		/* Write needs erase. */
 		if (flash->tested & TEST_BAD_ERASE) {
 			msg_cerr("Erase is not working on this chip. ");
@@ -1889,14 +1889,13 @@ int chip_safety_check(struct flashchip *flash, int force, int read_it, int write
  * but right now it allows us to split off the CLI code.
  * Besides that, the function itself is a textbook example of abysmal code flow.
  */
-int doit(struct flashchip *flash, int force, const char *filename, int read_it, int write_it, int erase_it, int verify_it)
+int doit(struct flashchip *flash, int force, const char *filename, int read_it, int write_it)
 {
-	uint8_t *oldcontents;
-	uint8_t *newcontents;
+	uint8_t oldcontents[BUF_SIZE];
+	uint8_t newcontents[BUF_SIZE];
 	int ret = 0;
-	unsigned long size = flash->total_size * 1024;
 
-	if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
+	if (chip_safety_check(flash, force, read_it, write_it)) {
 		msg_cerr("Aborting.\n");
 		ret = 1;
 		goto out_nofree;
@@ -1913,92 +1912,15 @@ int doit(struct flashchip *flash, int force, const char *filename, int read_it,
 		goto out_nofree;
 	}
 
-	oldcontents = malloc(size);
-	if (!oldcontents) {
-		msg_gerr("Out of memory!\n");
-		exit(1);
-	}
-	/* Assume worst case: All bits are 0. */
-	memset(oldcontents, 0x00, size);
-	newcontents = malloc(size);
-	if (!newcontents) {
-		msg_gerr("Out of memory!\n");
-		exit(1);
-	}
-	/* Assume best case: All bits should be 1. */
-	memset(newcontents, 0xff, size);
-	/* Side effect of the assumptions above: Default write action is erase
-	 * because newcontents looks like a completely erased chip, and
-	 * oldcontents being completely 0x00 means we have to erase everything
-	 * before we can write.
-	 */
-
-	if (write_it || verify_it) {
-		if (read_buf_from_file(newcontents, size, filename)) {
-			ret = 1;
-			goto out;
-		}
-
-#if CONFIG_INTERNAL == 1
-		if (programmer == PROGRAMMER_INTERNAL)
-			show_id(newcontents, size, force);
-#endif
-	}
-
-	/* Read the whole chip to be able to check whether regions need to be
-	 * erased and to give better diagnostics in case write fails.
-	 * The alternative would be to read only the regions which are to be
-	 * preserved, but in that case we might perform unneeded erase which
-	 * takes time as well.
-	 */
-	msg_cinfo("Reading old flash chip contents... ");
-	if (flash->read(flash, oldcontents, 0, size)) {
-		ret = 1;
-		msg_cinfo("FAILED.\n");
-		goto out;
-	}
-	msg_cinfo("done.\n");
-
-	// This should be moved into each flash part's code to do it 
-	// cleanly. This does the job.
-	handle_romentries(flash, oldcontents, newcontents);
-
-	// ////////////////////////////////////////////////////////////
-
 	if (write_it) {
 		if (erase_and_write_flash(flash, oldcontents, newcontents, filename)) {
-			msg_cerr("Uh oh. Erase/write failed. Checking if "
-				 "anything changed.\n");
-			if (!flash->read(flash, newcontents, 0, size)) {
-				if (!memcmp(oldcontents, newcontents, size)) {
-					msg_cinfo("Good. It seems nothing was "
-						  "changed.\n");
-					nonfatal_help_message();
-					ret = 1;
-					goto out;
-				}
-			}
-			emergency_help_message();
+			msg_cerr("Uh oh. Erase/write failed.\n");
 			ret = 1;
 			goto out;
 		}
 	}
 
-	if (verify_it) {
-		/* Work around chips which need some time to calm down. */
-		if (write_it)
-			programmer_delay(1000*1000);
-		ret = verify_flash(flash, newcontents);
-		/* If we tried to write, and verification now fails, we
-		 * might have an emergency situation.
-		 */
-		if (ret && write_it)
-			emergency_help_message();
-	}
-
 out:
-	free(oldcontents);
-	free(newcontents);
 out_nofree:
 	programmer_shutdown();
 	return ret;
-- 
1.7.6

