spi25: Replace spi_read_chunked() with more abstract version

The new flashprog_read_chunked() takes a low-level reading function as
argument. This allows us to make use of the chunking with non-SPI read
functions.

Change-Id: Ica1b616e75e4e7682120928588e231c82cf4cf70
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/74865
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
diff --git a/helpers.c b/helpers.c
index 5b47b68..27b9cf1 100644
--- a/helpers.c
+++ b/helpers.c
@@ -20,6 +20,21 @@
 #include <string.h>
 #include "flash.h"
 
+int flashprog_read_chunked(struct flashctx *const flash, uint8_t *dst, unsigned int start, unsigned int len,
+			  const unsigned int chunksize, readfunc_t *const read)
+{
+	int ret;
+	size_t to_read;
+	for (; len; len -= to_read, dst += to_read, start += to_read) {
+		to_read = min(chunksize, len);
+		ret = read(flash, dst, start, to_read);
+		if (ret)
+			return ret;
+		flashprog_progress_add(flash, to_read);
+	}
+	return 0;
+}
+
 /* Returns the minimum number of bits needed to represent the given address.
  * FIXME: use mind-blowing implementation. */
 uint32_t address_to_bits(uint32_t addr)
diff --git a/include/chipdrivers.h b/include/chipdrivers.h
index c8c61ca..1ac5e53 100644
--- a/include/chipdrivers.h
+++ b/include/chipdrivers.h
@@ -56,7 +56,6 @@
 const uint8_t *spi_get_opcode_from_erasefn(erasefunc_t *func, bool *native_4ba);
 int spi_chip_write_1(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len);
 int spi_nbyte_read(struct flashctx *flash, uint8_t *dst, unsigned int addr, unsigned int len);
-int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len, unsigned int chunksize);
 int spi_write_chunked(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len, unsigned int chunksize);
 int spi_enter_4ba(struct flashctx *flash);
 int spi_exit_4ba(struct flashctx *flash);
diff --git a/include/flash.h b/include/flash.h
index b411f34..cfc9c4e 100644
--- a/include/flash.h
+++ b/include/flash.h
@@ -178,6 +178,7 @@
 struct flashprog_flashctx;
 #define flashctx flashprog_flashctx /* TODO: Agree on a name and convert all occurrences. */
 typedef int (erasefunc_t)(struct flashctx *flash, unsigned int addr, unsigned int blocklen);
+typedef int (readfunc_t)(struct flashctx *flash, uint8_t *dst, unsigned int start, unsigned int len);
 
 enum flash_reg {
 	INVALID_REG = 0,
@@ -418,6 +419,7 @@
 void print_supported_wiki(void);
 
 /* helpers.c */
+int flashprog_read_chunked(struct flashctx *, uint8_t *dst, unsigned int start, unsigned int len, unsigned int chunksize, readfunc_t *);
 uint32_t address_to_bits(uint32_t addr);
 unsigned int bitcount(unsigned long a);
 #undef MIN
diff --git a/spi.c b/spi.c
index 296e8df..a6b5124 100644
--- a/spi.c
+++ b/spi.c
@@ -80,7 +80,7 @@
 			 "Please report a bug at flashprog@flashprog.org\n", __func__);
 		return 1;
 	}
-	return spi_read_chunked(flash, buf, start, len, max_data);
+	return flashprog_read_chunked(flash, buf, start, len, max_data, spi_nbyte_read);
 }
 
 int default_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
diff --git a/spi25.c b/spi25.c
index 0597cea..f8fdf8e 100644
--- a/spi25.c
+++ b/spi25.c
@@ -654,25 +654,6 @@
 }
 
 /*
- * Read a part of the flash chip.
- * Data is read in chunks with a maximum size of chunksize.
- */
-int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start,
-		     unsigned int len, unsigned int chunksize)
-{
-	int ret;
-	size_t to_read;
-	for (; len; len -= to_read, buf += to_read, start += to_read) {
-		to_read = min(chunksize, len);
-		ret = spi_nbyte_read(flash, buf, start, to_read);
-		if (ret)
-			return ret;
-		flashprog_progress_add(flash, to_read);
-	}
-	return 0;
-}
-
-/*
  * Write a part of the flash chip.
  * FIXME: Use the chunk code from Michael Karcher instead.
  * Each page is written separately in chunks with a maximum size of chunksize.