>From 34a675e998a5054ac7472c40f4a300b57deb83fc Mon Sep 17 00:00:00 2001
From: Tadas Slotkus <devtadas@gmail.com>
Date: Fri, 19 Aug 2011 16:40:47 +0300
Subject: [carFl mem 37/65] Globalisation pass 1: flashrom.c

This commit breaks builds until "Globalistation pass 1: fixes".

It does nothing more, just passes globals structure across function
calls.

Signed-off-by: Tadas Slotkus <devtadas@gmail.com>
---
 flashrom.c |   90 ++++++++++++++++++++++++++++++-----------------------------
 1 files changed, 46 insertions(+), 44 deletions(-)

diff --git a/flashrom.c b/flashrom.c
index bb06752..084a912 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -497,7 +497,7 @@ int register_shutdown(int (*function) (void *data), void *data)
 	return 0;
 }
 
-int programmer_init(char *param)
+int programmer_init(globals *g, char *param)
 {
 	int ret;
 	/* Initialize all programmer specific data. */
@@ -519,7 +519,7 @@ int programmer_init(char *param)
 	programmer_param = param;
 	msg_pdbg("Initializing %s programmer\n",
 		 programmer_table[programmer].name);
-	ret = programmer_table[programmer].init();
+	ret = programmer_table[programmer].init(g);
 	if (programmer_param && strlen(programmer_param)) {
 		msg_perr("Unhandled programmer parameters: %s\n",
 			 programmer_param);
@@ -541,75 +541,75 @@ int programmer_shutdown(void)
 	return ret;
 }
 
-void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
+void *programmer_map_flash_region(globals *g, const char *descr, unsigned long phys_addr,
 				  size_t len)
 {
 	return programmer_table[programmer].map_flash_region(descr,
 							     phys_addr, len);
 }
 
-void programmer_unmap_flash_region(void *virt_addr, size_t len)
+void programmer_unmap_flash_region(globals *g, void *virt_addr, size_t len)
 {
 	programmer_table[programmer].unmap_flash_region(virt_addr, len);
 }
 
-void chip_writeb(uint8_t val, chipaddr addr)
+void chip_writeb(globals *g, uint8_t val, chipaddr addr)
 {
 	programmer_table[programmer].chip_writeb(val, addr);
 }
 
-void chip_writew(uint16_t val, chipaddr addr)
+void chip_writew(globals *g, uint16_t val, chipaddr addr)
 {
 	programmer_table[programmer].chip_writew(val, addr);
 }
 
-void chip_writel(uint32_t val, chipaddr addr)
+void chip_writel(globals *g, uint32_t val, chipaddr addr)
 {
 	programmer_table[programmer].chip_writel(val, addr);
 }
 
-void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
+void chip_writen(globals *g, uint8_t *buf, chipaddr addr, size_t len)
 {
 	programmer_table[programmer].chip_writen(buf, addr, len);
 }
 
-uint8_t chip_readb(const chipaddr addr)
+uint8_t chip_readb(globals *g, const chipaddr addr)
 {
 	return programmer_table[programmer].chip_readb(addr);
 }
 
-uint16_t chip_readw(const chipaddr addr)
+uint16_t chip_readw(globals *g, const chipaddr addr)
 {
 	return programmer_table[programmer].chip_readw(addr);
 }
 
-uint32_t chip_readl(const chipaddr addr)
+uint32_t chip_readl(globals *g, const chipaddr addr)
 {
 	return programmer_table[programmer].chip_readl(addr);
 }
 
-void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
+void chip_readn(globals *g, uint8_t *buf, chipaddr addr, size_t len)
 {
 	programmer_table[programmer].chip_readn(buf, addr, len);
 }
 
-void programmer_delay(int usecs)
+void programmer_delay(globals *g, int usecs)
 {
 	programmer_table[programmer].delay(usecs);
 }
 
-void map_flash_registers(struct flashchip *flash)
+void map_flash_registers(globals *g, struct flashchip *flash)
 {
 	size_t size = flash->total_size * 1024;
 	/* Flash registers live 4 MByte below the flash. */
 	/* FIXME: This is incorrect for nonstandard flashbase. */
-	flash->virtual_registers = (chipaddr)programmer_map_flash_region(
+	flash->virtual_registers = (chipaddr)programmer_map_flash_region(g,
 	    "flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
 }
 
-int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
+int read_memmapped(globals *g, struct flashchip *flash, uint8_t *buf, int start, int len)
 {
-	chip_readn(buf, flash->virtual_memory + start, len);
+	chip_readn(g, buf, flash->virtual_memory + start, len);
 
 	return 0;
 }
@@ -730,13 +730,13 @@ static unsigned int count_usable_erasers(const struct flashchip *flash)
 }
 
 /* start is an offset to the base address of the flash chip */
-int check_erased_range(struct flashchip *flash, int start, int len, uint8_t *cmpbuf, uint8_t *curcontents)
+int check_erased_range(globals *g, struct flashchip *flash, int start, int len, uint8_t *cmpbuf, uint8_t *curcontents)
 {
 	int starthere = start, lenhere = BUF_SIZE;
 
 	for (; starthere < start + len; starthere += lenhere) {
 		memset(cmpbuf, 0xff, lenhere);
-		flash->read(flash, curcontents, starthere, lenhere);
+		flash->read(g, flash, curcontents, starthere, lenhere);
 		if (memcmp(curcontents, cmpbuf, lenhere))
 			return -1;
 	}
@@ -751,7 +751,7 @@ int check_erased_range(struct flashchip *flash, int start, int len, uint8_t *cmp
  * @message	string to print in the "FAILED" message
  * @return	0 for success, -1 for failure
  */
-int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len,
+int verify_range(globals *g, struct flashchip *flash, uint8_t *cmpbuf, int start, int len,
 		 const char *message)
 {
 	int i, ret = 0, failcount = 0;
@@ -775,7 +775,7 @@ int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len,
 	if (!message)
 		message = "VERIFY";
 
-	ret = flash->read(flash, readbuf, start, len);
+	ret = flash->read(g, flash, readbuf, start, len);
 	if (ret) {
 		msg_gerr("Verification impossible because read failed "
 			 "at 0x%x (len 0x%x)\n", start, len);
@@ -1121,7 +1121,7 @@ int check_max_decode(enum chipbustype buses, uint32_t size)
 	return 1;
 }
 
-int probe_flash(int startchip, struct flashchip *fill_flash, int force)
+int probe_flash(globals *g, int startchip, struct flashchip *fill_flash, int force)
 {
 	const struct flashchip *flash;
 	unsigned long base = 0;
@@ -1162,12 +1162,12 @@ int probe_flash(int startchip, struct flashchip *fill_flash, int force)
 		*fill_flash = *flash;
 
 		base = flashbase ? flashbase : (0xffffffff - size + 1);
-		fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
+		fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region(g, "flash chip", base, size);
 
 		if (force)
 			break;
 
-		if (fill_flash->probe(fill_flash) != 1)
+		if (fill_flash->probe(g, fill_flash) != 1)
 			goto notfound;
 
 		/* If this is the first chip found, accept it.
@@ -1182,7 +1182,7 @@ int probe_flash(int startchip, struct flashchip *fill_flash, int force)
 			break;
 
 notfound:
-		programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
+		programmer_unmap_flash_region(g, (void *)fill_flash->virtual_memory, size);
 	}
 
 	if (!flash || !flash->name)
@@ -1206,7 +1206,7 @@ notfound:
 	 */
 	if (!force)
 		if (fill_flash->printlock)
-			fill_flash->printlock(fill_flash);
+			fill_flash->printlock(g, fill_flash);
 
 	/* Return position of matching chip. */
 	return flash - flashchips;
@@ -1321,7 +1321,7 @@ int write_chunk_to_file(unsigned char *buf, unsigned long start, unsigned int si
 	return 0;
 }
 
-int read_flash_to_file(struct flashchip *flash, const char *filename, uint8_t *buf)
+int read_flash_to_file(globals *g, struct flashchip *flash, const char *filename, uint8_t *buf)
 {
 	unsigned long size = flash->total_size * 1024;
 	int ret = 0, starthere = 0, lenhere = BUF_SIZE;
@@ -1342,7 +1342,7 @@ int read_flash_to_file(struct flashchip *flash, const char *filename, uint8_t *b
 		goto out_free;
 	}
 	for (; starthere < size; starthere += lenhere) {
-		if (flash->read(flash, buf, starthere, lenhere)) {
+		if (flash->read(g, flash, buf, starthere, lenhere)) {
 			msg_cerr("Read operation failed!\n");
 			ret = 1;
 			goto out_free;
@@ -1425,11 +1425,12 @@ static int selfcheck_eraseblocks(const struct flashchip *flash)
 	return ret;
 }
 
-static int erase_and_write_block_helper(struct flashchip *flash,
+static int erase_and_write_block_helper(globals *g, struct flashchip *flash,
 					unsigned int start, unsigned int len,
 					uint8_t *curcontents,
 					uint8_t *newcontents,
-					int (*erasefn) (struct flashchip *flash,
+					int (*erasefn) (globals *g,
+							struct flashchip *flash,
 							unsigned int addr,
 							unsigned int len),
 					const char *filename)
@@ -1442,7 +1443,7 @@ static int erase_and_write_block_helper(struct flashchip *flash,
 	/* FIXME: Assume 256 byte granularity for now to play it safe. */
 	lenhere2 = BUF_SIZE;
 	for (starthere2 = start; starthere2 < start + len; starthere2 += lenhere2) {
-		flash->read(flash, curcontents, starthere2, lenhere2);
+		flash->read(g, flash, curcontents, starthere2, lenhere2);
 		read_chunk_from_file(newcontents, starthere2, lenhere2, filename);
 		msg_cdbg("C");
 		if (need_erase(curcontents, newcontents, lenhere2, gran)) {
@@ -1452,10 +1453,10 @@ static int erase_and_write_block_helper(struct flashchip *flash,
 	}
 	if (!skip) { //need erase
 		msg_cdbg("E");
-		ret = erasefn(flash, start, len);
+		ret = erasefn(g, flash, start, len);
 		if (ret)
 			return ret;
-		if (check_erased_range(flash, start, len, newcontents, curcontents)) {
+		if (check_erased_range(g, flash, start, len, newcontents, curcontents)) {
 			msg_cerr("ERASE FAILED!\n");
 			return -1;
 		}
@@ -1464,7 +1465,7 @@ static int erase_and_write_block_helper(struct flashchip *flash,
 	}
 
 	for (starthere2 = start; starthere2 < start + len; starthere2 += lenhere2) {
-		flash->read(flash, curcontents, starthere2, lenhere2);
+		flash->read(g, flash, curcontents, starthere2, lenhere2);
 		read_chunk_from_file(newcontents, starthere2, lenhere2, filename);
 		starthere = 0; lenhere = 0;
 		/* get_next_write() sets starthere to a new value after the call. */
@@ -1474,7 +1475,7 @@ static int erase_and_write_block_helper(struct flashchip *flash,
 			if (!writecount++)
 				msg_cdbg("W");
 			/* Needs the partial write function signature. */
-			ret = flash->write(flash, newcontents + starthere,
+			ret = flash->write(g, flash, newcontents + starthere,
 					   starthere2 + starthere, lenhere);
 			if (ret)
 				return ret;
@@ -1487,13 +1488,14 @@ static int erase_and_write_block_helper(struct flashchip *flash,
 	return ret;
 }
 
-static int walk_eraseregions(struct flashchip *flash, int erasefunction,
-			     int (*do_something) (struct flashchip *flash,
+static int walk_eraseregions(globals *g, struct flashchip *flash, int erasefunction,
+			     int (*do_something) (globals *g, struct flashchip *flash,
 						  unsigned int addr,
 						  unsigned int len,
 						  uint8_t *param1,
 						  uint8_t *param2,
 						  int (*erasefn) (
+							globals *g,
 							struct flashchip *flash,
 							unsigned int addr,
 							unsigned int len),
@@ -1515,7 +1517,7 @@ static int walk_eraseregions(struct flashchip *flash, int erasefunction,
 				msg_cdbg(", ");
 			msg_cdbg("0x%06x-0x%06x", start,
 				     start + len - 1);
-			if (do_something(flash, start, len, param1, param2,
+			if (do_something(g, flash, start, len, param1, param2,
 					 eraser.block_erase, filename)) {
 				return 1;
 			}
@@ -1550,7 +1552,7 @@ static int check_block_eraser(const struct flashchip *flash, int k, int log)
 	return 0;
 }
 
-int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents,
+int erase_and_write_flash(globals *g, struct flashchip *flash, uint8_t *oldcontents,
 			  uint8_t *newcontents, const char *filename)
 {
 	int k, ret = 1;
@@ -1569,7 +1571,7 @@ int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents,
 		if (check_block_eraser(flash, k, 1))
 			continue;
 		usable_erasefunctions--;
-		ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
+		ret = walk_eraseregions(g, flash, k, &erase_and_write_block_helper,
 					oldcontents, newcontents, filename);
 		/* If everything is OK, don't try another erase function. */
 		if (!ret)
@@ -1881,7 +1883,7 @@ 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 doit(globals *g, struct flashchip *flash, int force, const char *filename, int read_it, int write_it)
 {
 	uint8_t oldcontents[BUF_SIZE];
 	uint8_t newcontents[BUF_SIZE];
@@ -1897,15 +1899,15 @@ int doit(struct flashchip *flash, int force, const char *filename, int read_it,
 	 * erase and write.
 	 */
 	if (flash->unlock)
-		flash->unlock(flash);
+		flash->unlock(g, flash);
 
 	if (read_it) {
-		ret = read_flash_to_file(flash, filename, oldcontents);
+		ret = read_flash_to_file(g, flash, filename, oldcontents);
 		goto out_nofree;
 	}
 
 	if (write_it) {
-		if (erase_and_write_flash(flash, oldcontents, newcontents, filename)) {
+		if (erase_and_write_flash(g, flash, oldcontents, newcontents, filename)) {
 			msg_cerr("Uh oh. Erase/write failed.\n");
 			ret = 1;
 			goto out;
-- 
1.7.6

